perf(compose): simplify (#1701)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* perf(compose): simplify * denoify
authored by Yusuke Wada · committed by GitHub · parent 871cf80 · Browse files · 4 files·+20 added−66 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+20 added−66 removed
- other:+20 added−66 removed
Provenance
- Commit
16396bc- Tree
e116477- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
871cf80
4 files changed+20 added−66 removed
modified:deno_dist/compose.ts+9 added-31 removed
15 unmodified lines1617181920202122232416 unmodified lines414243444546474546474849504 unmodified lines555657586061625960636465666768697071727374757677787980818283848586616263646566676815 unmodified lines return (context: C, next?: Function) => { let index = -1 return dispatch(0)
function dispatch(i: number): C | Promise<C> { async function dispatch(i: number): Promise<C> { if (i <= index) { throw new Error('next() called multiple times') } index = i16 unmodified lines res = onNotFound(context) } } else { try { res = handler(context, () => { const dispatchRes = dispatch(i + 1) return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes) res = await handler(context, () => { return dispatch(i + 1) }) } catch (err) { if (err instanceof Error && context instanceof Context && onError) { context.error = err4 unmodified lines } } }
if (!(res instanceof Promise)) { if (res !== undefined && 'response' in res) { res = res['response'] if (res !== undefined && 'response' in res) { res = res['response'] } if (res && (context.finalized === false || isError)) { context.res = res } return context } else { return res .then((res) => { if (res !== undefined && 'response' in res) { res = res['response'] } if (res && context.finalized === false) { context.res = res } return context }) .catch(async (err) => { if (err instanceof Error && context instanceof Context && onError) { context.error = err context.res = await onError(err, context) return context } throw err }) } if (res && (context.finalized === false || isError)) { context.res = res } return context } }}modified:deno_dist/hono-base.ts+1 added-2 removed
326 unmodified lines327 const composed = compose<Context>(handlers, this.errorHandler, this.notFoundHandler)328 329 return (async () => {330 try {331- const tmp = composed(c)332- const context = tmp.constructor.name === 'Promise' ? await tmp : (tmp as Context)331+ const context = await composed(c)332 if (!context.finalized) {333 throw new Error(334 'Context is not finalized. You may forget returning Response object or `await next()`'335 )49 unmodified linesmodified:src/compose.ts+9 added-31 removed
15 unmodified lines1617181920202122232416 unmodified lines414243444546474546474849504 unmodified lines555657586061625960636465666768697071727374757677787980818283848586616263646566676815 unmodified lines return (context: C, next?: Function) => { let index = -1 return dispatch(0)
function dispatch(i: number): C | Promise<C> { async function dispatch(i: number): Promise<C> { if (i <= index) { throw new Error('next() called multiple times') } index = i16 unmodified lines res = onNotFound(context) } } else { try { res = handler(context, () => { const dispatchRes = dispatch(i + 1) return dispatchRes instanceof Promise ? dispatchRes : Promise.resolve(dispatchRes) res = await handler(context, () => { return dispatch(i + 1) }) } catch (err) { if (err instanceof Error && context instanceof Context && onError) { context.error = err4 unmodified lines } } }
if (!(res instanceof Promise)) { if (res !== undefined && 'response' in res) { res = res['response'] if (res !== undefined && 'response' in res) { res = res['response'] } if (res && (context.finalized === false || isError)) { context.res = res } return context } else { return res .then((res) => { if (res !== undefined && 'response' in res) { res = res['response'] } if (res && context.finalized === false) { context.res = res } return context }) .catch(async (err) => { if (err instanceof Error && context instanceof Context && onError) { context.error = err context.res = await onError(err, context) return context } throw err }) } if (res && (context.finalized === false || isError)) { context.res = res } return context } }}modified:src/hono-base.ts+1 added-2 removed
326 unmodified lines327 const composed = compose<Context>(handlers, this.errorHandler, this.notFoundHandler)328 329 return (async () => {330 try {331- const tmp = composed(c)332- const context = tmp.constructor.name === 'Promise' ? await tmp : (tmp as Context)331+ const context = await composed(c)332 if (!context.finalized) {333 throw new Error(334 'Context is not finalized. You may forget returning Response object or `await next()`'335 )49 unmodified lines