mirror of
https://github.com/honojs/hono.git
synced 2024-11-24 11:07:29 +01:00
perf(compose): check if it is a instance only once (#3585)
* perf(compose): check if it is a instance only once * chore: fix syntax * chore: sort if condition
This commit is contained in:
parent
3164326351
commit
efd3d04c56
@ -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)
|
||||
|
||||
/**
|
||||
@ -58,7 +60,7 @@ 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 {
|
||||
@ -66,7 +68,7 @@ export const compose = <C extends ComposeContext, E extends Env = Env>(
|
||||
}
|
||||
|
||||
if (!handler) {
|
||||
if (context instanceof Context && context.finalized === false && onNotFound) {
|
||||
if (isContext && context.finalized === false && onNotFound) {
|
||||
res = await onNotFound(context)
|
||||
}
|
||||
} else {
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user