* Creates the two-phase state machine for the route hook. * * - Pre-activation: collects sub-app references into a pending set. * - Post-activation: instruments sub-apps immediately at mount time.
()
| 33 | * - Post-activation: instruments sub-apps immediately at mount time. |
| 34 | */ |
| 35 | function createRouteHook(): { handle: RouteHookHandle; onSubAppMounted: (subApp: HonoAny) => void } { |
| 36 | const pendingSubApps = new Set<HonoAny>(); |
| 37 | let activated = false; |
| 38 | |
| 39 | return { |
| 40 | handle: { |
| 41 | activate: () => { |
| 42 | activated = true; |
| 43 | }, |
| 44 | getPendingSubApps: () => pendingSubApps, |
| 45 | }, |
| 46 | onSubAppMounted: (subApp: HonoAny) => { |
| 47 | if (activated) { |
| 48 | DEBUG_BUILD && debug.log(`[hono] Instrumenting sub-app at mount time (${subApp.routes.length} routes).`); |
| 49 | wrapSubAppMiddleware(subApp.routes as HonoRoute[]); |
| 50 | patchAppRequest(subApp); |
| 51 | } else { |
| 52 | DEBUG_BUILD && |
| 53 | debug.log(`[hono] Collecting sub-app for deferred instrumentation (${subApp.routes.length} routes).`); |
| 54 | pendingSubApps.add(subApp); |
| 55 | } |
| 56 | }, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Installs a hook on `HonoBase.prototype.route` to intercept sub-app mounting. |
no test coverage detected