Skip to content

Commit

Permalink
perf(compose): check if it is a instance only once (honojs#3585)
Browse files Browse the repository at this point in the history
* perf(compose): check if it is a instance only once

* chore: fix syntax

* chore: sort if condition
  • Loading branch information
EdamAme-x authored and TinsFox committed Nov 11, 2024
1 parent 29de142 commit 4352ef5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const compose = <C extends ComposeContext, E extends Env = Env>(
): ((context: C, next?: Function) => Promise<C>) => {
return (context, next) => {
let index = -1
const isContext = context instanceof Context

return dispatch(0)

/**
Expand All @@ -58,15 +60,15 @@ export const compose = <C extends ComposeContext, E extends Env = Env>(

if (middleware[i]) {
handler = middleware[i][0][0]
if (context instanceof Context) {
if (isContext) {
context.req.routeIndex = i
}
} else {
handler = (i === middleware.length && next) || undefined
}

if (!handler) {
if (context instanceof Context && context.finalized === false && onNotFound) {
if (isContext && context.finalized === false && onNotFound) {
res = await onNotFound(context)
}
} else {
Expand All @@ -75,7 +77,7 @@ export const compose = <C extends ComposeContext, E extends Env = Env>(
return dispatch(i + 1)
})
} catch (err) {
if (err instanceof Error && context instanceof Context && onError) {
if (err instanceof Error && isContext && onError) {
context.error = err
res = await onError(err, context)
isError = true
Expand Down

0 comments on commit 4352ef5

Please sign in to comment.