Skip to content

Commit

Permalink
Screw creating objects for TypeScript's benefit. Fewer allocations ar…
Browse files Browse the repository at this point in the history
…e better.
  • Loading branch information
benjie committed May 21, 2024
1 parent b31c86e commit 6439091
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions utils/graphile-config/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ export interface MiddlewareNext<
> {
(): TRawResult;
callback(
callback: (
params:
| { error: object; result?: never }
| { error?: undefined; result: TAwaitedResult },
) => TRawResult,
callback: (error: object | null, result: TAwaitedResult) => TRawResult,
): TRawResult;
}

Expand Down Expand Up @@ -163,15 +159,18 @@ function makeNext<TRawResult, TAwaitedResult = Awaited<TRawResult>>(
try {
result = fn() as PromiseOrDirect<TAwaitedResult>;
} catch (error) {
return callback({ error });
return callback(error, undefined as any);
}
if (isPromiseLike(result)) {
return result.then(
(result) => callback({ result }),
(error) => callback({ error }),
(result) =>
callback(null, result) as
| TAwaitedResult
| PromiseLike<TAwaitedResult>,
callback as any,
) as TRawResult;
} else {
return callback({ result });
return callback(null, result);
}
};
return next;
Expand Down

0 comments on commit 6439091

Please sign in to comment.