@real-router/core 0.54.4 → 0.54.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Router-BzV5P1lA.js +6 -0
- package/dist/cjs/Router-BzV5P1lA.js.map +1 -0
- package/dist/cjs/api.js +1 -1
- package/dist/cjs/{cloneRouter-CR7rdWPl.js → cloneRouter-D5bMudso.js} +2 -2
- package/dist/cjs/{cloneRouter-CR7rdWPl.js.map → cloneRouter-D5bMudso.js.map} +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/utils.js +1 -1
- package/dist/cjs/validation.d.ts +55 -14
- package/dist/cjs/validation.d.ts.map +1 -1
- package/dist/esm/Router-_BDnheMY.mjs +6 -0
- package/dist/esm/Router-_BDnheMY.mjs.map +1 -0
- package/dist/esm/api.mjs +1 -1
- package/dist/esm/{cloneRouter-BBjARLMk.mjs → cloneRouter-DM59kihh.mjs} +2 -2
- package/dist/esm/{cloneRouter-BBjARLMk.mjs.map → cloneRouter-DM59kihh.mjs.map} +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/utils.mjs +1 -1
- package/dist/esm/validation.d.mts +55 -14
- package/dist/esm/validation.d.mts.map +1 -1
- package/package.json +3 -3
- package/src/api/cloneRouter.ts +26 -7
- package/src/namespaces/EventBusNamespace/EventBusNamespace.ts +39 -10
- package/src/namespaces/NavigationNamespace/transition/guardPhase.ts +3 -0
- package/src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.ts +264 -83
- package/dist/cjs/Router-fHKWlCpv.js +0 -6
- package/dist/cjs/Router-fHKWlCpv.js.map +0 -1
- package/dist/esm/Router-Baa4TIPc.mjs +0 -6
- package/dist/esm/Router-Baa4TIPc.mjs.map +0 -1
package/dist/esm/api.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as e,c as t,d as n,i as r,l as i,r as a,s as o}from"./Router-
|
|
1
|
+
import{a as e,c as t,d as n,i as r,l as i,r as a,s as o}from"./Router-_BDnheMY.mjs";import{r as s,t as c}from"./internals-C59msvHY.mjs";import{i as l,n as u,r as d,t as f}from"./cloneRouter-DM59kihh.mjs";import{logger as p}from"@real-router/logger";function m(e,t,n){if(t){let n=t===e,r=t.startsWith(`${e}.`);if(n||r){let r=n?``:` (current: "${t}")`;return p.warn(`router.removeRoute`,`Cannot remove route "${e}" — it is currently active${r}. Navigate away first.`),!1}}return n&&p.warn(`router.removeRoute`,`Route "${e}" removed while navigation is in progress. This may cause unexpected behavior.`),!0}function h(e){return e?(p.error(`router.clearRoutes`,`Cannot clear routes while navigation is in progress. Wait for navigation to complete.`),!1):!0}function g(e,t,n=``){for(let r of e){let e=n?`${n}.${r.name}`:r.name;if(e===t)return r;if(r.children&&t.startsWith(`${e}.`))return g(r.children,t,e)}}function _(e,t,n,r){let i=t=>t===e||t.startsWith(`${e}.`);o(t.decoders,i),o(t.encoders,i),o(t.defaultParams,i),o(t.forwardMap,i),o(t.forwardFnMap,i),o(n,i),o(t.forwardMap,e=>i(t.forwardMap[e]));let[a,s]=r.getFactories();for(let e of Object.keys(s))i(e)&&r.clearCanActivate(e);for(let e of Object.keys(a))i(e)&&r.clearCanDeactivate(e)}function v(e,t,n,r){return t===null?(delete n.forwardMap[e],delete n.forwardFnMap[e]):typeof t==`string`?(delete n.forwardFnMap[e],n.forwardMap[e]=t):(delete n.forwardMap[e],n.forwardFnMap[e]=t),r(n)}function y(e,t,n,r){let i={name:e.name,path:e.path},a=n.forwardFnMap[t],o=n.forwardMap[t];a===void 0?o!==void 0&&(i.forwardTo=o):i.forwardTo=a,t in n.defaultParams&&(i.defaultParams=n.defaultParams[t]),t in n.decoders&&(i.decodeParams=n.decoders[t]),t in n.encoders&&(i.encodeParams=n.encoders[t]);let[s,c]=r;return t in c&&(i.canActivate=c[t]),t in s&&(i.canDeactivate=s[t]),e.children&&(i.children=e.children.map(e=>y(e,`${t}.${e.name}`,n,r))),i}function b(t,n,r){if(r){let e=g(t.definitions,r);e.children??=[];for(let t of n)e.children.push(i(t))}else for(let e of n)t.definitions.push(i(e));e(n,t.config,t.routeCustomFields,t.pendingCanActivate,t.pendingCanDeactivate,t.depsStore,r??``),t.treeOperations.commitTreeChanges(t)}function x(t,n,r,o,s){a(t),t.lifecycleNamespace.clearDefinitionGuards();for(let e of n)t.definitions.push(i(e));if(e(n,t.config,t.routeCustomFields,t.pendingCanActivate,t.pendingCanDeactivate,t.depsStore,``),t.treeOperations.commitTreeChanges(t),o!==void 0){let e=r.matchPath(o,r.getOptions());e?r.setState({...e,transition:s}):r.clearState()}}function S(e,n){return t(e.definitions,n)?(_(n,e.config,e.routeCustomFields,e.lifecycleNamespace),e.treeOperations.commitTreeChanges(e),!0):!1}function C(e,t,n){if(n.forwardTo!==void 0&&(e.resolvedForwardMap=v(t,n.forwardTo,e.config,e=>r(e))),n.defaultParams!==void 0&&(n.defaultParams===null?delete e.config.defaultParams[t]:e.config.defaultParams[t]=n.defaultParams),n.decodeParams!==void 0)if(n.decodeParams===null)delete e.config.decoders[t];else{let r=n.decodeParams;e.config.decoders[t]=e=>r(e)??e}if(n.encodeParams!==void 0)if(n.encodeParams===null)delete e.config.encoders[t];else{let r=n.encodeParams;e.config.encoders[t]=e=>r(e)??e}}function w(e,t){let n=e.matcher.getSegmentsByName(t);if(!n)return;let r=n.at(-1),i=e.treeOperations.nodeToDefinition(r),a=e.lifecycleNamespace.getFactories();return y(i,t,e.config,a)}function T(e){let t=s(e),r=t.routeGetStore(),i=c(`add`,(e,t)=>{b(r,e,t?.parent)},t.interceptors);return{add:(e,a)=>{l(t.isDisposed);let o=Array.isArray(e)?e:[e],s=a?.parent;n(o,t.validator),s!==void 0&&t.validator?.routes.validateParentOption(s,r.tree),t.validator?.routes.throwIfInternalRouteInArray(o,`addRoute`),t.validator?.routes.validateAddRouteArgs(o),t.validator?.routes.validateRoutes(o,r),i(o,s===void 0?void 0:{parent:s})},remove:e=>{l(t.isDisposed),t.validator?.routes.validateRemoveRouteArgs(e),t.validator?.routes.throwIfInternalRoute(e,`removeRoute`),m(e,t.getStateName(),t.isTransitioning())&&(S(r,e)||p.warn(`router.removeRoute`,`Route "${e}" not found. No changes made.`))},update:(e,n)=>{l(t.isDisposed),t.validator?.routes.validateUpdateRouteBasicArgs(e,n),t.validator?.routes.throwIfInternalRoute(e,`updateRoute`);let{forwardTo:i,defaultParams:a,decodeParams:o,encodeParams:s,canActivate:c,canDeactivate:u}=n;t.validator?.routes.validateUpdateRoutePropertyTypes(e,n),t.isTransitioning()&&p.error(`router.updateRoute`,`Updating route "${e}" while navigation is in progress. This may cause unexpected behavior.`),t.validator?.routes.validateUpdateRoute(e,n,r),C(r,e,{forwardTo:i,defaultParams:a,decodeParams:o,encodeParams:s}),c!==void 0&&(c===null?r.lifecycleNamespace.clearCanActivate(e):r.lifecycleNamespace.addCanActivate(e,c,!0)),u!==void 0&&(u===null?r.lifecycleNamespace.clearCanDeactivate(e):r.lifecycleNamespace.addCanDeactivate(e,u,!0))},clear:()=>{l(t.isDisposed),h(t.isTransitioning())&&(r.treeOperations.resetStore(r),r.lifecycleNamespace.clearAll(),t.clearState())},has:e=>(t.validator?.routes.validateRouteName(e,`hasRoute`),r.matcher.hasRoute(e)),get:e=>(t.validator?.routes.validateRouteName(e,`getRoute`),w(r,e)),replace:i=>{l(t.isDisposed);let a=Array.isArray(i)?i:[i];if(!h(t.isTransitioning()))return;n(a,t.validator),t.validator?.routes.throwIfInternalRouteInArray(a,`replaceRoutes`),t.validator?.routes.validateAddRouteArgs(a),t.validator?.routes.validateRoutes(a,r);let o=e.getState();x(r,a,t,o?.path,o?.transition)}}}function E(e,t,n,r){if(n===void 0)return!1;if(!Object.hasOwn(e.dependencies,t))r?.dependencies.validateDependencyCount(e,`setDependency`);else{let i=e.dependencies[t];i!==n&&!(Number.isNaN(i)&&Number.isNaN(n))&&r?.dependencies.warnOverwrite(t,`setDependency`)}return e.dependencies[t]=n,!0}function D(e,t,n){let r=[];for(let i in t)t[i]!==void 0&&(Object.hasOwn(e.dependencies,i)?r.push(i):n?.dependencies.validateDependencyCount(e,`setDependencies`),e.dependencies[i]=t[i]);r.length>0&&n?.dependencies.warnBatchOverwrite(r,`setDependencies`)}function O(e){let t=s(e);return{get:e=>{t.validator?.dependencies.validateDependencyName(e,`getDependency`);let n=t.dependenciesGetStore(),r=n.dependencies[e];return t.validator?.dependencies.validateDependencyExists(e,n),r},getAll:()=>({...t.dependenciesGetStore().dependencies}),set:(e,n)=>{l(t.isDisposed),t.validator?.dependencies.validateSetDependencyArgs(e,n,`setDependency`),E(t.dependenciesGetStore(),e,n,t.validator)},setAll:e=>{l(t.isDisposed);let n=t.dependenciesGetStore();t.validator?.dependencies.validateDependenciesObject(e,`setDependencies`),t.validator?.dependencies.validateDependencyLimit(n,n.limits),D(n,e,t.validator)},remove:e=>{l(t.isDisposed),t.validator?.dependencies.validateDependencyName(e,`removeDependency`);let n=t.dependenciesGetStore();Object.hasOwn(n.dependencies,e)||t.validator?.dependencies.warnRemoveNonExistent(e),delete n.dependencies[e]},reset:()=>{l(t.isDisposed);let e=t.dependenciesGetStore();e.dependencies=Object.create(null)},has:e=>(t.validator?.dependencies.validateDependencyName(e,`hasDependency`),Object.hasOwn(t.dependenciesGetStore().dependencies,e))}}export{f as cloneRouter,O as getDependenciesApi,u as getLifecycleApi,d as getPluginApi,T as getRoutesApi};
|
|
2
2
|
//# sourceMappingURL=api.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{m as e,n as t,t as n,u as r}from"./Router-
|
|
2
|
-
//# sourceMappingURL=cloneRouter-
|
|
1
|
+
import{m as e,n as t,t as n,u as r}from"./Router-_BDnheMY.mjs";import{r as i}from"./internals-C59msvHY.mjs";function a(n){if(n())throw new t(e.ROUTER_DISPOSED)}const o=new WeakMap;function s(n){let r=o.get(n);if(r)return r;let s=i(n),c={makeState:(e,t,n,r)=>(s.validator?.state.validateMakeStateArgs(e,t,n),s.makeState(e,t,n,r?.params)),buildState:(e,t)=>{s.validator?.routes.validateStateBuilderArgs(e,t,`buildState`);let{name:n,params:r}=s.forwardState(e,t);return s.buildStateResolved(n,r)},forwardState:(e,t)=>(s.validator?.routes.validateStateBuilderArgs(e,t,`forwardState`),s.forwardState(e,t)),matchPath:e=>(s.validator?.routes.validateMatchPathArgs(e),s.matchPath(e,s.getOptions())),navigateToState:(e,t)=>(a(s.isDisposed),s.validator?.navigation.validateNavigateToStateArgs(e),t!==void 0&&s.validator?.navigation.validateNavigationOptions(t,`navigateToState`),s.navigateToState(e,t)),setRootPath:e=>{a(s.isDisposed),s.validator?.routes.validateSetRootPathArgs(e),s.setRootPath(e)},getRootPath:s.getRootPath,addEventListener:(e,t)=>(a(s.isDisposed),s.validator?.eventBus.validateListenerArgs(e,t),s.addEventListener(e,t)),buildNavigationState:(e,t={})=>{s.validator?.routes.validateStateBuilderArgs(e,t,`buildNavigationState`);let{name:n,params:r}=s.forwardState(e,t),i=s.buildStateResolved(n,r);if(i)return s.makeState(i.name,i.params,s.buildPath(i.name,i.params),i.meta)},getOptions:s.getOptions,getTree:s.getTree,addInterceptor:(e,t)=>{a(s.isDisposed),s.validator?.plugins.validateAddInterceptorArgs(e,t);let n=s.interceptors.get(e);return n||(n=[],s.interceptors.set(e,n)),n.push(t),()=>{let e=n.indexOf(t);e!==-1&&n.splice(e,1)}},getRouteConfig:e=>{let t=s.routeGetStore();if(t.matcher.hasRoute(e))return t.routeCustomFields[e]},extendRouter:r=>{a(s.isDisposed);let i=Object.keys(r);for(let r of i)if(r in n)throw new t(e.PLUGIN_CONFLICT,{message:`Cannot extend router: property "${r}" already exists`});for(let e of i)n[e]=r[e];let o={keys:i};s.routerExtensions.push(o);let c=!1;return()=>{if(c)return;c=!0;for(let e of o.keys)delete n[e];let e=s.routerExtensions.indexOf(o);e!==-1&&s.routerExtensions.splice(e,1)}},emitTransitionError:e=>{a(s.isDisposed),s.emitTransitionError(e)},claimContextNamespace:n=>{if(a(s.isDisposed),s.contextClaimRecords.has(n))throw new t(e.CONTEXT_NAMESPACE_ALREADY_CLAIMED,{message:`Cannot claim context namespace: "${n}" is already claimed by another plugin`});return s.contextClaimRecords.add(n),{write(e,t){e.context[n]=t},release(){s.contextClaimRecords.delete(n)}}}};return o.set(n,c),c}function c(e){let t=i(e),n=t.routeGetStore().lifecycleNamespace;return{addActivateGuard(e,r){a(t.isDisposed),t.validator?.routes.validateRouteName(e,`addActivateGuard`),t.validator?.lifecycle.validateHandler(r,`addActivateGuard`);let i=n.getHandlerCount(`activate`);t.validator?.lifecycle.validateHandlerLimit(i,t.dependenciesGetStore().limits,`canActivate`),n.addCanActivate(e,r)},addDeactivateGuard(e,r){a(t.isDisposed),t.validator?.routes.validateRouteName(e,`addDeactivateGuard`),t.validator?.lifecycle.validateHandler(r,`addDeactivateGuard`);let i=n.getHandlerCount(`deactivate`);t.validator?.lifecycle.validateHandlerLimit(i,t.dependenciesGetStore().limits,`canDeactivate`),n.addCanDeactivate(e,r)},removeActivateGuard(e){a(t.isDisposed),t.validator?.routes.validateRouteName(e,`removeActivateGuard`),n.clearCanActivate(e)},removeDeactivateGuard(e){a(t.isDisposed),t.validator?.routes.validateRouteName(e,`removeDeactivateGuard`),n.clearCanDeactivate(e)}}}function l(a,o){let s=i(a);if(s.isDisposed())throw new t(e.ROUTER_DISPOSED);s.validator?.dependencies.validateCloneArgs(o);let l=s.routeGetStore(),u=r(l.tree),d=l.config,f=l.resolvedForwardMap,p=l.routeCustomFields,m=s.cloneOptions(),h=s.cloneDependencies(),{definition:g,external:_}=l.lifecycleNamespace.getFactoriesByOrigin(),v=s.getPluginFactories(),y=new n(u,m,{...h,...o}),b=i(y).routeGetStore(),x=b.lifecycleNamespace,[S,C]=g,[w,T]=_;for(let[e,t]of Object.entries(S))x.addCanDeactivate(e,t,!0);for(let[e,t]of Object.entries(C))x.addCanActivate(e,t,!0);let E=c(y);for(let[e,t]of Object.entries(w))E.addDeactivateGuard(e,t);for(let[e,t]of Object.entries(T))E.addActivateGuard(e,t);return v.length>0&&y.usePlugin(...v),Object.assign(b.config.decoders,d.decoders),Object.assign(b.config.encoders,d.encoders),Object.assign(b.config.defaultParams,d.defaultParams),Object.assign(b.config.forwardMap,d.forwardMap),Object.assign(b.config.forwardFnMap,d.forwardFnMap),Object.assign(b.resolvedForwardMap,f),Object.assign(b.routeCustomFields,p),y}export{a as i,c as n,s as r,l as t};
|
|
2
|
+
//# sourceMappingURL=cloneRouter-DM59kihh.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cloneRouter-BBjARLMk.mjs","names":["routeTreeToDefinitions","RouterClass"],"sources":["../../src/api/helpers.ts","../../src/api/getPluginApi.ts","../../src/api/getLifecycleApi.ts","../../src/api/cloneRouter.ts"],"sourcesContent":["// packages/core/src/api/helpers.ts\n\nimport { errorCodes } from \"../constants\";\nimport { RouterError } from \"../RouterError\";\n\nexport function throwIfDisposed(isDisposed: () => boolean): void {\n if (isDisposed()) {\n throw new RouterError(errorCodes.ROUTER_DISPOSED);\n }\n}\n","import { throwIfDisposed } from \"./helpers\";\nimport { errorCodes } from \"../constants\";\nimport { getInternals } from \"../internals\";\nimport { RouterError } from \"../RouterError\";\n\nimport type { PluginApi } from \"./types\";\nimport type {\n ContextNamespaceClaim,\n DefaultDependencies,\n Params,\n Router,\n State,\n} from \"@real-router/types\";\n\n// Cache the assembled PluginApi per router — mirrors getNavigator() (#525):\n// avoids re-allocating the closure-bag on each call (plugins call this once\n// at init, but tests + nested plugins poll it), and gives spy/stub helpers\n// a stable object identity to attach to (e.g. spying on\n// `getPluginApi(router).navigateToState` to inject errors in popstate\n// recovery tests).\nconst cache = new WeakMap<object, PluginApi>();\n\nexport function getPluginApi<\n Dependencies extends DefaultDependencies = DefaultDependencies,\n>(router: Router<Dependencies>): PluginApi {\n const cached = cache.get(router);\n\n if (cached) {\n return cached;\n }\n\n const ctx = getInternals(router);\n const api: PluginApi = {\n makeState: (name, params, path, meta) => {\n ctx.validator?.state.validateMakeStateArgs(name, params, path);\n\n return ctx.makeState(\n name,\n params,\n path,\n meta?.params as\n | Record<string, Record<string, \"url\" | \"query\">>\n | undefined,\n );\n },\n buildState: (routeName, routeParams) => {\n ctx.validator?.routes.validateStateBuilderArgs(\n routeName,\n routeParams,\n \"buildState\",\n );\n\n const { name, params } = ctx.forwardState(routeName, routeParams);\n\n return ctx.buildStateResolved(name, params);\n },\n forwardState: <P extends Params = Params>(\n routeName: string,\n routeParams: P,\n ) => {\n ctx.validator?.routes.validateStateBuilderArgs(\n routeName,\n routeParams,\n \"forwardState\",\n );\n\n return ctx.forwardState(routeName, routeParams);\n },\n matchPath: (path) => {\n ctx.validator?.routes.validateMatchPathArgs(path);\n\n return ctx.matchPath(path, ctx.getOptions());\n },\n navigateToState: (state, options) => {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.navigation.validateNavigateToStateArgs(state);\n\n if (options !== undefined) {\n ctx.validator?.navigation.validateNavigationOptions(\n options,\n \"navigateToState\",\n );\n }\n\n return ctx.navigateToState(state, options);\n },\n setRootPath: (rootPath) => {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateSetRootPathArgs(rootPath);\n\n ctx.setRootPath(rootPath);\n },\n getRootPath: ctx.getRootPath,\n addEventListener: (eventName, cb) => {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.eventBus.validateListenerArgs(eventName, cb);\n\n return ctx.addEventListener(eventName, cb);\n },\n buildNavigationState: (name, params = {}) => {\n ctx.validator?.routes.validateStateBuilderArgs(\n name,\n params,\n \"buildNavigationState\",\n );\n\n const { name: resolvedName, params: resolvedParams } = ctx.forwardState(\n name,\n params,\n );\n const routeInfo = ctx.buildStateResolved(resolvedName, resolvedParams);\n\n if (!routeInfo) {\n return;\n }\n\n return ctx.makeState(\n routeInfo.name,\n routeInfo.params,\n ctx.buildPath(routeInfo.name, routeInfo.params),\n routeInfo.meta,\n );\n },\n getOptions: ctx.getOptions,\n getTree: ctx.getTree,\n addInterceptor: (method, fn) => {\n throwIfDisposed(ctx.isDisposed);\n ctx.validator?.plugins.validateAddInterceptorArgs(method, fn);\n let list = ctx.interceptors.get(method);\n\n if (!list) {\n list = [];\n ctx.interceptors.set(method, list);\n }\n\n list.push(fn);\n\n return () => {\n const index = list.indexOf(fn);\n\n if (index !== -1) {\n list.splice(index, 1);\n }\n };\n },\n getRouteConfig: (name) => {\n const store = ctx.routeGetStore();\n\n if (!store.matcher.hasRoute(name)) {\n return;\n }\n\n return store.routeCustomFields[name];\n },\n extendRouter: (extensions: Record<string, unknown>) => {\n throwIfDisposed(ctx.isDisposed);\n\n const keys = Object.keys(extensions);\n\n for (const key of keys) {\n if (key in router) {\n throw new RouterError(errorCodes.PLUGIN_CONFLICT, {\n message: `Cannot extend router: property \"${key}\" already exists`,\n });\n }\n }\n\n for (const key of keys) {\n (router as Record<string, unknown>)[key] = extensions[key];\n }\n\n const extensionRecord = { keys };\n\n ctx.routerExtensions.push(extensionRecord);\n\n let removed = false;\n\n return () => {\n if (removed) {\n return;\n }\n\n removed = true;\n\n for (const key of extensionRecord.keys) {\n delete (router as Record<string, unknown>)[key];\n }\n\n const idx = ctx.routerExtensions.indexOf(extensionRecord);\n\n if (idx !== -1) {\n ctx.routerExtensions.splice(idx, 1);\n }\n };\n },\n emitTransitionError: (error) => {\n throwIfDisposed(ctx.isDisposed);\n ctx.emitTransitionError(error);\n },\n claimContextNamespace: (namespace: string) => {\n throwIfDisposed(ctx.isDisposed);\n\n if (ctx.contextClaimRecords.has(namespace)) {\n throw new RouterError(errorCodes.CONTEXT_NAMESPACE_ALREADY_CLAIMED, {\n message: `Cannot claim context namespace: \"${namespace}\" is already claimed by another plugin`,\n });\n }\n\n ctx.contextClaimRecords.add(namespace);\n\n return {\n write(state: State, value: unknown) {\n state.context[namespace] = value;\n },\n release() {\n ctx.contextClaimRecords.delete(namespace);\n },\n } satisfies ContextNamespaceClaim;\n },\n };\n\n cache.set(router, api);\n\n return api;\n}\n","import { throwIfDisposed } from \"./helpers\";\nimport { getInternals } from \"../internals\";\n\nimport type { LifecycleApi } from \"./types\";\nimport type { DefaultDependencies, Router } from \"@real-router/types\";\n\nexport function getLifecycleApi<\n Dependencies extends DefaultDependencies = DefaultDependencies,\n>(router: Router<Dependencies>): LifecycleApi<Dependencies> {\n const ctx = getInternals(router);\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring\n const lifecycleNamespace = ctx.routeGetStore().lifecycleNamespace!;\n\n return {\n addActivateGuard(name, handler) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"addActivateGuard\");\n ctx.validator?.lifecycle.validateHandler(handler, \"addActivateGuard\");\n\n const activateCount = lifecycleNamespace.getHandlerCount(\"activate\");\n\n ctx.validator?.lifecycle.validateHandlerLimit(\n activateCount,\n ctx.dependenciesGetStore().limits,\n \"canActivate\",\n );\n\n lifecycleNamespace.addCanActivate(name, handler);\n },\n\n addDeactivateGuard(name, handler) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"addDeactivateGuard\");\n ctx.validator?.lifecycle.validateHandler(handler, \"addDeactivateGuard\");\n\n const deactivateCount = lifecycleNamespace.getHandlerCount(\"deactivate\");\n\n ctx.validator?.lifecycle.validateHandlerLimit(\n deactivateCount,\n ctx.dependenciesGetStore().limits,\n \"canDeactivate\",\n );\n\n lifecycleNamespace.addCanDeactivate(name, handler);\n },\n\n removeActivateGuard(name) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"removeActivateGuard\");\n\n lifecycleNamespace.clearCanActivate(name);\n },\n\n removeDeactivateGuard(name) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"removeDeactivateGuard\");\n\n lifecycleNamespace.clearCanDeactivate(name);\n },\n };\n}\n","import { routeTreeToDefinitions } from \"route-tree\";\n\nimport { errorCodes } from \"../constants\";\nimport { getInternals } from \"../internals\";\nimport { Router as RouterClass } from \"../Router\";\nimport { RouterError } from \"../RouterError\";\nimport { getLifecycleApi } from \"./getLifecycleApi\";\n\nimport type { Route } from \"../types\";\nimport type { DefaultDependencies, Router } from \"@real-router/types\";\n\n/**\n * Build an independent router instance that shares the route tree, options,\n * lifecycle guards, and plugin factories of `router`. The primary use case\n * is **SSR multi-tenancy** — one base router per process, one clone per\n * request.\n *\n * @param router - Source router (must not be disposed).\n * @param dependencies - Optional per-clone overrides merged on top of the\n * base router's dependencies. Always **fresh per call** in the documented\n * SSR pattern: pass per-request state here, never store it in the base.\n *\n * @remarks\n *\n * **Dependency merge — shallow by design.** `base.dependencies` are spread\n * into the clone via `{ ...sourceDeps, ...dependencies }`. Top-level keys\n * are new objects, but **values are shared by reference**: a `Map`, `Set`,\n * class instance, function, or nested plain object stored in\n * `base.dependencies` is the **same instance** in every clone. Mutations\n * in one clone are visible in the base and in every sibling clone.\n *\n * This is intentional. `structuredClone` of dep values is **not** applied\n * because it would:\n * - strip class prototypes (`new DbClient()` → plain object, methods lost)\n * - reject functions and symbols (`DataCloneError`)\n * - fragment singleton pools (one connection pool per request — pool\n * semantics destroyed)\n * - reject circular references\n *\n * **SSR rule of thumb.** Place values in `base.dependencies` according to\n * their lifecycle:\n *\n * - **Singletons / shared services** → `base.dependencies`. Examples: DB\n * client, connection pool, logger, config, feature-flag client. Process-\n * wide pooling depends on sharing these by reference.\n * - **Per-request state** → the `dependencies` override parameter (or\n * `createRequestScope`'s `deps` argument). Examples: `currentUser`,\n * `traceId`, `sessionId`, `abortSignal`. The override is applied last,\n * so it wins over base keys; pass a fresh object per call.\n *\n * Cross-request data leaks are **only possible** when per-request mutable\n * state is incorrectly placed in `base.dependencies`. The override slot is\n * the safe channel.\n *\n * @example\n * ```typescript\n * // Server boot — singletons only\n * const base = createRouter(routes, options, {\n * db: new DbClient(dbUrl),\n * logger,\n * });\n *\n * // Per request — fresh override per call\n * const clone = cloneRouter(base, {\n * currentUser,\n * traceId,\n * });\n * // clone.deps.db === base.deps.db ✓ shared pool (intentional)\n * // clone.deps.currentUser ✓ unique per request\n * ```\n *\n * @see createRequestScope — `@real-router/core/utils` SSR helper that\n * wraps this function and injects `abortSignal` automatically.\n */\nexport function cloneRouter<\n Dependencies extends DefaultDependencies = DefaultDependencies,\n>(\n router: Router<Dependencies>,\n dependencies?: Dependencies,\n): RouterClass<Dependencies> {\n const ctx = getInternals(router);\n\n if (ctx.isDisposed()) {\n throw new RouterError(errorCodes.ROUTER_DISPOSED);\n }\n\n ctx.validator?.dependencies.validateCloneArgs(dependencies);\n\n // Get source store directly\n const sourceStore = ctx.routeGetStore();\n const routes = routeTreeToDefinitions(sourceStore.tree);\n const routeConfig = sourceStore.config;\n const resolvedForwardMap = sourceStore.resolvedForwardMap;\n const routeCustomFields = sourceStore.routeCustomFields;\n\n const options = ctx.cloneOptions();\n const sourceDeps = ctx.cloneDependencies();\n const [canDeactivateFactories, canActivateFactories] =\n ctx.getLifecycleFactories();\n const pluginFactories = ctx.getPluginFactories();\n\n const mergedDeps = {\n ...sourceDeps,\n ...dependencies,\n } as Dependencies;\n\n const newRouter = new RouterClass<Dependencies>(\n routes as Route<Dependencies>[],\n options,\n mergedDeps,\n );\n\n const lifecycle = getLifecycleApi(newRouter);\n\n for (const [name, handler] of Object.entries(canDeactivateFactories)) {\n lifecycle.addDeactivateGuard(name, handler);\n }\n\n for (const [name, handler] of Object.entries(canActivateFactories)) {\n lifecycle.addActivateGuard(name, handler);\n }\n\n if (pluginFactories.length > 0) {\n newRouter.usePlugin(...pluginFactories);\n }\n\n const newCtx = getInternals(newRouter);\n const newStore = newCtx.routeGetStore();\n\n // Apply cloned config directly to new store\n Object.assign(newStore.config.decoders, routeConfig.decoders);\n Object.assign(newStore.config.encoders, routeConfig.encoders);\n Object.assign(newStore.config.defaultParams, routeConfig.defaultParams);\n Object.assign(newStore.config.forwardMap, routeConfig.forwardMap);\n Object.assign(newStore.config.forwardFnMap, routeConfig.forwardFnMap);\n Object.assign(newStore.resolvedForward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aAAa,EACtE,OAAO,OAAO,EAAS,OAAO,WAAY,EAAY,UAAU,EAChE,OAAO,OAAO,EAAS,OAAO,aAAc,EAAY,YAAY,EACpE,OAAO,OAAO,EAAS,mBAAoB,CAAkB,EAC7D,OAAO,OAAO,EAAS,kBAAmB,CAAiB,EAEpD,CACT"}
|
|
1
|
+
{"version":3,"file":"cloneRouter-DM59kihh.mjs","names":["routeTreeToDefinitions","RouterClass"],"sources":["../../src/api/helpers.ts","../../src/api/getPluginApi.ts","../../src/api/getLifecycleApi.ts","../../src/api/cloneRouter.ts"],"sourcesContent":["// packages/core/src/api/helpers.ts\n\nimport { errorCodes } from \"../constants\";\nimport { RouterError } from \"../RouterError\";\n\nexport function throwIfDisposed(isDisposed: () => boolean): void {\n if (isDisposed()) {\n throw new RouterError(errorCodes.ROUTER_DISPOSED);\n }\n}\n","import { throwIfDisposed } from \"./helpers\";\nimport { errorCodes } from \"../constants\";\nimport { getInternals } from \"../internals\";\nimport { RouterError } from \"../RouterError\";\n\nimport type { PluginApi } from \"./types\";\nimport type {\n ContextNamespaceClaim,\n DefaultDependencies,\n Params,\n Router,\n State,\n} from \"@real-router/types\";\n\n// Cache the assembled PluginApi per router — mirrors getNavigator() (#525):\n// avoids re-allocating the closure-bag on each call (plugins call this once\n// at init, but tests + nested plugins poll it), and gives spy/stub helpers\n// a stable object identity to attach to (e.g. spying on\n// `getPluginApi(router).navigateToState` to inject errors in popstate\n// recovery tests).\nconst cache = new WeakMap<object, PluginApi>();\n\nexport function getPluginApi<\n Dependencies extends DefaultDependencies = DefaultDependencies,\n>(router: Router<Dependencies>): PluginApi {\n const cached = cache.get(router);\n\n if (cached) {\n return cached;\n }\n\n const ctx = getInternals(router);\n const api: PluginApi = {\n makeState: (name, params, path, meta) => {\n ctx.validator?.state.validateMakeStateArgs(name, params, path);\n\n return ctx.makeState(\n name,\n params,\n path,\n meta?.params as\n | Record<string, Record<string, \"url\" | \"query\">>\n | undefined,\n );\n },\n buildState: (routeName, routeParams) => {\n ctx.validator?.routes.validateStateBuilderArgs(\n routeName,\n routeParams,\n \"buildState\",\n );\n\n const { name, params } = ctx.forwardState(routeName, routeParams);\n\n return ctx.buildStateResolved(name, params);\n },\n forwardState: <P extends Params = Params>(\n routeName: string,\n routeParams: P,\n ) => {\n ctx.validator?.routes.validateStateBuilderArgs(\n routeName,\n routeParams,\n \"forwardState\",\n );\n\n return ctx.forwardState(routeName, routeParams);\n },\n matchPath: (path) => {\n ctx.validator?.routes.validateMatchPathArgs(path);\n\n return ctx.matchPath(path, ctx.getOptions());\n },\n navigateToState: (state, options) => {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.navigation.validateNavigateToStateArgs(state);\n\n if (options !== undefined) {\n ctx.validator?.navigation.validateNavigationOptions(\n options,\n \"navigateToState\",\n );\n }\n\n return ctx.navigateToState(state, options);\n },\n setRootPath: (rootPath) => {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateSetRootPathArgs(rootPath);\n\n ctx.setRootPath(rootPath);\n },\n getRootPath: ctx.getRootPath,\n addEventListener: (eventName, cb) => {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.eventBus.validateListenerArgs(eventName, cb);\n\n return ctx.addEventListener(eventName, cb);\n },\n buildNavigationState: (name, params = {}) => {\n ctx.validator?.routes.validateStateBuilderArgs(\n name,\n params,\n \"buildNavigationState\",\n );\n\n const { name: resolvedName, params: resolvedParams } = ctx.forwardState(\n name,\n params,\n );\n const routeInfo = ctx.buildStateResolved(resolvedName, resolvedParams);\n\n if (!routeInfo) {\n return;\n }\n\n return ctx.makeState(\n routeInfo.name,\n routeInfo.params,\n ctx.buildPath(routeInfo.name, routeInfo.params),\n routeInfo.meta,\n );\n },\n getOptions: ctx.getOptions,\n getTree: ctx.getTree,\n addInterceptor: (method, fn) => {\n throwIfDisposed(ctx.isDisposed);\n ctx.validator?.plugins.validateAddInterceptorArgs(method, fn);\n let list = ctx.interceptors.get(method);\n\n if (!list) {\n list = [];\n ctx.interceptors.set(method, list);\n }\n\n list.push(fn);\n\n return () => {\n const index = list.indexOf(fn);\n\n if (index !== -1) {\n list.splice(index, 1);\n }\n };\n },\n getRouteConfig: (name) => {\n const store = ctx.routeGetStore();\n\n if (!store.matcher.hasRoute(name)) {\n return;\n }\n\n return store.routeCustomFields[name];\n },\n extendRouter: (extensions: Record<string, unknown>) => {\n throwIfDisposed(ctx.isDisposed);\n\n const keys = Object.keys(extensions);\n\n for (const key of keys) {\n if (key in router) {\n throw new RouterError(errorCodes.PLUGIN_CONFLICT, {\n message: `Cannot extend router: property \"${key}\" already exists`,\n });\n }\n }\n\n for (const key of keys) {\n (router as Record<string, unknown>)[key] = extensions[key];\n }\n\n const extensionRecord = { keys };\n\n ctx.routerExtensions.push(extensionRecord);\n\n let removed = false;\n\n return () => {\n if (removed) {\n return;\n }\n\n removed = true;\n\n for (const key of extensionRecord.keys) {\n delete (router as Record<string, unknown>)[key];\n }\n\n const idx = ctx.routerExtensions.indexOf(extensionRecord);\n\n if (idx !== -1) {\n ctx.routerExtensions.splice(idx, 1);\n }\n };\n },\n emitTransitionError: (error) => {\n throwIfDisposed(ctx.isDisposed);\n ctx.emitTransitionError(error);\n },\n claimContextNamespace: (namespace: string) => {\n throwIfDisposed(ctx.isDisposed);\n\n if (ctx.contextClaimRecords.has(namespace)) {\n throw new RouterError(errorCodes.CONTEXT_NAMESPACE_ALREADY_CLAIMED, {\n message: `Cannot claim context namespace: \"${namespace}\" is already claimed by another plugin`,\n });\n }\n\n ctx.contextClaimRecords.add(namespace);\n\n return {\n write(state: State, value: unknown) {\n state.context[namespace] = value;\n },\n release() {\n ctx.contextClaimRecords.delete(namespace);\n },\n } satisfies ContextNamespaceClaim;\n },\n };\n\n cache.set(router, api);\n\n return api;\n}\n","import { throwIfDisposed } from \"./helpers\";\nimport { getInternals } from \"../internals\";\n\nimport type { LifecycleApi } from \"./types\";\nimport type { DefaultDependencies, Router } from \"@real-router/types\";\n\nexport function getLifecycleApi<\n Dependencies extends DefaultDependencies = DefaultDependencies,\n>(router: Router<Dependencies>): LifecycleApi<Dependencies> {\n const ctx = getInternals(router);\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring\n const lifecycleNamespace = ctx.routeGetStore().lifecycleNamespace!;\n\n return {\n addActivateGuard(name, handler) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"addActivateGuard\");\n ctx.validator?.lifecycle.validateHandler(handler, \"addActivateGuard\");\n\n const activateCount = lifecycleNamespace.getHandlerCount(\"activate\");\n\n ctx.validator?.lifecycle.validateHandlerLimit(\n activateCount,\n ctx.dependenciesGetStore().limits,\n \"canActivate\",\n );\n\n lifecycleNamespace.addCanActivate(name, handler);\n },\n\n addDeactivateGuard(name, handler) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"addDeactivateGuard\");\n ctx.validator?.lifecycle.validateHandler(handler, \"addDeactivateGuard\");\n\n const deactivateCount = lifecycleNamespace.getHandlerCount(\"deactivate\");\n\n ctx.validator?.lifecycle.validateHandlerLimit(\n deactivateCount,\n ctx.dependenciesGetStore().limits,\n \"canDeactivate\",\n );\n\n lifecycleNamespace.addCanDeactivate(name, handler);\n },\n\n removeActivateGuard(name) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"removeActivateGuard\");\n\n lifecycleNamespace.clearCanActivate(name);\n },\n\n removeDeactivateGuard(name) {\n throwIfDisposed(ctx.isDisposed);\n\n ctx.validator?.routes.validateRouteName(name, \"removeDeactivateGuard\");\n\n lifecycleNamespace.clearCanDeactivate(name);\n },\n };\n}\n","import { routeTreeToDefinitions } from \"route-tree\";\n\nimport { errorCodes } from \"../constants\";\nimport { getInternals } from \"../internals\";\nimport { Router as RouterClass } from \"../Router\";\nimport { RouterError } from \"../RouterError\";\nimport { getLifecycleApi } from \"./getLifecycleApi\";\n\nimport type { Route } from \"../types\";\nimport type { DefaultDependencies, Router } from \"@real-router/types\";\n\n/**\n * Build an independent router instance that shares the route tree, options,\n * lifecycle guards, and plugin factories of `router`. The primary use case\n * is **SSR multi-tenancy** — one base router per process, one clone per\n * request.\n *\n * @param router - Source router (must not be disposed).\n * @param dependencies - Optional per-clone overrides merged on top of the\n * base router's dependencies. Always **fresh per call** in the documented\n * SSR pattern: pass per-request state here, never store it in the base.\n *\n * @remarks\n *\n * **Dependency merge — shallow by design.** `base.dependencies` are spread\n * into the clone via `{ ...sourceDeps, ...dependencies }`. Top-level keys\n * are new objects, but **values are shared by reference**: a `Map`, `Set`,\n * class instance, function, or nested plain object stored in\n * `base.dependencies` is the **same instance** in every clone. Mutations\n * in one clone are visible in the base and in every sibling clone.\n *\n * This is intentional. `structuredClone` of dep values is **not** applied\n * because it would:\n * - strip class prototypes (`new DbClient()` → plain object, methods lost)\n * - reject functions and symbols (`DataCloneError`)\n * - fragment singleton pools (one connection pool per request — pool\n * semantics destroyed)\n * - reject circular references\n *\n * **SSR rule of thumb.** Place values in `base.dependencies` according to\n * their lifecycle:\n *\n * - **Singletons / shared services** → `base.dependencies`. Examples: DB\n * client, connection pool, logger, config, feature-flag client. Process-\n * wide pooling depends on sharing these by reference.\n * - **Per-request state** → the `dependencies` override parameter (or\n * `createRequestScope`'s `deps` argument). Examples: `currentUser`,\n * `traceId`, `sessionId`, `abortSignal`. The override is applied last,\n * so it wins over base keys; pass a fresh object per call.\n *\n * Cross-request data leaks are **only possible** when per-request mutable\n * state is incorrectly placed in `base.dependencies`. The override slot is\n * the safe channel.\n *\n * @example\n * ```typescript\n * // Server boot — singletons only\n * const base = createRouter(routes, options, {\n * db: new DbClient(dbUrl),\n * logger,\n * });\n *\n * // Per request — fresh override per call\n * const clone = cloneRouter(base, {\n * currentUser,\n * traceId,\n * });\n * // clone.deps.db === base.deps.db ✓ shared pool (intentional)\n * // clone.deps.currentUser ✓ unique per request\n * ```\n *\n * @see createRequestScope — `@real-router/core/utils` SSR helper that\n * wraps this function and injects `abortSignal` automatically.\n */\nexport function cloneRouter<\n Dependencies extends DefaultDependencies = DefaultDependencies,\n>(\n router: Router<Dependencies>,\n dependencies?: Dependencies,\n): RouterClass<Dependencies> {\n const ctx = getInternals(router);\n\n if (ctx.isDisposed()) {\n throw new RouterError(errorCodes.ROUTER_DISPOSED);\n }\n\n ctx.validator?.dependencies.validateCloneArgs(dependencies);\n\n // Get source store directly\n const sourceStore = ctx.routeGetStore();\n const routes = routeTreeToDefinitions(sourceStore.tree);\n const routeConfig = sourceStore.config;\n const resolvedForwardMap = sourceStore.resolvedForwardMap;\n const routeCustomFields = sourceStore.routeCustomFields;\n\n const options = ctx.cloneOptions();\n const sourceDeps = ctx.cloneDependencies();\n // Origin-aware factory snapshot — definition guards are re-registered with\n // `isFromDefinition=true` on the clone so `replace()` can still strip them\n // via `clearDefinitionGuards()`. External guards take the public lifecycle\n // API path so they survive `replace()` symmetric with the base.\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring\n const sourceLifecycleNamespace = sourceStore.lifecycleNamespace!;\n const { definition: definitionFactories, external: externalFactories } =\n sourceLifecycleNamespace.getFactoriesByOrigin();\n const pluginFactories = ctx.getPluginFactories();\n\n const mergedDeps = {\n ...sourceDeps,\n ...dependencies,\n } as Dependencies;\n\n const newRouter = new RouterClass<Dependencies>(\n routes as Route<Dependencies>[],\n options,\n mergedDeps,\n );\n\n const newCtx = getInternals(newRouter);\n const newStore = newCtx.routeGetStore();\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring\n const newLifecycleNamespace = newStore.lifecycleNamespace!;\n\n const [definitionDeactivate, definitionActivate] = definitionFactories;\n const [externalDeactivate, externalActivate] = externalFactories;\n\n for (const [name, handler] of Object.entries(definitionDeactivate)) {\n newLifecycleNamespace.addCanDeactivate(name, handler, true);\n }\n\n for (const [name, handler] of Object.entries(definitionActivate)) {\n newLifecycleNamespace.addCanActivate(name, handler, true);\n }\n\n const lifecycle = getLifecycleApi(newRouter);\n\n for (const [name, handler] of Object.entries(externalDeactivate)) {\n lifecycle.addDeactivateGuard(name, handler);\n }\n\n for (const [name, handler] of Object.entries(externalActivate)) {\n lifecycle.addActivateGuard(name, handler);\n }\n\n if (pluginFactories.length > 0) {\n newRouter.usePlugin(...pluginFactories);\n }\n\n // Apply cloned config directly to new store\n Object.assign(newStore.config.decoders, routeConfig.decoders);\n Object.assign(newStore.config.encoders, routeConfig.encoders);\n Object.assign(newStore.config.defaultParams, routeConfig.defaultParams);\n Object.assign(newStore.config.forwardMap, routeConfig.forwardMap);\n Object.assign(newStore.config.forwardFnMap, routeConfig.forwardFnMap);\n Object.assign(newStore.resolvedForwardMap, resolvedForwardMap);\n Object.assign(newStore.routeCustomFields, routeCustomFields);\n\n return newRouter;\n}\n"],"mappings":"4GAKA,SAAgB,EAAgB,EAAiC,CAC/D,GAAI,EAAW,EACb,MAAM,IAAI,EAAY,EAAW,eAAe,CAEpD,CCWA,MAAM,EAAQ,IAAI,QAElB,SAAgB,EAEd,EAAyC,CACzC,IAAM,EAAS,EAAM,IAAI,CAAM,EAE/B,GAAI,EACF,OAAO,EAGT,IAAM,EAAM,EAAa,CAAM,EACzB,EAAiB,CACrB,WAAY,EAAM,EAAQ,EAAM,KAC9B,EAAI,WAAW,MAAM,sBAAsB,EAAM,EAAQ,CAAI,EAEtD,EAAI,UACT,EACA,EACA,EACA,GAAM,MAGR,GAEF,YAAa,EAAW,IAAgB,CACtC,EAAI,WAAW,OAAO,yBACpB,EACA,EACA,YACF,EAEA,GAAM,CAAE,OAAM,UAAW,EAAI,aAAa,EAAW,CAAW,EAEhE,OAAO,EAAI,mBAAmB,EAAM,CAAM,CAC5C,EACA,cACE,EACA,KAEA,EAAI,WAAW,OAAO,yBACpB,EACA,EACA,cACF,EAEO,EAAI,aAAa,EAAW,CAAW,GAEhD,UAAY,IACV,EAAI,WAAW,OAAO,sBAAsB,CAAI,EAEzC,EAAI,UAAU,EAAM,EAAI,WAAW,CAAC,GAE7C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,EAAa,CACN,EAAE,cAAc,EAEhC,EAAwB,EAAS,mBAEjC,CAAC,EAAsB,GAAsB,EAC7C,CAAC,EAAoB,GAAoB,EAE/C,IAAK,GAAM,CAAC,EAAM,KAAY,OAAO,QAAQ,CAAoB,EAC/D,EAAsB,iBAAiB,EAAM,EAAS,EAAI,EAG5D,IAAK,GAAM,CAAC,EAAM,KAAY,OAAO,QAAQ,CAAkB,EAC7D,EAAsB,eAAe,EAAM,EAAS,EAAI,EAG1D,IAAM,EAAY,EAAgB,CAAS,EAE3C,IAAK,GAAM,CAAC,EAAM,KAAY,OAAO,QAAQ,CAAkB,EAC7D,EAAU,mBAAmB,EAAM,CAAO,EAG5C,IAAK,GAAM,CAAC,EAAM,KAAY,OAAO,QAAQ,CAAgB,EAC3D,EAAU,iBAAiB,EAAM,CAAO,EAgB1C,OAbI,EAAgB,OAAS,GAC3B,EAAU,UAAU,GAAG,CAAe,EAIxC,OAAO,OAAO,EAAS,OAAO,SAAU,EAAY,QAAQ,EAC5D,OAAO,OAAO,EAAS,OAAO,SAAU,EAAY,QAAQ,EAC5D,OAAO,OAAO,EAAS,OAAO,cAAe,EAAY,aAAa,EACtE,OAAO,OAAO,EAAS,OAAO,WAAY,EAAY,UAAU,EAChE,OAAO,OAAO,EAAS,OAAO,aAAc,EAAY,YAAY,EACpE,OAAO,OAAO,EAAS,mBAAoB,CAAkB,EAC7D,OAAO,OAAO,EAAS,kBAAmB,CAAiB,EAEpD,CACT"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{f as e,h as t,m as n,n as r,o as i,p as a,t as o}from"./Router-
|
|
1
|
+
import{f as e,h as t,m as n,n as r,o as i,p as a,t as o}from"./Router-_BDnheMY.mjs";const s=(e=[],t={},n={})=>new o(e,t,n),c=new WeakMap,l=e=>{let t=c.get(e);return t||(t=Object.freeze({navigate:e.navigate,getState:e.getState,isActiveRoute:e.isActiveRoute,canNavigateTo:e.canNavigateTo,subscribe:e.subscribe,subscribeLeave:e.subscribeLeave,isLeaveApproved:e.isLeaveApproved}),c.set(e,t)),t};export{o as Router,r as RouterError,e as UNKNOWN_ROUTE,a as constants,s as createRouter,n as errorCodes,t as events,l as getNavigator,i as resolveForwardChain};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/utils.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{r as e}from"./internals-C59msvHY.mjs";import{r as t,t as n}from"./cloneRouter-
|
|
1
|
+
import{r as e}from"./internals-C59msvHY.mjs";import{r as t,t as n}from"./cloneRouter-DM59kihh.mjs";function r(e){return`signal`in e&&typeof e.signal==`object`&&e.signal!==void 0&&typeof e.signal.aborted==`boolean`}function i(e,t,i){let a,o;if(r(e))o=e.signal;else{let t=new AbortController,n=()=>{t.abort()};e.on(`close`,n),o=t.signal,a=()=>{e.removeListener?.(`close`,n)}}let s=n(t,{...i,abortSignal:o}),c=!1,l=()=>c?Promise.resolve():(c=!0,a?.(),s.dispose(),Promise.resolve());return{router:s,signal:o,dispose:l,[Symbol.asyncDispose]:l}}function a(e){let t=[];for(let n of e.children.values())n.children.size===0?t.push(n.fullName):t.push(...a(n));return t}async function o(e,n){let r=a(t(e).getTree()),i=[];for(let t of r){let r=n?.[t];if(r){let n=await r();for(let r of n)i.push(e.buildPath(t,r))}else i.push(e.buildPath(t,{}))}return i}async function s(t,n,r){let i=r?.deserialize??JSON.parse,a=typeof n==`string`?i(n):n,o=e(t),s=o.hydrationState;o.hydrationState=a;try{return await t.start(a.path)}finally{o.hydrationState=s}}function c(e,t){return((t?.serialize??JSON.stringify)(e)??`null`).replaceAll(`<`,String.raw`\u003c`).replaceAll(`>`,String.raw`\u003e`).replaceAll(`&`,String.raw`\u0026`)}function l(e,t){let n=t?.excludeContext,r=e.context;if(n?.length){let t={},i=e.context;for(let e of Object.keys(i))n.includes(e)||(t[e]=i[e]);r=t}let i={name:e.name,params:e.params,path:e.path,context:r};return t?.serialize?c(i,{serialize:t.serialize}):c(i)}export{i as createRequestScope,o as getStaticPaths,s as hydrateRouter,l as serializeRouterState,c as serializeState};
|
|
2
2
|
//# sourceMappingURL=utils.mjs.map
|
|
@@ -17,6 +17,22 @@ interface RouteLifecycleDependencies<Dependencies extends DefaultDependencies =
|
|
|
17
17
|
}
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.d.ts
|
|
20
|
+
/**
|
|
21
|
+
* Origin of a registered guard. `"definition"` — declared in route config and
|
|
22
|
+
* thus subject to `clearDefinitionGuards()` cleanup during `replace()`.
|
|
23
|
+
* `"external"` — registered post-hoc via `getLifecycleApi().add*Guard(...)`
|
|
24
|
+
* and survives `replace()`.
|
|
25
|
+
*
|
|
26
|
+
* Storage is split per origin (two Maps per kind, four Maps total) so origin
|
|
27
|
+
* is a primary invariant rather than a derived Set-tracked property. The
|
|
28
|
+
* compiled function (`#canActivateFunctions` / `#canDeactivateFunctions`)
|
|
29
|
+
* reflects the **last add wins** semantic for the slot (regardless of
|
|
30
|
+
* origin) — matching the pre-refactor behaviour and the
|
|
31
|
+
* `replaceRoutes.test.ts` "definition wins on replace" contract. On clear,
|
|
32
|
+
* the function falls back to whichever origin Map still holds an entry
|
|
33
|
+
* (external preferred if both remain after a partial clear).
|
|
34
|
+
*/
|
|
35
|
+
type GuardOrigin = "definition" | "external";
|
|
20
36
|
/**
|
|
21
37
|
* Independent namespace for managing route lifecycle handlers.
|
|
22
38
|
*
|
|
@@ -36,36 +52,41 @@ declare class RouteLifecycleNamespace<Dependencies extends DefaultDependencies =
|
|
|
36
52
|
getHandlerCount(type: "activate" | "deactivate"): number;
|
|
37
53
|
/**
|
|
38
54
|
* Adds a canActivate guard for a route.
|
|
39
|
-
* Handles overwrite detection and registration.
|
|
40
55
|
*
|
|
41
56
|
* @param name - Route name (input-validated by facade)
|
|
42
57
|
* @param handler - Guard function or boolean (input-validated by facade)
|
|
43
|
-
* @param isFromDefinition - True when guard comes from route definition
|
|
58
|
+
* @param isFromDefinition - True when guard comes from route definition
|
|
59
|
+
* (lands in the definition Map; subject to `clearDefinitionGuards()`).
|
|
60
|
+
* False (default) when added via `getLifecycleApi().addActivateGuard(...)`
|
|
61
|
+
* (lands in the external Map; survives `replace()`).
|
|
62
|
+
*
|
|
63
|
+
* Last add wins at runtime: the compiled function reflects the factory
|
|
64
|
+
* passed to the most recent call, regardless of origin. Origin only
|
|
65
|
+
* determines which Map the factory is filed under (relevant for
|
|
66
|
+
* `clearDefinitionGuards()` and `cloneRouter` re-registration).
|
|
44
67
|
*/
|
|
45
68
|
addCanActivate(name: string, handler: GuardFnFactory<Dependencies> | boolean, isFromDefinition?: boolean): void;
|
|
46
69
|
/**
|
|
47
70
|
* Adds a canDeactivate guard for a route.
|
|
48
|
-
* Handles overwrite detection and registration.
|
|
49
71
|
*
|
|
50
|
-
*
|
|
51
|
-
* @param handler - Guard function or boolean (input-validated by facade)
|
|
52
|
-
* @param isFromDefinition - True when guard comes from route definition (tracked for HMR replace)
|
|
72
|
+
* Symmetric counterpart to {@link addCanActivate}.
|
|
53
73
|
*/
|
|
54
74
|
addCanDeactivate(name: string, handler: GuardFnFactory<Dependencies> | boolean, isFromDefinition?: boolean): void;
|
|
55
75
|
/**
|
|
56
76
|
* Removes a canActivate guard for a route.
|
|
57
|
-
* Input already validated by facade (not registering).
|
|
58
77
|
*
|
|
59
78
|
* @param name - Route name (already validated by facade)
|
|
79
|
+
* @param origin - Optional origin filter. `"definition"` clears only the
|
|
80
|
+
* definition slot; `"external"` clears only the external slot; omitted
|
|
81
|
+
* (default) clears both — backward compatible with pre-refactor semantics.
|
|
60
82
|
*/
|
|
61
|
-
clearCanActivate(name: string): void;
|
|
83
|
+
clearCanActivate(name: string, origin?: GuardOrigin): void;
|
|
62
84
|
/**
|
|
63
85
|
* Removes a canDeactivate guard for a route.
|
|
64
|
-
* Input already validated by facade (not registering).
|
|
65
86
|
*
|
|
66
|
-
*
|
|
87
|
+
* Symmetric counterpart to {@link clearCanActivate}.
|
|
67
88
|
*/
|
|
68
|
-
clearCanDeactivate(name: string): void;
|
|
89
|
+
clearCanDeactivate(name: string, origin?: GuardOrigin): void;
|
|
69
90
|
/**
|
|
70
91
|
* Clears all lifecycle handlers (canActivate and canDeactivate).
|
|
71
92
|
* Used by clearRoutes to reset all lifecycle state.
|
|
@@ -73,15 +94,35 @@ declare class RouteLifecycleNamespace<Dependencies extends DefaultDependencies =
|
|
|
73
94
|
clearAll(): void;
|
|
74
95
|
/**
|
|
75
96
|
* Clears only lifecycle handlers that were registered from route definitions.
|
|
76
|
-
* Used by HMR to remove definition-sourced guards without
|
|
97
|
+
* Used by HMR `replace()` to remove definition-sourced guards without
|
|
98
|
+
* touching externally-added guards.
|
|
99
|
+
*
|
|
100
|
+
* For slots where both definition and external exist, the external factory
|
|
101
|
+
* stays and the compiled function is unchanged (external already won at
|
|
102
|
+
* registration time). For definition-only slots, the compiled function is
|
|
103
|
+
* dropped.
|
|
77
104
|
*/
|
|
78
105
|
clearDefinitionGuards(): void;
|
|
79
106
|
/**
|
|
80
|
-
* Returns lifecycle factories as
|
|
107
|
+
* Returns lifecycle factories as a flat `[deactivate, activate]` tuple of
|
|
108
|
+
* `Record<name, factory>` — the effective view where external wins over
|
|
109
|
+
* definition for the same slot. Used by `getRoutesApi` to enrich route
|
|
110
|
+
* objects with their current canActivate / canDeactivate factories and by
|
|
111
|
+
* the route-removal cleanup path.
|
|
81
112
|
*
|
|
82
|
-
*
|
|
113
|
+
* For cloneRouter (which needs to preserve origin on re-registration), use
|
|
114
|
+
* {@link getFactoriesByOrigin} instead.
|
|
83
115
|
*/
|
|
84
116
|
getFactories(): [Record<string, GuardFnFactory<Dependencies>>, Record<string, GuardFnFactory<Dependencies>>];
|
|
117
|
+
/**
|
|
118
|
+
* Returns factories tagged by origin — definition and external as separate
|
|
119
|
+
* `[deactivate, activate]` tuples. Used by `cloneRouter` to re-register
|
|
120
|
+
* guards on the clone with their original origin flag preserved.
|
|
121
|
+
*/
|
|
122
|
+
getFactoriesByOrigin(): {
|
|
123
|
+
definition: [Record<string, GuardFnFactory<Dependencies>>, Record<string, GuardFnFactory<Dependencies>>];
|
|
124
|
+
external: [Record<string, GuardFnFactory<Dependencies>>, Record<string, GuardFnFactory<Dependencies>>];
|
|
125
|
+
};
|
|
85
126
|
/**
|
|
86
127
|
* Returns compiled lifecycle functions for transition execution.
|
|
87
128
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.mts","names":[],"sources":["../../src/namespaces/DependenciesNamespace/dependenciesStore.ts","../../src/namespaces/RouteLifecycleNamespace/types.ts","../../src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.ts","../../src/namespaces/RoutesNamespace/types.ts","../../src/namespaces/RoutesNamespace/routesStore.ts","../../src/internals.ts"],"mappings":";;;;;;;;UAKiB,iBAAA,sBACM,mBAAA,GAAsB,mBAAA;EAE3C,YAAA,EAAc,OAAA,CAAQ,YAAA;EACtB,MAAA,EAAQ,MAAA;AAAA;;;UCJO,0BAAA,sBACM,mBAAA,GAAsB,mBAAA;EAE3C,cAAA,GAAiB,OAAA,EAAS,cAAA,CAAe,YAAA,MAAkB,OAAA;AAAA;;;;;;;ADH7D
|
|
1
|
+
{"version":3,"file":"validation.d.mts","names":[],"sources":["../../src/namespaces/DependenciesNamespace/dependenciesStore.ts","../../src/namespaces/RouteLifecycleNamespace/types.ts","../../src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.ts","../../src/namespaces/RoutesNamespace/types.ts","../../src/namespaces/RoutesNamespace/routesStore.ts","../../src/internals.ts"],"mappings":";;;;;;;;UAKiB,iBAAA,sBACM,mBAAA,GAAsB,mBAAA;EAE3C,YAAA,EAAc,OAAA,CAAQ,YAAA;EACtB,MAAA,EAAQ,MAAA;AAAA;;;UCJO,0BAAA,sBACM,mBAAA,GAAsB,mBAAA;EAE3C,cAAA,GAAiB,OAAA,EAAS,cAAA,CAAe,YAAA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AA;EAAA,SAI7B,WAAA,GAAc,QAAA;EAAA,SACd,WAAA;EAAA,SAEA,OAAA,QAAe,SAAA;EAAA,SAEf,UAAA;EAET,SAAA,EAAW,eAAA;EAAA,SAGF,oBAAA,QAA4B,iBAAA,CAAkB,CAAA;EAAA,SAG9C,YAAA,QAAoB,OAAA;EAAA,SACpB,iBAAA,QAAyB,MAAA;EAAA,SACzB,qBAAA,SACP,MAAA,SAAe,cAAA,CAAe,CAAA,IAC9B,MAAA,SAAe,cAAA,CAAe,CAAA;EAAA,SAEvB,kBAAA,QAA0B,aAAA,CAAc,CAAA;EAAA,SAGxC,aAAA,QAAqB,WAAA,CAAY,CAAA;EAAA,SAGjC,YAAA;EAAA,SACA,eAAA;EAAA,SACA,UAAA;EAAA,SACA,QAAA,GAAW,KAAA,EAAO,KAAA;EAAA,SAClB,gBAAA;IAAoB,IAAA;EAAA;EAAA,SACpB,mBAAA,EAAqB,GAAA;EJnG6B;;AAAO;;;;AC4BpE;;;EGkFE,cAAA,EAAgB,qBAAA;AAAA;AAAA,iBAMF,YAAA,WAAuB,mBAAA,CAAA,CACrC,MAAA,EAAQ,MAAA,CAAgB,CAAA,IACvB,eAAA,CAAgB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/core",
|
|
3
|
-
"version": "0.54.
|
|
3
|
+
"version": "0.54.5",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "A simple, powerful, view-agnostic, modular and extensible router",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -87,9 +87,9 @@
|
|
|
87
87
|
"homepage": "https://github.com/greydragon888/real-router",
|
|
88
88
|
"sideEffects": false,
|
|
89
89
|
"dependencies": {
|
|
90
|
-
"@real-router/fsm": "^0.4.0",
|
|
91
90
|
"@real-router/logger": "^0.3.0",
|
|
92
|
-
"@real-router/types": "^0.35.0"
|
|
91
|
+
"@real-router/types": "^0.35.0",
|
|
92
|
+
"@real-router/fsm": "^0.4.0"
|
|
93
93
|
},
|
|
94
94
|
"devDependencies": {
|
|
95
95
|
"event-emitter": "^0.1.2",
|
package/src/api/cloneRouter.ts
CHANGED
|
@@ -95,8 +95,14 @@ export function cloneRouter<
|
|
|
95
95
|
|
|
96
96
|
const options = ctx.cloneOptions();
|
|
97
97
|
const sourceDeps = ctx.cloneDependencies();
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
// Origin-aware factory snapshot — definition guards are re-registered with
|
|
99
|
+
// `isFromDefinition=true` on the clone so `replace()` can still strip them
|
|
100
|
+
// via `clearDefinitionGuards()`. External guards take the public lifecycle
|
|
101
|
+
// API path so they survive `replace()` symmetric with the base.
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring
|
|
103
|
+
const sourceLifecycleNamespace = sourceStore.lifecycleNamespace!;
|
|
104
|
+
const { definition: definitionFactories, external: externalFactories } =
|
|
105
|
+
sourceLifecycleNamespace.getFactoriesByOrigin();
|
|
100
106
|
const pluginFactories = ctx.getPluginFactories();
|
|
101
107
|
|
|
102
108
|
const mergedDeps = {
|
|
@@ -110,13 +116,29 @@ export function cloneRouter<
|
|
|
110
116
|
mergedDeps,
|
|
111
117
|
);
|
|
112
118
|
|
|
119
|
+
const newCtx = getInternals(newRouter);
|
|
120
|
+
const newStore = newCtx.routeGetStore();
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed set after wiring
|
|
122
|
+
const newLifecycleNamespace = newStore.lifecycleNamespace!;
|
|
123
|
+
|
|
124
|
+
const [definitionDeactivate, definitionActivate] = definitionFactories;
|
|
125
|
+
const [externalDeactivate, externalActivate] = externalFactories;
|
|
126
|
+
|
|
127
|
+
for (const [name, handler] of Object.entries(definitionDeactivate)) {
|
|
128
|
+
newLifecycleNamespace.addCanDeactivate(name, handler, true);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const [name, handler] of Object.entries(definitionActivate)) {
|
|
132
|
+
newLifecycleNamespace.addCanActivate(name, handler, true);
|
|
133
|
+
}
|
|
134
|
+
|
|
113
135
|
const lifecycle = getLifecycleApi(newRouter);
|
|
114
136
|
|
|
115
|
-
for (const [name, handler] of Object.entries(
|
|
137
|
+
for (const [name, handler] of Object.entries(externalDeactivate)) {
|
|
116
138
|
lifecycle.addDeactivateGuard(name, handler);
|
|
117
139
|
}
|
|
118
140
|
|
|
119
|
-
for (const [name, handler] of Object.entries(
|
|
141
|
+
for (const [name, handler] of Object.entries(externalActivate)) {
|
|
120
142
|
lifecycle.addActivateGuard(name, handler);
|
|
121
143
|
}
|
|
122
144
|
|
|
@@ -124,9 +146,6 @@ export function cloneRouter<
|
|
|
124
146
|
newRouter.usePlugin(...pluginFactories);
|
|
125
147
|
}
|
|
126
148
|
|
|
127
|
-
const newCtx = getInternals(newRouter);
|
|
128
|
-
const newStore = newCtx.routeGetStore();
|
|
129
|
-
|
|
130
149
|
// Apply cloned config directly to new store
|
|
131
150
|
Object.assign(newStore.config.decoders, routeConfig.decoders);
|
|
132
151
|
Object.assign(newStore.config.encoders, routeConfig.encoders);
|
|
@@ -28,19 +28,48 @@ function ensureError(value: unknown): Error {
|
|
|
28
28
|
function settleLeavePromises(
|
|
29
29
|
promises: Promise<void>[],
|
|
30
30
|
firstSyncError: unknown,
|
|
31
|
+
signal: AbortSignal,
|
|
31
32
|
): Promise<void> {
|
|
32
|
-
return Promise
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
33
|
+
return new Promise<void>((resolve, reject) => {
|
|
34
|
+
const onAbort = (): void => {
|
|
35
|
+
reject(ensureError(signal.reason));
|
|
36
|
+
};
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
(
|
|
39
|
-
);
|
|
38
|
+
if (signal.aborted) {
|
|
39
|
+
onAbort();
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
throw ensureError(rejected.reason);
|
|
41
|
+
return;
|
|
43
42
|
}
|
|
43
|
+
|
|
44
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
45
|
+
|
|
46
|
+
void Promise.allSettled(promises).then((results) => {
|
|
47
|
+
signal.removeEventListener("abort", onAbort);
|
|
48
|
+
|
|
49
|
+
if (signal.aborted) {
|
|
50
|
+
// Race lost to abort — the abort handler already rejected; do nothing
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (firstSyncError !== undefined) {
|
|
55
|
+
reject(ensureError(firstSyncError));
|
|
56
|
+
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const rejected = results.find(
|
|
61
|
+
(result): result is PromiseRejectedResult =>
|
|
62
|
+
result.status === "rejected",
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (rejected !== undefined) {
|
|
66
|
+
reject(ensureError(rejected.reason));
|
|
67
|
+
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
resolve();
|
|
72
|
+
});
|
|
44
73
|
});
|
|
45
74
|
}
|
|
46
75
|
|
|
@@ -311,7 +340,7 @@ export class EventBusNamespace {
|
|
|
311
340
|
return undefined;
|
|
312
341
|
}
|
|
313
342
|
|
|
314
|
-
return settleLeavePromises(promises, firstSyncError);
|
|
343
|
+
return settleLeavePromises(promises, firstSyncError, signal);
|
|
315
344
|
}
|
|
316
345
|
|
|
317
346
|
clearAll(): void {
|
|
@@ -202,6 +202,9 @@ async function finishAfterAsyncLeave(
|
|
|
202
202
|
): Promise<void> {
|
|
203
203
|
await leaveCompletion;
|
|
204
204
|
|
|
205
|
+
/* v8 ignore next 3 -- @preserve: unreachable after #663 — signal abort
|
|
206
|
+
mid-leave rejects via settleLeavePromises, so `await leaveCompletion`
|
|
207
|
+
throws directly instead of completing with a stale isActive() */
|
|
205
208
|
if (!isActive()) {
|
|
206
209
|
throw new RouterError(errorCodes.TRANSITION_CANCELLED);
|
|
207
210
|
}
|