Skip to content

Commit

Permalink
adjust types and use our own recursion guard
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed May 7, 2024
1 parent 0c64b3e commit 848a57f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ type EngineInfoByRoute = Record<string, { name: string }>;

let Router: typeof EmberRouter;

interface GetRoute {
(name: string): unknown;
isEmbroiderRouterHandler?: true;
}

interface Internals {
_routerMicrolib: {
getRoute: GetRoute;
};
}

if (macroCondition(getGlobalConfig<GlobalConfig>()['@embroider/core']?.active ?? false)) {
const waiter = buildWaiter('@embroider/router:lazy-route-waiter');

Expand Down Expand Up @@ -65,19 +76,18 @@ if (macroCondition(getGlobalConfig<GlobalConfig>()['@embroider/core']?.active ??

// This is the framework method that we're overriding to provide our own
// handlerResolver.
setupRouter(...args: unknown[]) {
setupRouter(this: this & Internals, ...args: unknown[]) {
// @ts-expect-error extending private method
let isSetup = super.setupRouter(...args);
if (isSetup) {
let microLib = (this as unknown as { _routerMicrolib: { getRoute: (name: string) => unknown } })
._routerMicrolib;
let microLib = this._routerMicrolib;
if (!microLib.getRoute.isEmbroiderRouterHandler) {
microLib.getRoute = this._handlerResolver(microLib.getRoute.bind(microLib));
}
return isSetup;
}

private _handlerResolver(original: (name: string) => unknown) {
return (name: string) => {
let handler = ((name: string) => {
const bundle = this.lazyBundle(name);
if (!bundle || bundle.loaded) {
return original(name);
Expand All @@ -96,7 +106,9 @@ if (macroCondition(getGlobalConfig<GlobalConfig>()['@embroider/core']?.active ??
throw err;
}
);
};
}) as GetRoute;
handler.isEmbroiderRouterHandler = true;
return handler;
}
}

Expand Down

0 comments on commit 848a57f

Please sign in to comment.