@real-router/sources 0.2.1 → 0.2.3
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/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/metafile-cjs.json +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/metafile-esm.json +1 -1
- package/package.json +4 -2
- package/src/BaseSource.ts +30 -1
- package/src/createActiveRouteSource.ts +39 -63
- package/src/createRouteNodeSource.ts +53 -98
- package/src/createRouteSource.ts +31 -61
- package/src/createTransitionSource.ts +34 -52
package/dist/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var e=require("@real-router/route-utils"),t=require("@real-router/core"),s=require("@real-router/core/api"),r=class{#e;#t=!1;#s=new Set;#r;#o;#n;constructor(e,t){this.#e=e,this.#r=t?.onFirstSubscribe,this.#o=t?.onLastUnsubscribe,this.#n=t?.onDestroy,this.subscribe=this.subscribe.bind(this),this.getSnapshot=this.getSnapshot.bind(this),this.destroy=this.destroy.bind(this)}subscribe(e){return this.#t?()=>{}:(0===this.#s.size&&this.#r&&this.#r(),this.#s.add(e),()=>{this.#s.delete(e),!this.#t&&0===this.#s.size&&this.#o&&this.#o()})}getSnapshot(){return this.#e}updateSnapshot(e){this.#t||(this.#e=e,this.#s.forEach(e=>{e()}))}destroy(){this.#t||(this.#t=!0,this.#n?.(),this.#s.clear())}};function o(e,t,s,r){const o=r?.route??t.getState(),n=r?.previousRoute,i=""===s||void 0!==o&&(o.name===s||o.name.startsWith(`${s}.`))?o:void 0;return i===e.route&&n===e.previousRoute?e:{route:i,previousRoute:n}}var n=new WeakMap,i={isTransitioning:!1,toRoute:null,fromRoute:null};exports.createActiveRouteSource=function(t,s,o,n){const i=n?.strict??!1,u=n?.ignoreQueryParams??!0,a=t.isActiveRoute(s,o,i,u),c=new r(a,{onDestroy:()=>{h()}}),h=t.subscribe(r=>{const n=e.areRoutesRelated(s,r.route.name),a=r.previousRoute&&e.areRoutesRelated(s,r.previousRoute.name);if(!n&&!a)return;const h=!!n&&t.isActiveRoute(s,o,i,u);Object.is(c.getSnapshot(),h)||c.updateSnapshot(h)});return c},exports.createRouteNodeSource=function(e,t){let s=null;const i=function(e,t){let s=n.get(e);s||(s=new Map,n.set(e,s));let r=s.get(t);return r||(r=e.shouldUpdateNode(t),s.set(t,r)),r}(e,t),u=()=>{const e=s;s=null,e?.()},a=new r(o({route:void 0,previousRoute:void 0},e,t),{onFirstSubscribe:()=>{a.updateSnapshot(o(a.getSnapshot(),e,t)),s=e.subscribe(s=>{if(!i(s.route,s.previousRoute))return;const r=o(a.getSnapshot(),e,t,s);Object.is(a.getSnapshot(),r)||a.updateSnapshot(r)})},onLastUnsubscribe:u,onDestroy:u});return a},exports.createRouteSource=function(e){let t=null;const s=()=>{const e=t;t=null,e?.()},o=new r({route:e.getState(),previousRoute:void 0},{onFirstSubscribe:()=>{t=e.subscribe(e=>{o.updateSnapshot({route:e.route,previousRoute:e.previousRoute})})},onLastUnsubscribe:s,onDestroy:s});return o},exports.createTransitionSource=function(e){const o=new r(i,{onDestroy:()=>{a.forEach(e=>{e()})}}),n=s.getPluginApi(e),u=()=>{o.updateSnapshot(i)},a=[n.addEventListener(t.events.TRANSITION_START,(e,t)=>{o.updateSnapshot({isTransitioning:!0,toRoute:e,fromRoute:t??null})}),n.addEventListener(t.events.TRANSITION_SUCCESS,u),n.addEventListener(t.events.TRANSITION_ERROR,u),n.addEventListener(t.events.TRANSITION_CANCEL,u)];return o};//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/createRouteSource.ts","../../src/computeSnapshot.ts","../../src/shouldUpdateCache.ts","../../src/createRouteNodeSource.ts","../../src/BaseSource.ts","../../src/createActiveRouteSource.ts","../../src/createTransitionSource.ts"],"names":["areRout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AO,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,QAAQ,CAAA;AAAA,EACxC;AAAA,EAEA,WAAA,GAAwC;AACtC,IAAA,OAAO,IAAA,CAAK,QAAQ,WAAA,EAAY;AAAA,EAClC;AAAA,EAEA,OAAA,GAAgB;AACd,IAAA,IAAA,CAAK,OAAA,CAAQ,OAAA,CAAQ,CAAC,CAAA,KAAM;AAC1B,MAAA,CAAA,EAAE;AAAA,IACJ,CAAC,CAAA;AACD,IAAA,IAAA,CAAK,QAAQ,OAAA,EAAQ;AAAA,EACvB;AACF,CAAA;AAEO,SAAS,uBACd,MAAA,EACwC;AACxC,EAAA,OAAO,IAAI,iBAAiB,MAAM,CAAA;AACpC","file":"index.js","sourcesContent":["import type { RouteSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\nclass RouteSource implements RouterSource<RouteSnapshot> {\n #routerUnsubscribe: (() => void) | null = null;\n #currentSnapshot: RouteSnapshot;\n\n readonly #listeners = new Set<() => void>();\n readonly #router: Router;\n\n constructor(router: Router) {\n this.#router = router;\n\n this.#currentSnapshot = {\n route: router.getState(),\n previousRoute: undefined,\n };\n\n this.subscribe = this.subscribe.bind(this);\n this.destroy = this.destroy.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#listeners.size === 0) {\n // Connect to router on first subscription\n this.#routerUnsubscribe = this.#router.subscribe((next) => {\n this.#currentSnapshot = {\n route: next.route,\n previousRoute: next.previousRoute,\n };\n this.#listeners.forEach((cb) => {\n cb();\n });\n });\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n\n if (this.#listeners.size === 0 && this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n };\n }\n\n getSnapshot(): RouteSnapshot {\n return this.#currentSnapshot;\n }\n\n destroy(): void {\n if (this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n\n this.#listeners.clear();\n }\n}\n\n/**\n * Creates a source for the full route state.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteSource(router: Router): RouterSource<RouteSnapshot> {\n return new RouteSource(router);\n}\n","import type { RouteNodeSnapshot } from \"./types.js\";\nimport type { Router, SubscribeState } from \"@real-router/core\";\n\nexport function computeSnapshot(\n currentSnapshot: RouteNodeSnapshot,\n router: Router,\n nodeName: string,\n next?: SubscribeState,\n): RouteNodeSnapshot {\n const currentRoute = next?.route ?? router.getState();\n const previousRoute = next?.previousRoute;\n\n const isNodeActive =\n nodeName === \"\" ||\n (currentRoute !== undefined &&\n (currentRoute.name === nodeName ||\n currentRoute.name.startsWith(`${nodeName}.`)));\n\n const route = isNodeActive ? currentRoute : undefined;\n\n if (\n route === currentSnapshot.route &&\n previousRoute === currentSnapshot.previousRoute\n ) {\n return currentSnapshot;\n }\n\n return { route, previousRoute };\n}\n","import type { Router, State } from \"@real-router/core\";\n\nconst shouldUpdateCache = new WeakMap<\n Router,\n Map<string, (toState: State, fromState?: State) => boolean>\n>();\n\nexport function getCachedShouldUpdate(\n router: Router,\n nodeName: string,\n): (toState: State, fromState?: State) => boolean {\n let routerCache = shouldUpdateCache.get(router);\n\n if (!routerCache) {\n routerCache = new Map();\n shouldUpdateCache.set(router, routerCache);\n }\n\n let fn = routerCache.get(nodeName);\n\n if (!fn) {\n fn = router.shouldUpdateNode(nodeName);\n routerCache.set(nodeName, fn);\n }\n\n return fn;\n}\n","import { computeSnapshot } from \"./computeSnapshot.js\";\nimport { getCachedShouldUpdate } from \"./shouldUpdateCache.js\";\n\nimport type { RouteNodeSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\nclass RouteNodeSource implements RouterSource<RouteNodeSnapshot> {\n #routerUnsubscribe: (() => void) | null = null;\n #currentSnapshot: RouteNodeSnapshot;\n #destroyed = false;\n\n readonly #listeners = new Set<() => void>();\n readonly #router: Router;\n readonly #nodeName: string;\n readonly #shouldUpdate: ReturnType<typeof getCachedShouldUpdate>;\n\n constructor(router: Router, nodeName: string) {\n this.#router = router;\n this.#nodeName = nodeName;\n this.#shouldUpdate = getCachedShouldUpdate(router, nodeName);\n\n const initialSnapshot: RouteNodeSnapshot = {\n route: undefined,\n previousRoute: undefined,\n };\n\n this.#currentSnapshot = computeSnapshot(initialSnapshot, router, nodeName);\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#destroyed) {\n return () => {};\n }\n\n if (this.#listeners.size === 0) {\n // Reconcile snapshot with current router state before connecting.\n // Covers reconnection after Activity hide/show cycles where the\n // source was disconnected and missed navigation events.\n this.#currentSnapshot = computeSnapshot(\n this.#currentSnapshot,\n this.#router,\n this.#nodeName,\n );\n\n // Connect to router on first subscription\n this.#routerUnsubscribe = this.#router.subscribe((next) => {\n if (!this.#shouldUpdate(next.route, next.previousRoute)) {\n return;\n }\n\n const newSnapshot = computeSnapshot(\n this.#currentSnapshot,\n this.#router,\n this.#nodeName,\n next,\n );\n\n /* v8 ignore next 3 -- @preserve: dedup guard; shouldUpdateNode filters accurately so computeSnapshot always returns new ref */\n if (!Object.is(this.#currentSnapshot, newSnapshot)) {\n this.#currentSnapshot = newSnapshot;\n this.#listeners.forEach((cb) => {\n cb();\n });\n }\n });\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n\n if (this.#listeners.size === 0 && this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n };\n }\n\n getSnapshot(): RouteNodeSnapshot {\n return this.#currentSnapshot;\n }\n\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n this.#destroyed = true;\n\n if (this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n\n this.#listeners.clear();\n }\n}\n\n/**\n * Creates a source scoped to a specific route node.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteNodeSource(\n router: Router,\n nodeName: string,\n): RouterSource<RouteNodeSnapshot> {\n return new RouteNodeSource(router, nodeName);\n}\n","export class BaseSource<T> {\n #currentSnapshot: T;\n #destroyed = false;\n\n readonly #listeners = new Set<() => void>();\n\n constructor(initialSnapshot: T) {\n this.#currentSnapshot = initialSnapshot;\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#destroyed) {\n return () => {};\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n };\n }\n\n getSnapshot(): T {\n return this.#currentSnapshot;\n }\n\n updateSnapshot(snapshot: T): void {\n /* v8 ignore next 2 -- @preserve: defensive guard unreachable via public API (destroy() removes router subscription first) */\n if (this.#destroyed) {\n return;\n }\n\n this.#currentSnapshot = snapshot;\n this.#listeners.forEach((listener) => {\n listener();\n });\n }\n\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n this.#destroyed = true;\n this.#listeners.clear();\n }\n}\n","import { areRoutesRelated } from \"@real-router/route-utils\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { ActiveRouteSourceOptions, RouterSource } from \"./types.js\";\nimport type { Params, Router } from \"@real-router/core\";\n\nclass ActiveRouteSource implements RouterSource<boolean> {\n readonly #source: BaseSource<boolean>;\n readonly #unsubscribe: () => void;\n\n constructor(\n router: Router,\n routeName: string,\n params?: Params,\n options?: ActiveRouteSourceOptions,\n ) {\n const strict = options?.strict ?? false;\n const ignoreQueryParams = options?.ignoreQueryParams ?? true;\n\n const initialValue = router.isActiveRoute(\n routeName,\n params,\n strict,\n ignoreQueryParams,\n );\n\n this.#source = new BaseSource(initialValue);\n\n this.#unsubscribe = router.subscribe((next) => {\n const isNewRelated = areRoutesRelated(routeName, next.route.name);\n const isPrevRelated =\n next.previousRoute &&\n areRoutesRelated(routeName, next.previousRoute.name);\n\n if (!isNewRelated && !isPrevRelated) {\n return;\n }\n\n // If new route is not related, we know the route is inactive —\n // avoid calling isActiveRoute for the optimization\n const newValue = isNewRelated\n ? router.isActiveRoute(routeName, params, strict, ignoreQueryParams)\n : false;\n\n if (!Object.is(this.#source.getSnapshot(), newValue)) {\n this.#source.updateSnapshot(newValue);\n }\n });\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n return this.#source.subscribe(listener);\n }\n\n getSnapshot(): boolean {\n return this.#source.getSnapshot();\n }\n\n destroy(): void {\n this.#unsubscribe();\n this.#source.destroy();\n }\n}\n\nexport function createActiveRouteSource(\n router: Router,\n routeName: string,\n params?: Params,\n options?: ActiveRouteSourceOptions,\n): RouterSource<boolean> {\n return new ActiveRouteSource(router, routeName, params, options);\n}\n","import { getPluginApi, events } from \"@real-router/core\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { RouterTransitionSnapshot, RouterSource } from \"./types.js\";\nimport type { Router, State } from \"@real-router/core\";\n\nconst IDLE_SNAPSHOT: RouterTransitionSnapshot = {\n isTransitioning: false,\n toRoute: null,\n fromRoute: null,\n};\n\nclass TransitionSource implements RouterSource<RouterTransitionSnapshot> {\n readonly #source: BaseSource<RouterTransitionSnapshot>;\n readonly #unsubs: (() => void)[];\n\n constructor(router: Router) {\n this.#source = new BaseSource(IDLE_SNAPSHOT);\n\n const api = getPluginApi(router);\n\n const resetToIdle = (): void => {\n this.#source.updateSnapshot(IDLE_SNAPSHOT);\n };\n\n this.#unsubs = [\n api.addEventListener(\n events.TRANSITION_START,\n (toState: State, fromState?: State) => {\n this.#source.updateSnapshot({\n isTransitioning: true,\n toRoute: toState,\n fromRoute: fromState ?? null,\n });\n },\n ),\n api.addEventListener(events.TRANSITION_SUCCESS, resetToIdle),\n api.addEventListener(events.TRANSITION_ERROR, resetToIdle),\n api.addEventListener(events.TRANSITION_CANCEL, resetToIdle),\n ];\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n return this.#source.subscribe(listener);\n }\n\n getSnapshot(): RouterTransitionSnapshot {\n return this.#source.getSnapshot();\n }\n\n destroy(): void {\n this.#unsubs.forEach((u) => {\n u();\n });\n this.#source.destroy();\n }\n}\n\nexport function createTransitionSource(\n router: Router,\n): RouterSource<RouterTransitionSnapshot> {\n return new TransitionSource(router);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/BaseSource.ts","../../src/createRouteSource.ts","../../src/computeSnapshot.ts","../../src/shouldUpdateCache.ts","../../src/createRouteNodeSource.ts","../../src/createActiveRouteSource.ts","../../src/createTransitionSource.ts"],"names":["areRoutesRelated","api","getPluginApi","events"],"mappings":";AAMO,IAAM,aAAN,MAAoB;AAAA,EACzB,gBAAA;AAAA,EACA,UAAA,GAAa,KAAA;AAAA,EAEJ,UAAA,uBAAiB,GAAA,EAAgB;AAAA,EACjC,iBAAA;AAAA,EACA,kBAAA;AAAA,EACA,UAAA;AAAA,EAET,WAAA,CAAY,iBAAoB,OAAA,EAA6B;AAC3D,IAAA,IAAA,CAAK,gBAAA,GAAmB,eAAA;AACxB,IAAA,IAAA,CAAK,oBAAoB,OAAA,EAAS,gBAAA;AAClC,IAAA,IAAA,CAAK,qBAAqB,OAAA,EAAS,iBAAA;AACnC,IAAA,IAAA,CAAK,aAAa,OAAA,EAAS,SAAA;AAE3B,IAAA,IAAA,CAAK,SAAA,GAAY,IAAA,CAAK,SAAA,CAAU,IAAA,CAAK,IAAI,CAAA;AACzC,IAAA,IAAA,CAAK,WAAA,GAAc,IAAA,CAAK,WAAA,CAAY,IAAA,CAAK,IAAI,CAAA;AAC7C,IAAA,IAAA,CAAK,OAAA,G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AAAA,UACT,WAAW,SAAA,IAAa;AAAA,SACzB,CAAA;AAAA,MACH;AAAA,KACF;AAAA,IACAF,KAAA,CAAI,gBAAA,CAAiBE,WAAA,CAAO,kBAAA,EAAoB,WAAW,CAAA;AAAA,IAC3DF,KAAA,CAAI,gBAAA,CAAiBE,WAAA,CAAO,gBAAA,EAAkB,WAAW,CAAA;AAAA,IACzDF,KAAA,CAAI,gBAAA,CAAiBE,WAAA,CAAO,iBAAA,EAAmB,WAAW;AAAA,GAC5D;AAEA,EAAA,OAAO,MAAA;AACT","file":"index.js","sourcesContent":["export interface BaseSourceOptions {\n onFirstSubscribe?: () => void;\n onLastUnsubscribe?: () => void;\n onDestroy?: () => void;\n}\n\nexport class BaseSource<T> {\n #currentSnapshot: T;\n #destroyed = false;\n\n readonly #listeners = new Set<() => void>();\n readonly #onFirstSubscribe: (() => void) | undefined;\n readonly #onLastUnsubscribe: (() => void) | undefined;\n readonly #onDestroy: (() => void) | undefined;\n\n constructor(initialSnapshot: T, options?: BaseSourceOptions) {\n this.#currentSnapshot = initialSnapshot;\n this.#onFirstSubscribe = options?.onFirstSubscribe;\n this.#onLastUnsubscribe = options?.onLastUnsubscribe;\n this.#onDestroy = options?.onDestroy;\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#destroyed) {\n return () => {};\n }\n\n if (this.#listeners.size === 0 && this.#onFirstSubscribe) {\n this.#onFirstSubscribe();\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n\n if (\n !this.#destroyed &&\n this.#listeners.size === 0 &&\n this.#onLastUnsubscribe\n ) {\n this.#onLastUnsubscribe();\n }\n };\n }\n\n getSnapshot(): T {\n return this.#currentSnapshot;\n }\n\n updateSnapshot(snapshot: T): void {\n /* v8 ignore next 2 -- @preserve: defensive guard unreachable via public API (destroy() removes router subscription first) */\n if (this.#destroyed) {\n return;\n }\n\n this.#currentSnapshot = snapshot;\n this.#listeners.forEach((listener) => {\n listener();\n });\n }\n\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n this.#destroyed = true;\n this.#onDestroy?.();\n this.#listeners.clear();\n }\n}\n","import { BaseSource } from \"./BaseSource\";\n\nimport type { RouteSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\n/**\n * Creates a source for the full route state.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteSource(router: Router): RouterSource<RouteSnapshot> {\n let routerUnsubscribe: (() => void) | null = null;\n\n const disconnect = (): void => {\n const unsub = routerUnsubscribe;\n\n routerUnsubscribe = null;\n unsub?.();\n };\n\n const source = new BaseSource<RouteSnapshot>(\n {\n route: router.getState(),\n previousRoute: undefined,\n },\n {\n onFirstSubscribe: () => {\n routerUnsubscribe = router.subscribe((next) => {\n source.updateSnapshot({\n route: next.route,\n previousRoute: next.previousRoute,\n });\n });\n },\n onLastUnsubscribe: disconnect,\n onDestroy: disconnect,\n },\n );\n\n return source;\n}\n","import type { RouteNodeSnapshot } from \"./types.js\";\nimport type { Router, SubscribeState } from \"@real-router/core\";\n\nexport function computeSnapshot(\n currentSnapshot: RouteNodeSnapshot,\n router: Router,\n nodeName: string,\n next?: SubscribeState,\n): RouteNodeSnapshot {\n const currentRoute = next?.route ?? router.getState();\n const previousRoute = next?.previousRoute;\n\n const isNodeActive =\n nodeName === \"\" ||\n (currentRoute !== undefined &&\n (currentRoute.name === nodeName ||\n currentRoute.name.startsWith(`${nodeName}.`)));\n\n const route = isNodeActive ? currentRoute : undefined;\n\n if (\n route === currentSnapshot.route &&\n previousRoute === currentSnapshot.previousRoute\n ) {\n return currentSnapshot;\n }\n\n return { route, previousRoute };\n}\n","import type { Router, State } from \"@real-router/core\";\n\nconst shouldUpdateCache = new WeakMap<\n Router,\n Map<string, (toState: State, fromState?: State) => boolean>\n>();\n\nexport function getCachedShouldUpdate(\n router: Router,\n nodeName: string,\n): (toState: State, fromState?: State) => boolean {\n let routerCache = shouldUpdateCache.get(router);\n\n if (!routerCache) {\n routerCache = new Map();\n shouldUpdateCache.set(router, routerCache);\n }\n\n let fn = routerCache.get(nodeName);\n\n if (!fn) {\n fn = router.shouldUpdateNode(nodeName);\n routerCache.set(nodeName, fn);\n }\n\n return fn;\n}\n","import { BaseSource } from \"./BaseSource\";\nimport { computeSnapshot } from \"./computeSnapshot.js\";\nimport { getCachedShouldUpdate } from \"./shouldUpdateCache.js\";\n\nimport type { RouteNodeSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\n/**\n * Creates a source scoped to a specific route node.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteNodeSource(\n router: Router,\n nodeName: string,\n): RouterSource<RouteNodeSnapshot> {\n let routerUnsubscribe: (() => void) | null = null;\n\n const shouldUpdate = getCachedShouldUpdate(router, nodeName);\n\n const initialSnapshot: RouteNodeSnapshot = {\n route: undefined,\n previousRoute: undefined,\n };\n\n const disconnect = (): void => {\n const unsub = routerUnsubscribe;\n\n routerUnsubscribe = null;\n unsub?.();\n };\n\n const source = new BaseSource<RouteNodeSnapshot>(\n computeSnapshot(initialSnapshot, router, nodeName),\n {\n onFirstSubscribe: () => {\n // Reconcile snapshot with current router state before connecting.\n // Covers reconnection after Activity hide/show cycles where the\n // source was disconnected and missed navigation events.\n source.updateSnapshot(\n computeSnapshot(source.getSnapshot(), router, nodeName),\n );\n\n // Connect to router on first subscription\n routerUnsubscribe = router.subscribe((next) => {\n if (!shouldUpdate(next.route, next.previousRoute)) {\n return;\n }\n\n const newSnapshot = computeSnapshot(\n source.getSnapshot(),\n router,\n nodeName,\n next,\n );\n\n /* v8 ignore next 3 -- @preserve: dedup guard; shouldUpdateNode filters accurately so computeSnapshot always returns new ref */\n if (!Object.is(source.getSnapshot(), newSnapshot)) {\n source.updateSnapshot(newSnapshot);\n }\n });\n },\n onLastUnsubscribe: disconnect,\n onDestroy: disconnect,\n },\n );\n\n return source;\n}\n","import { areRoutesRelated } from \"@real-router/route-utils\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { ActiveRouteSourceOptions, RouterSource } from \"./types.js\";\nimport type { Params, Router } from \"@real-router/core\";\n\nexport function createActiveRouteSource(\n router: Router,\n routeName: string,\n params?: Params,\n options?: ActiveRouteSourceOptions,\n): RouterSource<boolean> {\n const strict = options?.strict ?? false;\n const ignoreQueryParams = options?.ignoreQueryParams ?? true;\n\n const initialValue = router.isActiveRoute(\n routeName,\n params,\n strict,\n ignoreQueryParams,\n );\n\n const source = new BaseSource(initialValue, {\n onDestroy: () => {\n unsubscribe();\n },\n });\n\n // Eager connection: subscribe to router immediately\n const unsubscribe = router.subscribe((next) => {\n const isNewRelated = areRoutesRelated(routeName, next.route.name);\n const isPrevRelated =\n next.previousRoute &&\n areRoutesRelated(routeName, next.previousRoute.name);\n\n if (!isNewRelated && !isPrevRelated) {\n return;\n }\n\n // If new route is not related, we know the route is inactive —\n // avoid calling isActiveRoute for the optimization\n const newValue = isNewRelated\n ? router.isActiveRoute(routeName, params, strict, ignoreQueryParams)\n : false;\n\n if (!Object.is(source.getSnapshot(), newValue)) {\n source.updateSnapshot(newValue);\n }\n });\n\n return source;\n}\n","import { events } from \"@real-router/core\";\nimport { getPluginApi } from \"@real-router/core/api\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { RouterTransitionSnapshot, RouterSource } from \"./types.js\";\nimport type { Router, State } from \"@real-router/core\";\n\nconst IDLE_SNAPSHOT: RouterTransitionSnapshot = {\n isTransitioning: false,\n toRoute: null,\n fromRoute: null,\n};\n\nexport function createTransitionSource(\n router: Router,\n): RouterSource<RouterTransitionSnapshot> {\n const source = new BaseSource(IDLE_SNAPSHOT, {\n onDestroy: () => {\n unsubs.forEach((u) => {\n u();\n });\n },\n });\n\n const api = getPluginApi(router);\n\n const resetToIdle = (): void => {\n source.updateSnapshot(IDLE_SNAPSHOT);\n };\n\n // Eager connection: subscribe to router events immediately\n const unsubs = [\n api.addEventListener(\n events.TRANSITION_START,\n (toState: State, fromState?: State) => {\n source.updateSnapshot({\n isTransitioning: true,\n toRoute: toState,\n fromRoute: fromState ?? null,\n });\n },\n ),\n api.addEventListener(events.TRANSITION_SUCCESS, resetToIdle),\n api.addEventListener(events.TRANSITION_ERROR, resetToIdle),\n api.addEventListener(events.TRANSITION_CANCEL, resetToIdle),\n ];\n\n return source;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.
|
|
1
|
+
{"inputs":{"../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js":{"bytes":569,"imports":[],"format":"esm"},"src/BaseSource.ts":{"bytes":1857,"imports":[{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/createRouteSource.ts":{"bytes":1168,"imports":[{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"},{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/computeSnapshot.ts":{"bytes":790,"imports":[{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/shouldUpdateCache.ts":{"bytes":610,"imports":[{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/createRouteNodeSource.ts":{"bytes":2245,"imports":[{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"},{"path":"src/computeSnapshot.ts","kind":"import-statement","original":"./computeSnapshot.js"},{"path":"src/shouldUpdateCache.ts","kind":"import-statement","original":"./shouldUpdateCache.js"},{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/createActiveRouteSource.ts":{"bytes":1461,"imports":[{"path":"@real-router/route-utils","kind":"import-statement","external":true},{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"},{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/createTransitionSource.ts":{"bytes":1318,"imports":[{"path":"@real-router/core","kind":"import-statement","external":true},{"path":"@real-router/core/api","kind":"import-statement","external":true},{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"},{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":407,"imports":[{"path":"src/createRouteSource.ts","kind":"import-statement","original":"./createRouteSource"},{"path":"src/createRouteNodeSource.ts","kind":"import-statement","original":"./createRouteNodeSource"},{"path":"src/createActiveRouteSource.ts","kind":"import-statement","original":"./createActiveRouteSource"},{"path":"src/createTransitionSource.ts","kind":"import-statement","original":"./createTransitionSource"},{"path":"/home/runner/work/real-router/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"}},"outputs":{"dist/cjs/index.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":14205},"dist/cjs/index.js":{"imports":[{"path":"@real-router/route-utils","kind":"import-statement","external":true},{"path":"@real-router/core","kind":"import-statement","external":true},{"path":"@real-router/core/api","kind":"import-statement","external":true}],"exports":["createActiveRouteSource","createRouteNodeSource","createRouteSource","createTransitionSource"],"entryPoint":"src/index.ts","inputs":{"src/BaseSource.ts":{"bytesInOutput":1375},"src/createRouteSource.ts":{"bytesInOutput":627},"src/index.ts":{"bytesInOutput":0},"src/computeSnapshot.ts":{"bytesInOutput":536},"src/shouldUpdateCache.ts":{"bytesInOutput":425},"src/createRouteNodeSource.ts":{"bytesInOutput":1120},"src/createActiveRouteSource.ts":{"bytesInOutput":977},"src/createTransitionSource.ts":{"bytesInOutput":961}},"bytes":6593}}}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{areRoutesRelated as t}from"@real-router/route-utils";import{
|
|
1
|
+
import{areRoutesRelated as t}from"@real-router/route-utils";import{events as e}from"@real-router/core";import{getPluginApi as s}from"@real-router/core/api";var o=class{#t;#e=!1;#s=new Set;#o;#r;#n;constructor(t,e){this.#t=t,this.#o=e?.onFirstSubscribe,this.#r=e?.onLastUnsubscribe,this.#n=e?.onDestroy,this.subscribe=this.subscribe.bind(this),this.getSnapshot=this.getSnapshot.bind(this),this.destroy=this.destroy.bind(this)}subscribe(t){return this.#e?()=>{}:(0===this.#s.size&&this.#o&&this.#o(),this.#s.add(t),()=>{this.#s.delete(t),!this.#e&&0===this.#s.size&&this.#r&&this.#r()})}getSnapshot(){return this.#t}updateSnapshot(t){this.#e||(this.#t=t,this.#s.forEach(t=>{t()}))}destroy(){this.#e||(this.#e=!0,this.#n?.(),this.#s.clear())}};function r(t){let e=null;const s=()=>{const t=e;e=null,t?.()},r=new o({route:t.getState(),previousRoute:void 0},{onFirstSubscribe:()=>{e=t.subscribe(t=>{r.updateSnapshot({route:t.route,previousRoute:t.previousRoute})})},onLastUnsubscribe:s,onDestroy:s});return r}function n(t,e,s,o){const r=o?.route??e.getState(),n=o?.previousRoute,i=""===s||void 0!==r&&(r.name===s||r.name.startsWith(`${s}.`))?r:void 0;return i===t.route&&n===t.previousRoute?t:{route:i,previousRoute:n}}var i=new WeakMap;function u(t,e){let s=null;const r=function(t,e){let s=i.get(t);s||(s=new Map,i.set(t,s));let o=s.get(e);return o||(o=t.shouldUpdateNode(e),s.set(e,o)),o}(t,e),u=()=>{const t=s;s=null,t?.()},a=new o(n({route:void 0,previousRoute:void 0},t,e),{onFirstSubscribe:()=>{a.updateSnapshot(n(a.getSnapshot(),t,e)),s=t.subscribe(s=>{if(!r(s.route,s.previousRoute))return;const o=n(a.getSnapshot(),t,e,s);Object.is(a.getSnapshot(),o)||a.updateSnapshot(o)})},onLastUnsubscribe:u,onDestroy:u});return a}function a(e,s,r,n){const i=n?.strict??!1,u=n?.ignoreQueryParams??!0,a=e.isActiveRoute(s,r,i,u),c=new o(a,{onDestroy:()=>{h()}}),h=e.subscribe(o=>{const n=t(s,o.route.name),a=o.previousRoute&&t(s,o.previousRoute.name);if(!n&&!a)return;const h=!!n&&e.isActiveRoute(s,r,i,u);Object.is(c.getSnapshot(),h)||c.updateSnapshot(h)});return c}var c={isTransitioning:!1,toRoute:null,fromRoute:null};function h(t){const r=new o(c,{onDestroy:()=>{u.forEach(t=>{t()})}}),n=s(t),i=()=>{r.updateSnapshot(c)},u=[n.addEventListener(e.TRANSITION_START,(t,e)=>{r.updateSnapshot({isTransitioning:!0,toRoute:t,fromRoute:e??null})}),n.addEventListener(e.TRANSITION_SUCCESS,i),n.addEventListener(e.TRANSITION_ERROR,i),n.addEventListener(e.TRANSITION_CANCEL,i)];return r}export{a as createActiveRouteSource,u as createRouteNodeSource,r as createRouteSource,h as createTransitionSource};//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/createRouteSource.ts","../../src/computeSnapshot.ts","../../src/shouldUpdateCache.ts","../../src/createRouteNodeSource.ts","../../src/BaseSource.ts","../../src/createActiveRouteSource.ts","../../src/createTransitionSource.ts"],"names":[],"mappings":";AAGA,IAAM,cAAN,MAAyD;AAAA,EACvD,kBAAA,GAA0C,IAAA;AAAA,EAC1C,gBAAA;AAAA,EAES,UAAA,uBAAiB,GAAA,EAAgB;AAAA,EACjC,OAAA;AAAA,EAET,YAAY,MAAA,EAAgB;AAC1B,IAAA,IAAA,CAAK,OAAA,GAAU,MAAA;AAEf,IAAA,IAAA,CAAK,gBAAA,GAAmB;AAAA,MACtB,KAAA,EAAO,OAAO,QAAA,EAAS;AAAA,MACvB,aAAA,EAAe;AAAA,KACjB;AAEA,IAAA,IAAA,CAAK,SAAA,GAAY,IAAA,CAAK,SAAA,CAAU,IAAA,CAAK,IAAI,CAAA;AACzC,IAAA,IAAA,CAAK,OAAA,GAAU,IAAA,CAAK,OAAA,CAAQ,IAAA,CAAK,IAAI,CAAA;AACrC,IAAA,IAAA,CAAK,WAAA,GAAc,IAAA,CAAK,WAAA,CAAY,IAAA,CAAK,IAAI,CAAA;AAAA,EAC/C;AAAA,EAEA,UAAU,QAAA,EAAkC;AAC1C,IAAA,IAAI,IAAA,CAAK,UAAA,CAAW,IAAA,KAAS,CAAA,EAAG;AAE9B,MAAA,IAAA,CAAK,kBAAA,GAAqB,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,CAAC,IAAA,KAAS;AACzD,QAAA,IAAA,CAAK,gBAAA,GAAmB;AAAA,UACtB,OAAO,IAAA,CAAK,KAAA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AmB,WAAW;AAAA,KAC5D;AAEA,IAAA,IAAA,CAAK,SAAA,GAAY,IAAA,CAAK,SAAA,CAAU,IAAA,CAAK,IAAI,CAAA;AACzC,IAAA,IAAA,CAAK,WAAA,GAAc,IAAA,CAAK,WAAA,CAAY,IAAA,CAAK,IAAI,CAAA;AAC7C,IAAA,IAAA,CAAK,OAAA,GAAU,IAAA,CAAK,OAAA,CAAQ,IAAA,CAAK,IAAI,CAAA;AAAA,EACvC;AAAA,EAEA,UAAU,QAAA,EAAkC;AAC1C,IAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,QAAQ,CAAA;AAAA,EACxC;AAAA,EAEA,WAAA,GAAwC;AACtC,IAAA,OAAO,IAAA,CAAK,QAAQ,WAAA,EAAY;AAAA,EAClC;AAAA,EAEA,OAAA,GAAgB;AACd,IAAA,IAAA,CAAK,OAAA,CAAQ,OAAA,CAAQ,CAAC,CAAA,KAAM;AAC1B,MAAA,CAAA,EAAE;AAAA,IACJ,CAAC,CAAA;AACD,IAAA,IAAA,CAAK,QAAQ,OAAA,EAAQ;AAAA,EACvB;AACF,CAAA;AAEO,SAAS,uBACd,MAAA,EACwC;AACxC,EAAA,OAAO,IAAI,iBAAiB,MAAM,CAAA;AACpC","file":"index.mjs","sourcesContent":["import type { RouteSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\nclass RouteSource implements RouterSource<RouteSnapshot> {\n #routerUnsubscribe: (() => void) | null = null;\n #currentSnapshot: RouteSnapshot;\n\n readonly #listeners = new Set<() => void>();\n readonly #router: Router;\n\n constructor(router: Router) {\n this.#router = router;\n\n this.#currentSnapshot = {\n route: router.getState(),\n previousRoute: undefined,\n };\n\n this.subscribe = this.subscribe.bind(this);\n this.destroy = this.destroy.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#listeners.size === 0) {\n // Connect to router on first subscription\n this.#routerUnsubscribe = this.#router.subscribe((next) => {\n this.#currentSnapshot = {\n route: next.route,\n previousRoute: next.previousRoute,\n };\n this.#listeners.forEach((cb) => {\n cb();\n });\n });\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n\n if (this.#listeners.size === 0 && this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n };\n }\n\n getSnapshot(): RouteSnapshot {\n return this.#currentSnapshot;\n }\n\n destroy(): void {\n if (this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n\n this.#listeners.clear();\n }\n}\n\n/**\n * Creates a source for the full route state.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteSource(router: Router): RouterSource<RouteSnapshot> {\n return new RouteSource(router);\n}\n","import type { RouteNodeSnapshot } from \"./types.js\";\nimport type { Router, SubscribeState } from \"@real-router/core\";\n\nexport function computeSnapshot(\n currentSnapshot: RouteNodeSnapshot,\n router: Router,\n nodeName: string,\n next?: SubscribeState,\n): RouteNodeSnapshot {\n const currentRoute = next?.route ?? router.getState();\n const previousRoute = next?.previousRoute;\n\n const isNodeActive =\n nodeName === \"\" ||\n (currentRoute !== undefined &&\n (currentRoute.name === nodeName ||\n currentRoute.name.startsWith(`${nodeName}.`)));\n\n const route = isNodeActive ? currentRoute : undefined;\n\n if (\n route === currentSnapshot.route &&\n previousRoute === currentSnapshot.previousRoute\n ) {\n return currentSnapshot;\n }\n\n return { route, previousRoute };\n}\n","import type { Router, State } from \"@real-router/core\";\n\nconst shouldUpdateCache = new WeakMap<\n Router,\n Map<string, (toState: State, fromState?: State) => boolean>\n>();\n\nexport function getCachedShouldUpdate(\n router: Router,\n nodeName: string,\n): (toState: State, fromState?: State) => boolean {\n let routerCache = shouldUpdateCache.get(router);\n\n if (!routerCache) {\n routerCache = new Map();\n shouldUpdateCache.set(router, routerCache);\n }\n\n let fn = routerCache.get(nodeName);\n\n if (!fn) {\n fn = router.shouldUpdateNode(nodeName);\n routerCache.set(nodeName, fn);\n }\n\n return fn;\n}\n","import { computeSnapshot } from \"./computeSnapshot.js\";\nimport { getCachedShouldUpdate } from \"./shouldUpdateCache.js\";\n\nimport type { RouteNodeSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\nclass RouteNodeSource implements RouterSource<RouteNodeSnapshot> {\n #routerUnsubscribe: (() => void) | null = null;\n #currentSnapshot: RouteNodeSnapshot;\n #destroyed = false;\n\n readonly #listeners = new Set<() => void>();\n readonly #router: Router;\n readonly #nodeName: string;\n readonly #shouldUpdate: ReturnType<typeof getCachedShouldUpdate>;\n\n constructor(router: Router, nodeName: string) {\n this.#router = router;\n this.#nodeName = nodeName;\n this.#shouldUpdate = getCachedShouldUpdate(router, nodeName);\n\n const initialSnapshot: RouteNodeSnapshot = {\n route: undefined,\n previousRoute: undefined,\n };\n\n this.#currentSnapshot = computeSnapshot(initialSnapshot, router, nodeName);\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#destroyed) {\n return () => {};\n }\n\n if (this.#listeners.size === 0) {\n // Reconcile snapshot with current router state before connecting.\n // Covers reconnection after Activity hide/show cycles where the\n // source was disconnected and missed navigation events.\n this.#currentSnapshot = computeSnapshot(\n this.#currentSnapshot,\n this.#router,\n this.#nodeName,\n );\n\n // Connect to router on first subscription\n this.#routerUnsubscribe = this.#router.subscribe((next) => {\n if (!this.#shouldUpdate(next.route, next.previousRoute)) {\n return;\n }\n\n const newSnapshot = computeSnapshot(\n this.#currentSnapshot,\n this.#router,\n this.#nodeName,\n next,\n );\n\n /* v8 ignore next 3 -- @preserve: dedup guard; shouldUpdateNode filters accurately so computeSnapshot always returns new ref */\n if (!Object.is(this.#currentSnapshot, newSnapshot)) {\n this.#currentSnapshot = newSnapshot;\n this.#listeners.forEach((cb) => {\n cb();\n });\n }\n });\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n\n if (this.#listeners.size === 0 && this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n };\n }\n\n getSnapshot(): RouteNodeSnapshot {\n return this.#currentSnapshot;\n }\n\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n this.#destroyed = true;\n\n if (this.#routerUnsubscribe) {\n this.#routerUnsubscribe();\n this.#routerUnsubscribe = null;\n }\n\n this.#listeners.clear();\n }\n}\n\n/**\n * Creates a source scoped to a specific route node.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteNodeSource(\n router: Router,\n nodeName: string,\n): RouterSource<RouteNodeSnapshot> {\n return new RouteNodeSource(router, nodeName);\n}\n","export class BaseSource<T> {\n #currentSnapshot: T;\n #destroyed = false;\n\n readonly #listeners = new Set<() => void>();\n\n constructor(initialSnapshot: T) {\n this.#currentSnapshot = initialSnapshot;\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#destroyed) {\n return () => {};\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n };\n }\n\n getSnapshot(): T {\n return this.#currentSnapshot;\n }\n\n updateSnapshot(snapshot: T): void {\n /* v8 ignore next 2 -- @preserve: defensive guard unreachable via public API (destroy() removes router subscription first) */\n if (this.#destroyed) {\n return;\n }\n\n this.#currentSnapshot = snapshot;\n this.#listeners.forEach((listener) => {\n listener();\n });\n }\n\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n this.#destroyed = true;\n this.#listeners.clear();\n }\n}\n","import { areRoutesRelated } from \"@real-router/route-utils\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { ActiveRouteSourceOptions, RouterSource } from \"./types.js\";\nimport type { Params, Router } from \"@real-router/core\";\n\nclass ActiveRouteSource implements RouterSource<boolean> {\n readonly #source: BaseSource<boolean>;\n readonly #unsubscribe: () => void;\n\n constructor(\n router: Router,\n routeName: string,\n params?: Params,\n options?: ActiveRouteSourceOptions,\n ) {\n const strict = options?.strict ?? false;\n const ignoreQueryParams = options?.ignoreQueryParams ?? true;\n\n const initialValue = router.isActiveRoute(\n routeName,\n params,\n strict,\n ignoreQueryParams,\n );\n\n this.#source = new BaseSource(initialValue);\n\n this.#unsubscribe = router.subscribe((next) => {\n const isNewRelated = areRoutesRelated(routeName, next.route.name);\n const isPrevRelated =\n next.previousRoute &&\n areRoutesRelated(routeName, next.previousRoute.name);\n\n if (!isNewRelated && !isPrevRelated) {\n return;\n }\n\n // If new route is not related, we know the route is inactive —\n // avoid calling isActiveRoute for the optimization\n const newValue = isNewRelated\n ? router.isActiveRoute(routeName, params, strict, ignoreQueryParams)\n : false;\n\n if (!Object.is(this.#source.getSnapshot(), newValue)) {\n this.#source.updateSnapshot(newValue);\n }\n });\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n return this.#source.subscribe(listener);\n }\n\n getSnapshot(): boolean {\n return this.#source.getSnapshot();\n }\n\n destroy(): void {\n this.#unsubscribe();\n this.#source.destroy();\n }\n}\n\nexport function createActiveRouteSource(\n router: Router,\n routeName: string,\n params?: Params,\n options?: ActiveRouteSourceOptions,\n): RouterSource<boolean> {\n return new ActiveRouteSource(router, routeName, params, options);\n}\n","import { getPluginApi, events } from \"@real-router/core\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { RouterTransitionSnapshot, RouterSource } from \"./types.js\";\nimport type { Router, State } from \"@real-router/core\";\n\nconst IDLE_SNAPSHOT: RouterTransitionSnapshot = {\n isTransitioning: false,\n toRoute: null,\n fromRoute: null,\n};\n\nclass TransitionSource implements RouterSource<RouterTransitionSnapshot> {\n readonly #source: BaseSource<RouterTransitionSnapshot>;\n readonly #unsubs: (() => void)[];\n\n constructor(router: Router) {\n this.#source = new BaseSource(IDLE_SNAPSHOT);\n\n const api = getPluginApi(router);\n\n const resetToIdle = (): void => {\n this.#source.updateSnapshot(IDLE_SNAPSHOT);\n };\n\n this.#unsubs = [\n api.addEventListener(\n events.TRANSITION_START,\n (toState: State, fromState?: State) => {\n this.#source.updateSnapshot({\n isTransitioning: true,\n toRoute: toState,\n fromRoute: fromState ?? null,\n });\n },\n ),\n api.addEventListener(events.TRANSITION_SUCCESS, resetToIdle),\n api.addEventListener(events.TRANSITION_ERROR, resetToIdle),\n api.addEventListener(events.TRANSITION_CANCEL, resetToIdle),\n ];\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n return this.#source.subscribe(listener);\n }\n\n getSnapshot(): RouterTransitionSnapshot {\n return this.#source.getSnapshot();\n }\n\n destroy(): void {\n this.#unsubs.forEach((u) => {\n u();\n });\n this.#source.destroy();\n }\n}\n\nexport function createTransitionSource(\n router: Router,\n): RouterSource<RouterTransitionSnapshot> {\n return new TransitionSource(router);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/BaseSource.ts","../../src/createRouteSource.ts","../../src/computeSnapshot.ts","../../src/shouldUpdateCache.ts","../../src/createRouteNodeSource.ts","../../src/createActiveRouteSource.ts","../../src/createTransitionSource.ts"],"names":[],"mappings":";AAMO,IAAM,aAAN,MAAoB;AAAA,EACzB,gBAAA;AAAA,EACA,UAAA,GAAa,KAAA;AAAA,EAEJ,UAAA,uBAAiB,GAAA,EAAgB;AAAA,EACjC,iBAAA;AAAA,EACA,kBAAA;AAAA,EACA,UAAA;AAAA,EAET,WAAA,CAAY,iBAAoB,OAAA,EAA6B;AAC3D,IAAA,IAAA,CAAK,gBAAA,GAAmB,eAAA;AACxB,IAAA,IAAA,CAAK,oBAAoB,OAAA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AA,MAAM,cAAc,MAAY;AAC9B,IAAA,MAAA,CAAO,eAAe,aAAa,CAAA;AAAA,EACrC,CAAA;AAGA,EAAA,MAAM,MAAA,GAAS;AAAA,IACb,GAAA,CAAI,gBAAA;AAAA,MACF,MAAA,CAAO,gBAAA;AAAA,MACP,CAAC,SAAgB,SAAA,KAAsB;AACrC,QAAA,MAAA,CAAO,cAAA,CAAe;AAAA,UACpB,eAAA,EAAiB,IAAA;AAAA,UACjB,OAAA,EAAS,OAAA;AAAA,UACT,WAAW,SAAA,IAAa;AAAA,SACzB,CAAA;AAAA,MACH;AAAA,KACF;AAAA,IACA,GAAA,CAAI,gBAAA,CAAiB,MAAA,CAAO,kBAAA,EAAoB,WAAW,CAAA;AAAA,IAC3D,GAAA,CAAI,gBAAA,CAAiB,MAAA,CAAO,gBAAA,EAAkB,WAAW,CAAA;AAAA,IACzD,GAAA,CAAI,gBAAA,CAAiB,MAAA,CAAO,iBAAA,EAAmB,WAAW;AAAA,GAC5D;AAEA,EAAA,OAAO,MAAA;AACT","file":"index.mjs","sourcesContent":["export interface BaseSourceOptions {\n onFirstSubscribe?: () => void;\n onLastUnsubscribe?: () => void;\n onDestroy?: () => void;\n}\n\nexport class BaseSource<T> {\n #currentSnapshot: T;\n #destroyed = false;\n\n readonly #listeners = new Set<() => void>();\n readonly #onFirstSubscribe: (() => void) | undefined;\n readonly #onLastUnsubscribe: (() => void) | undefined;\n readonly #onDestroy: (() => void) | undefined;\n\n constructor(initialSnapshot: T, options?: BaseSourceOptions) {\n this.#currentSnapshot = initialSnapshot;\n this.#onFirstSubscribe = options?.onFirstSubscribe;\n this.#onLastUnsubscribe = options?.onLastUnsubscribe;\n this.#onDestroy = options?.onDestroy;\n\n this.subscribe = this.subscribe.bind(this);\n this.getSnapshot = this.getSnapshot.bind(this);\n this.destroy = this.destroy.bind(this);\n }\n\n subscribe(listener: () => void): () => void {\n if (this.#destroyed) {\n return () => {};\n }\n\n if (this.#listeners.size === 0 && this.#onFirstSubscribe) {\n this.#onFirstSubscribe();\n }\n\n this.#listeners.add(listener);\n\n return () => {\n this.#listeners.delete(listener);\n\n if (\n !this.#destroyed &&\n this.#listeners.size === 0 &&\n this.#onLastUnsubscribe\n ) {\n this.#onLastUnsubscribe();\n }\n };\n }\n\n getSnapshot(): T {\n return this.#currentSnapshot;\n }\n\n updateSnapshot(snapshot: T): void {\n /* v8 ignore next 2 -- @preserve: defensive guard unreachable via public API (destroy() removes router subscription first) */\n if (this.#destroyed) {\n return;\n }\n\n this.#currentSnapshot = snapshot;\n this.#listeners.forEach((listener) => {\n listener();\n });\n }\n\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n this.#destroyed = true;\n this.#onDestroy?.();\n this.#listeners.clear();\n }\n}\n","import { BaseSource } from \"./BaseSource\";\n\nimport type { RouteSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\n/**\n * Creates a source for the full route state.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteSource(router: Router): RouterSource<RouteSnapshot> {\n let routerUnsubscribe: (() => void) | null = null;\n\n const disconnect = (): void => {\n const unsub = routerUnsubscribe;\n\n routerUnsubscribe = null;\n unsub?.();\n };\n\n const source = new BaseSource<RouteSnapshot>(\n {\n route: router.getState(),\n previousRoute: undefined,\n },\n {\n onFirstSubscribe: () => {\n routerUnsubscribe = router.subscribe((next) => {\n source.updateSnapshot({\n route: next.route,\n previousRoute: next.previousRoute,\n });\n });\n },\n onLastUnsubscribe: disconnect,\n onDestroy: disconnect,\n },\n );\n\n return source;\n}\n","import type { RouteNodeSnapshot } from \"./types.js\";\nimport type { Router, SubscribeState } from \"@real-router/core\";\n\nexport function computeSnapshot(\n currentSnapshot: RouteNodeSnapshot,\n router: Router,\n nodeName: string,\n next?: SubscribeState,\n): RouteNodeSnapshot {\n const currentRoute = next?.route ?? router.getState();\n const previousRoute = next?.previousRoute;\n\n const isNodeActive =\n nodeName === \"\" ||\n (currentRoute !== undefined &&\n (currentRoute.name === nodeName ||\n currentRoute.name.startsWith(`${nodeName}.`)));\n\n const route = isNodeActive ? currentRoute : undefined;\n\n if (\n route === currentSnapshot.route &&\n previousRoute === currentSnapshot.previousRoute\n ) {\n return currentSnapshot;\n }\n\n return { route, previousRoute };\n}\n","import type { Router, State } from \"@real-router/core\";\n\nconst shouldUpdateCache = new WeakMap<\n Router,\n Map<string, (toState: State, fromState?: State) => boolean>\n>();\n\nexport function getCachedShouldUpdate(\n router: Router,\n nodeName: string,\n): (toState: State, fromState?: State) => boolean {\n let routerCache = shouldUpdateCache.get(router);\n\n if (!routerCache) {\n routerCache = new Map();\n shouldUpdateCache.set(router, routerCache);\n }\n\n let fn = routerCache.get(nodeName);\n\n if (!fn) {\n fn = router.shouldUpdateNode(nodeName);\n routerCache.set(nodeName, fn);\n }\n\n return fn;\n}\n","import { BaseSource } from \"./BaseSource\";\nimport { computeSnapshot } from \"./computeSnapshot.js\";\nimport { getCachedShouldUpdate } from \"./shouldUpdateCache.js\";\n\nimport type { RouteNodeSnapshot, RouterSource } from \"./types.js\";\nimport type { Router } from \"@real-router/core\";\n\n/**\n * Creates a source scoped to a specific route node.\n *\n * Uses a lazy-connection pattern: the router subscription is created when the\n * first listener subscribes and removed when the last listener unsubscribes.\n * This is compatible with React's useSyncExternalStore and Strict Mode.\n */\nexport function createRouteNodeSource(\n router: Router,\n nodeName: string,\n): RouterSource<RouteNodeSnapshot> {\n let routerUnsubscribe: (() => void) | null = null;\n\n const shouldUpdate = getCachedShouldUpdate(router, nodeName);\n\n const initialSnapshot: RouteNodeSnapshot = {\n route: undefined,\n previousRoute: undefined,\n };\n\n const disconnect = (): void => {\n const unsub = routerUnsubscribe;\n\n routerUnsubscribe = null;\n unsub?.();\n };\n\n const source = new BaseSource<RouteNodeSnapshot>(\n computeSnapshot(initialSnapshot, router, nodeName),\n {\n onFirstSubscribe: () => {\n // Reconcile snapshot with current router state before connecting.\n // Covers reconnection after Activity hide/show cycles where the\n // source was disconnected and missed navigation events.\n source.updateSnapshot(\n computeSnapshot(source.getSnapshot(), router, nodeName),\n );\n\n // Connect to router on first subscription\n routerUnsubscribe = router.subscribe((next) => {\n if (!shouldUpdate(next.route, next.previousRoute)) {\n return;\n }\n\n const newSnapshot = computeSnapshot(\n source.getSnapshot(),\n router,\n nodeName,\n next,\n );\n\n /* v8 ignore next 3 -- @preserve: dedup guard; shouldUpdateNode filters accurately so computeSnapshot always returns new ref */\n if (!Object.is(source.getSnapshot(), newSnapshot)) {\n source.updateSnapshot(newSnapshot);\n }\n });\n },\n onLastUnsubscribe: disconnect,\n onDestroy: disconnect,\n },\n );\n\n return source;\n}\n","import { areRoutesRelated } from \"@real-router/route-utils\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { ActiveRouteSourceOptions, RouterSource } from \"./types.js\";\nimport type { Params, Router } from \"@real-router/core\";\n\nexport function createActiveRouteSource(\n router: Router,\n routeName: string,\n params?: Params,\n options?: ActiveRouteSourceOptions,\n): RouterSource<boolean> {\n const strict = options?.strict ?? false;\n const ignoreQueryParams = options?.ignoreQueryParams ?? true;\n\n const initialValue = router.isActiveRoute(\n routeName,\n params,\n strict,\n ignoreQueryParams,\n );\n\n const source = new BaseSource(initialValue, {\n onDestroy: () => {\n unsubscribe();\n },\n });\n\n // Eager connection: subscribe to router immediately\n const unsubscribe = router.subscribe((next) => {\n const isNewRelated = areRoutesRelated(routeName, next.route.name);\n const isPrevRelated =\n next.previousRoute &&\n areRoutesRelated(routeName, next.previousRoute.name);\n\n if (!isNewRelated && !isPrevRelated) {\n return;\n }\n\n // If new route is not related, we know the route is inactive —\n // avoid calling isActiveRoute for the optimization\n const newValue = isNewRelated\n ? router.isActiveRoute(routeName, params, strict, ignoreQueryParams)\n : false;\n\n if (!Object.is(source.getSnapshot(), newValue)) {\n source.updateSnapshot(newValue);\n }\n });\n\n return source;\n}\n","import { events } from \"@real-router/core\";\nimport { getPluginApi } from \"@real-router/core/api\";\n\nimport { BaseSource } from \"./BaseSource\";\n\nimport type { RouterTransitionSnapshot, RouterSource } from \"./types.js\";\nimport type { Router, State } from \"@real-router/core\";\n\nconst IDLE_SNAPSHOT: RouterTransitionSnapshot = {\n isTransitioning: false,\n toRoute: null,\n fromRoute: null,\n};\n\nexport function createTransitionSource(\n router: Router,\n): RouterSource<RouterTransitionSnapshot> {\n const source = new BaseSource(IDLE_SNAPSHOT, {\n onDestroy: () => {\n unsubs.forEach((u) => {\n u();\n });\n },\n });\n\n const api = getPluginApi(router);\n\n const resetToIdle = (): void => {\n source.updateSnapshot(IDLE_SNAPSHOT);\n };\n\n // Eager connection: subscribe to router events immediately\n const unsubs = [\n api.addEventListener(\n events.TRANSITION_START,\n (toState: State, fromState?: State) => {\n source.updateSnapshot({\n isTransitioning: true,\n toRoute: toState,\n fromRoute: fromState ?? null,\n });\n },\n ),\n api.addEventListener(events.TRANSITION_SUCCESS, resetToIdle),\n api.addEventListener(events.TRANSITION_ERROR, resetToIdle),\n api.addEventListener(events.TRANSITION_CANCEL, resetToIdle),\n ];\n\n return source;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/createRouteSource.ts":{"bytes":
|
|
1
|
+
{"inputs":{"src/BaseSource.ts":{"bytes":1857,"imports":[],"format":"esm"},"src/createRouteSource.ts":{"bytes":1168,"imports":[{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"}],"format":"esm"},"src/computeSnapshot.ts":{"bytes":790,"imports":[],"format":"esm"},"src/shouldUpdateCache.ts":{"bytes":610,"imports":[],"format":"esm"},"src/createRouteNodeSource.ts":{"bytes":2245,"imports":[{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"},{"path":"src/computeSnapshot.ts","kind":"import-statement","original":"./computeSnapshot.js"},{"path":"src/shouldUpdateCache.ts","kind":"import-statement","original":"./shouldUpdateCache.js"}],"format":"esm"},"src/createActiveRouteSource.ts":{"bytes":1461,"imports":[{"path":"@real-router/route-utils","kind":"import-statement","external":true},{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"}],"format":"esm"},"src/createTransitionSource.ts":{"bytes":1318,"imports":[{"path":"@real-router/core","kind":"import-statement","external":true},{"path":"@real-router/core/api","kind":"import-statement","external":true},{"path":"src/BaseSource.ts","kind":"import-statement","original":"./BaseSource"}],"format":"esm"},"src/index.ts":{"bytes":407,"imports":[{"path":"src/createRouteSource.ts","kind":"import-statement","original":"./createRouteSource"},{"path":"src/createRouteNodeSource.ts","kind":"import-statement","original":"./createRouteNodeSource"},{"path":"src/createActiveRouteSource.ts","kind":"import-statement","original":"./createActiveRouteSource"},{"path":"src/createTransitionSource.ts","kind":"import-statement","original":"./createTransitionSource"}],"format":"esm"}},"outputs":{"dist/esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":14205},"dist/esm/index.mjs":{"imports":[{"path":"@real-router/route-utils","kind":"import-statement","external":true},{"path":"@real-router/core","kind":"import-statement","external":true},{"path":"@real-router/core/api","kind":"import-statement","external":true}],"exports":["createActiveRouteSource","createRouteNodeSource","createRouteSource","createTransitionSource"],"entryPoint":"src/index.ts","inputs":{"src/BaseSource.ts":{"bytesInOutput":1375},"src/createRouteSource.ts":{"bytesInOutput":627},"src/index.ts":{"bytesInOutput":0},"src/computeSnapshot.ts":{"bytesInOutput":536},"src/shouldUpdateCache.ts":{"bytesInOutput":425},"src/createRouteNodeSource.ts":{"bytesInOutput":1120},"src/createActiveRouteSource.ts":{"bytesInOutput":977},"src/createTransitionSource.ts":{"bytesInOutput":961}},"bytes":6593}}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/sources",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "Framework-agnostic subscription layer for Real-Router state",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"homepage": "https://github.com/greydragon888/real-router",
|
|
44
44
|
"sideEffects": false,
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@real-router/core": "^0.
|
|
46
|
+
"@real-router/core": "^0.36.0",
|
|
47
47
|
"@real-router/route-utils": "^0.1.4"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
@@ -56,6 +56,8 @@
|
|
|
56
56
|
"lint": "eslint --cache --ext .ts src/ tests/ --fix --max-warnings 0 --no-error-on-unmatched-pattern",
|
|
57
57
|
"lint:package": "publint",
|
|
58
58
|
"lint:types": "attw --pack .",
|
|
59
|
+
"test:properties": "vitest --config vitest.config.properties.mts --run",
|
|
60
|
+
"test:stress": "vitest run --config vitest.config.stress.mts",
|
|
59
61
|
"bench": "NODE_OPTIONS='--expose-gc --max-old-space-size=4096' npx tsx tests/benchmarks/index.ts"
|
|
60
62
|
}
|
|
61
63
|
}
|
package/src/BaseSource.ts
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
+
export interface BaseSourceOptions {
|
|
2
|
+
onFirstSubscribe?: () => void;
|
|
3
|
+
onLastUnsubscribe?: () => void;
|
|
4
|
+
onDestroy?: () => void;
|
|
5
|
+
}
|
|
6
|
+
|
|
1
7
|
export class BaseSource<T> {
|
|
2
8
|
#currentSnapshot: T;
|
|
3
9
|
#destroyed = false;
|
|
4
10
|
|
|
5
11
|
readonly #listeners = new Set<() => void>();
|
|
12
|
+
readonly #onFirstSubscribe: (() => void) | undefined;
|
|
13
|
+
readonly #onLastUnsubscribe: (() => void) | undefined;
|
|
14
|
+
readonly #onDestroy: (() => void) | undefined;
|
|
6
15
|
|
|
7
|
-
constructor(initialSnapshot: T) {
|
|
16
|
+
constructor(initialSnapshot: T, options?: BaseSourceOptions) {
|
|
8
17
|
this.#currentSnapshot = initialSnapshot;
|
|
18
|
+
this.#onFirstSubscribe = options?.onFirstSubscribe;
|
|
19
|
+
this.#onLastUnsubscribe = options?.onLastUnsubscribe;
|
|
20
|
+
this.#onDestroy = options?.onDestroy;
|
|
21
|
+
|
|
22
|
+
this.subscribe = this.subscribe.bind(this);
|
|
23
|
+
this.getSnapshot = this.getSnapshot.bind(this);
|
|
24
|
+
this.destroy = this.destroy.bind(this);
|
|
9
25
|
}
|
|
10
26
|
|
|
11
27
|
subscribe(listener: () => void): () => void {
|
|
@@ -13,10 +29,22 @@ export class BaseSource<T> {
|
|
|
13
29
|
return () => {};
|
|
14
30
|
}
|
|
15
31
|
|
|
32
|
+
if (this.#listeners.size === 0 && this.#onFirstSubscribe) {
|
|
33
|
+
this.#onFirstSubscribe();
|
|
34
|
+
}
|
|
35
|
+
|
|
16
36
|
this.#listeners.add(listener);
|
|
17
37
|
|
|
18
38
|
return () => {
|
|
19
39
|
this.#listeners.delete(listener);
|
|
40
|
+
|
|
41
|
+
if (
|
|
42
|
+
!this.#destroyed &&
|
|
43
|
+
this.#listeners.size === 0 &&
|
|
44
|
+
this.#onLastUnsubscribe
|
|
45
|
+
) {
|
|
46
|
+
this.#onLastUnsubscribe();
|
|
47
|
+
}
|
|
20
48
|
};
|
|
21
49
|
}
|
|
22
50
|
|
|
@@ -42,6 +70,7 @@ export class BaseSource<T> {
|
|
|
42
70
|
}
|
|
43
71
|
|
|
44
72
|
this.#destroyed = true;
|
|
73
|
+
this.#onDestroy?.();
|
|
45
74
|
this.#listeners.clear();
|
|
46
75
|
}
|
|
47
76
|
}
|
|
@@ -5,73 +5,49 @@ import { BaseSource } from "./BaseSource";
|
|
|
5
5
|
import type { ActiveRouteSourceOptions, RouterSource } from "./types.js";
|
|
6
6
|
import type { Params, Router } from "@real-router/core";
|
|
7
7
|
|
|
8
|
-
class ActiveRouteSource implements RouterSource<boolean> {
|
|
9
|
-
readonly #source: BaseSource<boolean>;
|
|
10
|
-
readonly #unsubscribe: () => void;
|
|
11
|
-
|
|
12
|
-
constructor(
|
|
13
|
-
router: Router,
|
|
14
|
-
routeName: string,
|
|
15
|
-
params?: Params,
|
|
16
|
-
options?: ActiveRouteSourceOptions,
|
|
17
|
-
) {
|
|
18
|
-
const strict = options?.strict ?? false;
|
|
19
|
-
const ignoreQueryParams = options?.ignoreQueryParams ?? true;
|
|
20
|
-
|
|
21
|
-
const initialValue = router.isActiveRoute(
|
|
22
|
-
routeName,
|
|
23
|
-
params,
|
|
24
|
-
strict,
|
|
25
|
-
ignoreQueryParams,
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
this.#source = new BaseSource(initialValue);
|
|
29
|
-
|
|
30
|
-
this.#unsubscribe = router.subscribe((next) => {
|
|
31
|
-
const isNewRelated = areRoutesRelated(routeName, next.route.name);
|
|
32
|
-
const isPrevRelated =
|
|
33
|
-
next.previousRoute &&
|
|
34
|
-
areRoutesRelated(routeName, next.previousRoute.name);
|
|
35
|
-
|
|
36
|
-
if (!isNewRelated && !isPrevRelated) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// If new route is not related, we know the route is inactive —
|
|
41
|
-
// avoid calling isActiveRoute for the optimization
|
|
42
|
-
const newValue = isNewRelated
|
|
43
|
-
? router.isActiveRoute(routeName, params, strict, ignoreQueryParams)
|
|
44
|
-
: false;
|
|
45
|
-
|
|
46
|
-
if (!Object.is(this.#source.getSnapshot(), newValue)) {
|
|
47
|
-
this.#source.updateSnapshot(newValue);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
this.subscribe = this.subscribe.bind(this);
|
|
52
|
-
this.getSnapshot = this.getSnapshot.bind(this);
|
|
53
|
-
this.destroy = this.destroy.bind(this);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
subscribe(listener: () => void): () => void {
|
|
57
|
-
return this.#source.subscribe(listener);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
getSnapshot(): boolean {
|
|
61
|
-
return this.#source.getSnapshot();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
destroy(): void {
|
|
65
|
-
this.#unsubscribe();
|
|
66
|
-
this.#source.destroy();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
8
|
export function createActiveRouteSource(
|
|
71
9
|
router: Router,
|
|
72
10
|
routeName: string,
|
|
73
11
|
params?: Params,
|
|
74
12
|
options?: ActiveRouteSourceOptions,
|
|
75
13
|
): RouterSource<boolean> {
|
|
76
|
-
|
|
14
|
+
const strict = options?.strict ?? false;
|
|
15
|
+
const ignoreQueryParams = options?.ignoreQueryParams ?? true;
|
|
16
|
+
|
|
17
|
+
const initialValue = router.isActiveRoute(
|
|
18
|
+
routeName,
|
|
19
|
+
params,
|
|
20
|
+
strict,
|
|
21
|
+
ignoreQueryParams,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const source = new BaseSource(initialValue, {
|
|
25
|
+
onDestroy: () => {
|
|
26
|
+
unsubscribe();
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Eager connection: subscribe to router immediately
|
|
31
|
+
const unsubscribe = router.subscribe((next) => {
|
|
32
|
+
const isNewRelated = areRoutesRelated(routeName, next.route.name);
|
|
33
|
+
const isPrevRelated =
|
|
34
|
+
next.previousRoute &&
|
|
35
|
+
areRoutesRelated(routeName, next.previousRoute.name);
|
|
36
|
+
|
|
37
|
+
if (!isNewRelated && !isPrevRelated) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// If new route is not related, we know the route is inactive —
|
|
42
|
+
// avoid calling isActiveRoute for the optimization
|
|
43
|
+
const newValue = isNewRelated
|
|
44
|
+
? router.isActiveRoute(routeName, params, strict, ignoreQueryParams)
|
|
45
|
+
: false;
|
|
46
|
+
|
|
47
|
+
if (!Object.is(source.getSnapshot(), newValue)) {
|
|
48
|
+
source.updateSnapshot(newValue);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return source;
|
|
77
53
|
}
|
|
@@ -1,106 +1,10 @@
|
|
|
1
|
+
import { BaseSource } from "./BaseSource";
|
|
1
2
|
import { computeSnapshot } from "./computeSnapshot.js";
|
|
2
3
|
import { getCachedShouldUpdate } from "./shouldUpdateCache.js";
|
|
3
4
|
|
|
4
5
|
import type { RouteNodeSnapshot, RouterSource } from "./types.js";
|
|
5
6
|
import type { Router } from "@real-router/core";
|
|
6
7
|
|
|
7
|
-
class RouteNodeSource implements RouterSource<RouteNodeSnapshot> {
|
|
8
|
-
#routerUnsubscribe: (() => void) | null = null;
|
|
9
|
-
#currentSnapshot: RouteNodeSnapshot;
|
|
10
|
-
#destroyed = false;
|
|
11
|
-
|
|
12
|
-
readonly #listeners = new Set<() => void>();
|
|
13
|
-
readonly #router: Router;
|
|
14
|
-
readonly #nodeName: string;
|
|
15
|
-
readonly #shouldUpdate: ReturnType<typeof getCachedShouldUpdate>;
|
|
16
|
-
|
|
17
|
-
constructor(router: Router, nodeName: string) {
|
|
18
|
-
this.#router = router;
|
|
19
|
-
this.#nodeName = nodeName;
|
|
20
|
-
this.#shouldUpdate = getCachedShouldUpdate(router, nodeName);
|
|
21
|
-
|
|
22
|
-
const initialSnapshot: RouteNodeSnapshot = {
|
|
23
|
-
route: undefined,
|
|
24
|
-
previousRoute: undefined,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
this.#currentSnapshot = computeSnapshot(initialSnapshot, router, nodeName);
|
|
28
|
-
|
|
29
|
-
this.subscribe = this.subscribe.bind(this);
|
|
30
|
-
this.getSnapshot = this.getSnapshot.bind(this);
|
|
31
|
-
this.destroy = this.destroy.bind(this);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
subscribe(listener: () => void): () => void {
|
|
35
|
-
if (this.#destroyed) {
|
|
36
|
-
return () => {};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (this.#listeners.size === 0) {
|
|
40
|
-
// Reconcile snapshot with current router state before connecting.
|
|
41
|
-
// Covers reconnection after Activity hide/show cycles where the
|
|
42
|
-
// source was disconnected and missed navigation events.
|
|
43
|
-
this.#currentSnapshot = computeSnapshot(
|
|
44
|
-
this.#currentSnapshot,
|
|
45
|
-
this.#router,
|
|
46
|
-
this.#nodeName,
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
// Connect to router on first subscription
|
|
50
|
-
this.#routerUnsubscribe = this.#router.subscribe((next) => {
|
|
51
|
-
if (!this.#shouldUpdate(next.route, next.previousRoute)) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const newSnapshot = computeSnapshot(
|
|
56
|
-
this.#currentSnapshot,
|
|
57
|
-
this.#router,
|
|
58
|
-
this.#nodeName,
|
|
59
|
-
next,
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
/* v8 ignore next 3 -- @preserve: dedup guard; shouldUpdateNode filters accurately so computeSnapshot always returns new ref */
|
|
63
|
-
if (!Object.is(this.#currentSnapshot, newSnapshot)) {
|
|
64
|
-
this.#currentSnapshot = newSnapshot;
|
|
65
|
-
this.#listeners.forEach((cb) => {
|
|
66
|
-
cb();
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
this.#listeners.add(listener);
|
|
73
|
-
|
|
74
|
-
return () => {
|
|
75
|
-
this.#listeners.delete(listener);
|
|
76
|
-
|
|
77
|
-
if (this.#listeners.size === 0 && this.#routerUnsubscribe) {
|
|
78
|
-
this.#routerUnsubscribe();
|
|
79
|
-
this.#routerUnsubscribe = null;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
getSnapshot(): RouteNodeSnapshot {
|
|
85
|
-
return this.#currentSnapshot;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
destroy(): void {
|
|
89
|
-
if (this.#destroyed) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
this.#destroyed = true;
|
|
94
|
-
|
|
95
|
-
if (this.#routerUnsubscribe) {
|
|
96
|
-
this.#routerUnsubscribe();
|
|
97
|
-
this.#routerUnsubscribe = null;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
this.#listeners.clear();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
8
|
/**
|
|
105
9
|
* Creates a source scoped to a specific route node.
|
|
106
10
|
*
|
|
@@ -112,5 +16,56 @@ export function createRouteNodeSource(
|
|
|
112
16
|
router: Router,
|
|
113
17
|
nodeName: string,
|
|
114
18
|
): RouterSource<RouteNodeSnapshot> {
|
|
115
|
-
|
|
19
|
+
let routerUnsubscribe: (() => void) | null = null;
|
|
20
|
+
|
|
21
|
+
const shouldUpdate = getCachedShouldUpdate(router, nodeName);
|
|
22
|
+
|
|
23
|
+
const initialSnapshot: RouteNodeSnapshot = {
|
|
24
|
+
route: undefined,
|
|
25
|
+
previousRoute: undefined,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const disconnect = (): void => {
|
|
29
|
+
const unsub = routerUnsubscribe;
|
|
30
|
+
|
|
31
|
+
routerUnsubscribe = null;
|
|
32
|
+
unsub?.();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const source = new BaseSource<RouteNodeSnapshot>(
|
|
36
|
+
computeSnapshot(initialSnapshot, router, nodeName),
|
|
37
|
+
{
|
|
38
|
+
onFirstSubscribe: () => {
|
|
39
|
+
// Reconcile snapshot with current router state before connecting.
|
|
40
|
+
// Covers reconnection after Activity hide/show cycles where the
|
|
41
|
+
// source was disconnected and missed navigation events.
|
|
42
|
+
source.updateSnapshot(
|
|
43
|
+
computeSnapshot(source.getSnapshot(), router, nodeName),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Connect to router on first subscription
|
|
47
|
+
routerUnsubscribe = router.subscribe((next) => {
|
|
48
|
+
if (!shouldUpdate(next.route, next.previousRoute)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const newSnapshot = computeSnapshot(
|
|
53
|
+
source.getSnapshot(),
|
|
54
|
+
router,
|
|
55
|
+
nodeName,
|
|
56
|
+
next,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
/* v8 ignore next 3 -- @preserve: dedup guard; shouldUpdateNode filters accurately so computeSnapshot always returns new ref */
|
|
60
|
+
if (!Object.is(source.getSnapshot(), newSnapshot)) {
|
|
61
|
+
source.updateSnapshot(newSnapshot);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
onLastUnsubscribe: disconnect,
|
|
66
|
+
onDestroy: disconnect,
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
return source;
|
|
116
71
|
}
|
package/src/createRouteSource.ts
CHANGED
|
@@ -1,66 +1,8 @@
|
|
|
1
|
+
import { BaseSource } from "./BaseSource";
|
|
2
|
+
|
|
1
3
|
import type { RouteSnapshot, RouterSource } from "./types.js";
|
|
2
4
|
import type { Router } from "@real-router/core";
|
|
3
5
|
|
|
4
|
-
class RouteSource implements RouterSource<RouteSnapshot> {
|
|
5
|
-
#routerUnsubscribe: (() => void) | null = null;
|
|
6
|
-
#currentSnapshot: RouteSnapshot;
|
|
7
|
-
|
|
8
|
-
readonly #listeners = new Set<() => void>();
|
|
9
|
-
readonly #router: Router;
|
|
10
|
-
|
|
11
|
-
constructor(router: Router) {
|
|
12
|
-
this.#router = router;
|
|
13
|
-
|
|
14
|
-
this.#currentSnapshot = {
|
|
15
|
-
route: router.getState(),
|
|
16
|
-
previousRoute: undefined,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
this.subscribe = this.subscribe.bind(this);
|
|
20
|
-
this.destroy = this.destroy.bind(this);
|
|
21
|
-
this.getSnapshot = this.getSnapshot.bind(this);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
subscribe(listener: () => void): () => void {
|
|
25
|
-
if (this.#listeners.size === 0) {
|
|
26
|
-
// Connect to router on first subscription
|
|
27
|
-
this.#routerUnsubscribe = this.#router.subscribe((next) => {
|
|
28
|
-
this.#currentSnapshot = {
|
|
29
|
-
route: next.route,
|
|
30
|
-
previousRoute: next.previousRoute,
|
|
31
|
-
};
|
|
32
|
-
this.#listeners.forEach((cb) => {
|
|
33
|
-
cb();
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
this.#listeners.add(listener);
|
|
39
|
-
|
|
40
|
-
return () => {
|
|
41
|
-
this.#listeners.delete(listener);
|
|
42
|
-
|
|
43
|
-
if (this.#listeners.size === 0 && this.#routerUnsubscribe) {
|
|
44
|
-
this.#routerUnsubscribe();
|
|
45
|
-
this.#routerUnsubscribe = null;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
getSnapshot(): RouteSnapshot {
|
|
51
|
-
return this.#currentSnapshot;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
destroy(): void {
|
|
55
|
-
if (this.#routerUnsubscribe) {
|
|
56
|
-
this.#routerUnsubscribe();
|
|
57
|
-
this.#routerUnsubscribe = null;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
this.#listeners.clear();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
6
|
/**
|
|
65
7
|
* Creates a source for the full route state.
|
|
66
8
|
*
|
|
@@ -69,5 +11,33 @@ class RouteSource implements RouterSource<RouteSnapshot> {
|
|
|
69
11
|
* This is compatible with React's useSyncExternalStore and Strict Mode.
|
|
70
12
|
*/
|
|
71
13
|
export function createRouteSource(router: Router): RouterSource<RouteSnapshot> {
|
|
72
|
-
|
|
14
|
+
let routerUnsubscribe: (() => void) | null = null;
|
|
15
|
+
|
|
16
|
+
const disconnect = (): void => {
|
|
17
|
+
const unsub = routerUnsubscribe;
|
|
18
|
+
|
|
19
|
+
routerUnsubscribe = null;
|
|
20
|
+
unsub?.();
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const source = new BaseSource<RouteSnapshot>(
|
|
24
|
+
{
|
|
25
|
+
route: router.getState(),
|
|
26
|
+
previousRoute: undefined,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
onFirstSubscribe: () => {
|
|
30
|
+
routerUnsubscribe = router.subscribe((next) => {
|
|
31
|
+
source.updateSnapshot({
|
|
32
|
+
route: next.route,
|
|
33
|
+
previousRoute: next.previousRoute,
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
onLastUnsubscribe: disconnect,
|
|
38
|
+
onDestroy: disconnect,
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return source;
|
|
73
43
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { events } from "@real-router/core";
|
|
2
|
+
import { getPluginApi } from "@real-router/core/api";
|
|
2
3
|
|
|
3
4
|
import { BaseSource } from "./BaseSource";
|
|
4
5
|
|
|
@@ -11,58 +12,39 @@ const IDLE_SNAPSHOT: RouterTransitionSnapshot = {
|
|
|
11
12
|
fromRoute: null,
|
|
12
13
|
};
|
|
13
14
|
|
|
14
|
-
class TransitionSource implements RouterSource<RouterTransitionSnapshot> {
|
|
15
|
-
readonly #source: BaseSource<RouterTransitionSnapshot>;
|
|
16
|
-
readonly #unsubs: (() => void)[];
|
|
17
|
-
|
|
18
|
-
constructor(router: Router) {
|
|
19
|
-
this.#source = new BaseSource(IDLE_SNAPSHOT);
|
|
20
|
-
|
|
21
|
-
const api = getPluginApi(router);
|
|
22
|
-
|
|
23
|
-
const resetToIdle = (): void => {
|
|
24
|
-
this.#source.updateSnapshot(IDLE_SNAPSHOT);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
this.#unsubs = [
|
|
28
|
-
api.addEventListener(
|
|
29
|
-
events.TRANSITION_START,
|
|
30
|
-
(toState: State, fromState?: State) => {
|
|
31
|
-
this.#source.updateSnapshot({
|
|
32
|
-
isTransitioning: true,
|
|
33
|
-
toRoute: toState,
|
|
34
|
-
fromRoute: fromState ?? null,
|
|
35
|
-
});
|
|
36
|
-
},
|
|
37
|
-
),
|
|
38
|
-
api.addEventListener(events.TRANSITION_SUCCESS, resetToIdle),
|
|
39
|
-
api.addEventListener(events.TRANSITION_ERROR, resetToIdle),
|
|
40
|
-
api.addEventListener(events.TRANSITION_CANCEL, resetToIdle),
|
|
41
|
-
];
|
|
42
|
-
|
|
43
|
-
this.subscribe = this.subscribe.bind(this);
|
|
44
|
-
this.getSnapshot = this.getSnapshot.bind(this);
|
|
45
|
-
this.destroy = this.destroy.bind(this);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
subscribe(listener: () => void): () => void {
|
|
49
|
-
return this.#source.subscribe(listener);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
getSnapshot(): RouterTransitionSnapshot {
|
|
53
|
-
return this.#source.getSnapshot();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
destroy(): void {
|
|
57
|
-
this.#unsubs.forEach((u) => {
|
|
58
|
-
u();
|
|
59
|
-
});
|
|
60
|
-
this.#source.destroy();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
15
|
export function createTransitionSource(
|
|
65
16
|
router: Router,
|
|
66
17
|
): RouterSource<RouterTransitionSnapshot> {
|
|
67
|
-
|
|
18
|
+
const source = new BaseSource(IDLE_SNAPSHOT, {
|
|
19
|
+
onDestroy: () => {
|
|
20
|
+
unsubs.forEach((u) => {
|
|
21
|
+
u();
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const api = getPluginApi(router);
|
|
27
|
+
|
|
28
|
+
const resetToIdle = (): void => {
|
|
29
|
+
source.updateSnapshot(IDLE_SNAPSHOT);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Eager connection: subscribe to router events immediately
|
|
33
|
+
const unsubs = [
|
|
34
|
+
api.addEventListener(
|
|
35
|
+
events.TRANSITION_START,
|
|
36
|
+
(toState: State, fromState?: State) => {
|
|
37
|
+
source.updateSnapshot({
|
|
38
|
+
isTransitioning: true,
|
|
39
|
+
toRoute: toState,
|
|
40
|
+
fromRoute: fromState ?? null,
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
),
|
|
44
|
+
api.addEventListener(events.TRANSITION_SUCCESS, resetToIdle),
|
|
45
|
+
api.addEventListener(events.TRANSITION_ERROR, resetToIdle),
|
|
46
|
+
api.addEventListener(events.TRANSITION_CANCEL, resetToIdle),
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
return source;
|
|
68
50
|
}
|