@real-router/lifecycle-plugin 0.4.7 → 0.5.0
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.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.mts.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/factory.ts +46 -2
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;;;;AAOA;;KAAY,aAAA,IACV,OAAA,EAAS,KAAA,EACT,SAAA,EAAW,KAAK;;;;;;KAQN,oBAAA,sBACW,qBAAA,GAAsB,qBAAA,KAE3C,MAAA,EAAQ,MAAA,CAAO,YAAA,GACf,aAAA,mBAAgC,YAAA,EAAc,GAAA,EAAK,CAAA,KAAM,YAAA,CAAa,CAAA,MACnE,aAAA;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;;;;AAOA;;KAAY,aAAA,IACV,OAAA,EAAS,KAAA,EACT,SAAA,EAAW,KAAK;;;;;;KAQN,oBAAA,sBACW,qBAAA,GAAsB,qBAAA,KAE3C,MAAA,EAAQ,MAAA,CAAO,YAAA,GACf,aAAA,mBAAgC,YAAA,EAAc,GAAA,EAAK,CAAA,KAAM,YAAA,CAAa,CAAA,MACnE,aAAA;;;iBCyFW,sBAAA,IAA0B,aAAa;;;;ADxGvD;;;;YEGY,KAAA,sBAA2B,mBAAA;IFFrC;IEIE,OAAA,GAAU,oBAAA,CAAqB,YAAA;IFHjC;IEKE,MAAA,GAAS,oBAAA,CAAqB,YAAA;IFLJ;IEO1B,OAAA,GAAU,oBAAA,CAAqB,YAAA;IFCH;;;;;IEK5B,UAAA,GAAa,oBAAA,CAAqB,YAAA;EAAA;AAAA"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@real-router/core/api");function
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@real-router/core/api");const t=[`onEnter`,`onStay`,`onLeave`,`onNavigate`];function n(...n){let[r,i]=n,a=(0,e.getPluginApi)(r),o=new Map,s=e=>{for(let n of t)o.delete(`${n}:${e}`)},c=(0,e.getRoutesApi)(r).subscribeChanges(e=>{switch(e.op){case`remove`:for(let t of e.removedSubtree)s(t.name);break;case`replace`:for(let t of e.removed)s(t.name);break;case`clear`:o.clear();break}});function l(e,t){let n=`${e}:${t}`,s=a.getRouteConfig(t),c=typeof s?.[e]==`function`?s[e]:void 0;if(!c){o.delete(n);return}let l=o.get(n);if(l?.factory===c)return l.hook;let u=c(r,i);return o.set(n,{hook:u,factory:c}),u}return{onTransitionLeaveApprove:(e,t)=>{t&&e.name!==t.name&&l(`onLeave`,t.name)?.(e,t)},onTransitionSuccess:(e,t)=>{e.name===t?.name?l(`onStay`,e.name)?.(e,t):l(`onEnter`,e.name)?.(e,t),l(`onNavigate`,e.name)?.(e,t)},teardown:()=>{c()}}}function r(){return n}exports.lifecyclePluginFactory=r;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/factory.ts"],"sourcesContent":["import { getPluginApi } from \"@real-router/core/api\";\n\nimport type { LifecycleHook, LifecycleHookFactory } from \"./types\";\nimport type { PluginFactory, State } from \"@real-router/core\";\n\nfunction createPlugin(\n ...args: Parameters<PluginFactory>\n): ReturnType<PluginFactory> {\n const [router, getDependency] = args;\n const api = getPluginApi(router);\n const compiledHooks = new Map<\n string,\n { hook: LifecycleHook; factory: LifecycleHookFactory }\n >();\n\n function compileHook(\n hookName: \"onEnter\" | \"onStay\" | \"onLeave\" | \"onNavigate\",\n routeName: string,\n ): LifecycleHook | undefined {\n const key = `${hookName}:${routeName}`;\n const config = api.getRouteConfig(routeName);\n const factory =\n typeof config?.[hookName] === \"function\"\n ? (config[hookName] as LifecycleHookFactory)\n : undefined;\n\n if (!factory) {\n compiledHooks.delete(key);\n\n return undefined;\n }\n\n const cached = compiledHooks.get(key);\n\n if (cached?.factory === factory) {\n return cached.hook;\n }\n\n const hook = factory(router, getDependency);\n\n compiledHooks.set(key, { hook, factory });\n\n return hook;\n }\n\n return {\n onTransitionLeaveApprove: (\n toState: State,\n fromState: State | undefined,\n ) => {\n if (fromState && toState.name !== fromState.name) {\n compileHook(\"onLeave\", fromState.name)?.(toState, fromState);\n }\n },\n\n onTransitionSuccess: (toState: State, fromState: State | undefined) => {\n if (toState.name === fromState?.name) {\n compileHook(\"onStay\", toState.name)?.(toState, fromState);\n } else {\n compileHook(\"onEnter\", toState.name)?.(toState, fromState);\n }\n\n compileHook(\"onNavigate\", toState.name)?.(toState, fromState);\n },\n };\n}\n\nexport function lifecyclePluginFactory(): PluginFactory {\n return createPlugin;\n}\n"],"mappings":"0GAKA,SAAS,EACP,GAAG,EACwB,CAC3B,GAAM,CAAC,EAAQ,GAAiB,EAC1B,GAAA,EAAA,EAAA,
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/factory.ts"],"sourcesContent":["import { getPluginApi, getRoutesApi } from \"@real-router/core/api\";\n\nimport type { LifecycleHook, LifecycleHookFactory } from \"./types\";\nimport type { PluginFactory, State, TreeChangedEvent } from \"@real-router/core\";\n\nconst HOOK_NAMES = [\"onEnter\", \"onStay\", \"onLeave\", \"onNavigate\"] as const;\n\nfunction createPlugin(\n ...args: Parameters<PluginFactory>\n): ReturnType<PluginFactory> {\n const [router, getDependency] = args;\n const api = getPluginApi(router);\n const compiledHooks = new Map<\n string,\n { hook: LifecycleHook; factory: LifecycleHookFactory }\n >();\n\n // Drop compiled hooks for routes removed from the tree. `compileHook` can\n // never reach them again (no navigation to a removed route), so the entries\n // would be dead memory until teardown. `add`/`update` need no handling —\n // `compileHook` already revalidates lazily via the cached `factory` reference.\n const forgetRoute = (routeName: string): void => {\n for (const hookName of HOOK_NAMES) {\n compiledHooks.delete(`${hookName}:${routeName}`);\n }\n };\n\n const onTreeChanged = (event: TreeChangedEvent): void => {\n switch (event.op) {\n case \"remove\": {\n for (const route of event.removedSubtree) {\n forgetRoute(route.name);\n }\n\n break;\n }\n case \"replace\": {\n for (const route of event.removed) {\n forgetRoute(route.name);\n }\n\n break;\n }\n case \"clear\": {\n compiledHooks.clear();\n\n break;\n }\n // \"add\" / \"update\": lazy factory-reference revalidation handles these.\n }\n };\n\n const removeChangesSubscription =\n getRoutesApi(router).subscribeChanges(onTreeChanged);\n\n function compileHook(\n hookName: \"onEnter\" | \"onStay\" | \"onLeave\" | \"onNavigate\",\n routeName: string,\n ): LifecycleHook | undefined {\n const key = `${hookName}:${routeName}`;\n const config = api.getRouteConfig(routeName);\n const factory =\n typeof config?.[hookName] === \"function\"\n ? (config[hookName] as LifecycleHookFactory)\n : undefined;\n\n if (!factory) {\n compiledHooks.delete(key);\n\n return undefined;\n }\n\n const cached = compiledHooks.get(key);\n\n if (cached?.factory === factory) {\n return cached.hook;\n }\n\n const hook = factory(router, getDependency);\n\n compiledHooks.set(key, { hook, factory });\n\n return hook;\n }\n\n return {\n onTransitionLeaveApprove: (\n toState: State,\n fromState: State | undefined,\n ) => {\n if (fromState && toState.name !== fromState.name) {\n compileHook(\"onLeave\", fromState.name)?.(toState, fromState);\n }\n },\n\n onTransitionSuccess: (toState: State, fromState: State | undefined) => {\n if (toState.name === fromState?.name) {\n compileHook(\"onStay\", toState.name)?.(toState, fromState);\n } else {\n compileHook(\"onEnter\", toState.name)?.(toState, fromState);\n }\n\n compileHook(\"onNavigate\", toState.name)?.(toState, fromState);\n },\n\n teardown: () => {\n removeChang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AAU,EAAQ,IAAI,CAAC,GAAG,EAAS,CAAS,EAExD,EAAY,UAAW,EAAQ,IAAI,CAAC,GAAG,EAAS,CAAS,EAG3D,EAAY,aAAc,EAAQ,IAAI,CAAC,GAAG,EAAS,CAAS,CAC9D,EAEA,aAAgB,CACd,EAA0B,CAC5B,CACF,CACF,CAEA,SAAgB,GAAwC,CACtD,OAAO,CACT"}
|
package/dist/esm/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;;;;AAOA;;KAAY,aAAA,IACV,OAAA,EAAS,KAAA,EACT,SAAA,EAAW,KAAK;;;;;;KAQN,oBAAA,sBACW,qBAAA,GAAsB,qBAAA,KAE3C,MAAA,EAAQ,MAAA,CAAO,YAAA,GACf,aAAA,mBAAgC,YAAA,EAAc,GAAA,EAAK,CAAA,KAAM,YAAA,CAAa,CAAA,MACnE,aAAA;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;;;;AAOA;;KAAY,aAAA,IACV,OAAA,EAAS,KAAA,EACT,SAAA,EAAW,KAAK;;;;;;KAQN,oBAAA,sBACW,qBAAA,GAAsB,qBAAA,KAE3C,MAAA,EAAQ,MAAA,CAAO,YAAA,GACf,aAAA,mBAAgC,YAAA,EAAc,GAAA,EAAK,CAAA,KAAM,YAAA,CAAa,CAAA,MACnE,aAAA;;;iBCyFW,sBAAA,IAA0B,aAAa;;;;ADxGvD;;;;YEGY,KAAA,sBAA2B,mBAAA;IFFrC;IEIE,OAAA,GAAU,oBAAA,CAAqB,YAAA;IFHjC;IEKE,MAAA,GAAS,oBAAA,CAAqB,YAAA;IFLJ;IEO1B,OAAA,GAAU,oBAAA,CAAqB,YAAA;IFCH;;;;;IEK5B,UAAA,GAAa,oBAAA,CAAqB,YAAA;EAAA;AAAA"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{getPluginApi as e}from"@real-router/core/api";function
|
|
1
|
+
import{getPluginApi as e,getRoutesApi as t}from"@real-router/core/api";const n=[`onEnter`,`onStay`,`onLeave`,`onNavigate`];function r(...r){let[i,a]=r,o=e(i),s=new Map,c=e=>{for(let t of n)s.delete(`${t}:${e}`)},l=t(i).subscribeChanges(e=>{switch(e.op){case`remove`:for(let t of e.removedSubtree)c(t.name);break;case`replace`:for(let t of e.removed)c(t.name);break;case`clear`:s.clear();break}});function u(e,t){let n=`${e}:${t}`,r=o.getRouteConfig(t),c=typeof r?.[e]==`function`?r[e]:void 0;if(!c){s.delete(n);return}let l=s.get(n);if(l?.factory===c)return l.hook;let u=c(i,a);return s.set(n,{hook:u,factory:c}),u}return{onTransitionLeaveApprove:(e,t)=>{t&&e.name!==t.name&&u(`onLeave`,t.name)?.(e,t)},onTransitionSuccess:(e,t)=>{e.name===t?.name?u(`onStay`,e.name)?.(e,t):u(`onEnter`,e.name)?.(e,t),u(`onNavigate`,e.name)?.(e,t)},teardown:()=>{l()}}}function i(){return r}export{i as lifecyclePluginFactory};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/factory.ts"],"sourcesContent":["import { getPluginApi } from \"@real-router/core/api\";\n\nimport type { LifecycleHook, LifecycleHookFactory } from \"./types\";\nimport type { PluginFactory, State } from \"@real-router/core\";\n\nfunction createPlugin(\n ...args: Parameters<PluginFactory>\n): ReturnType<PluginFactory> {\n const [router, getDependency] = args;\n const api = getPluginApi(router);\n const compiledHooks = new Map<\n string,\n { hook: LifecycleHook; factory: LifecycleHookFactory }\n >();\n\n function compileHook(\n hookName: \"onEnter\" | \"onStay\" | \"onLeave\" | \"onNavigate\",\n routeName: string,\n ): LifecycleHook | undefined {\n const key = `${hookName}:${routeName}`;\n const config = api.getRouteConfig(routeName);\n const factory =\n typeof config?.[hookName] === \"function\"\n ? (config[hookName] as LifecycleHookFactory)\n : undefined;\n\n if (!factory) {\n compiledHooks.delete(key);\n\n return undefined;\n }\n\n const cached = compiledHooks.get(key);\n\n if (cached?.factory === factory) {\n return cached.hook;\n }\n\n const hook = factory(router, getDependency);\n\n compiledHooks.set(key, { hook, factory });\n\n return hook;\n }\n\n return {\n onTransitionLeaveApprove: (\n toState: State,\n fromState: State | undefined,\n ) => {\n if (fromState && toState.name !== fromState.name) {\n compileHook(\"onLeave\", fromState.name)?.(toState, fromState);\n }\n },\n\n onTransitionSuccess: (toState: State, fromState: State | undefined) => {\n if (toState.name === fromState?.name) {\n compileHook(\"onStay\", toState.name)?.(toState, fromState);\n } else {\n compileHook(\"onEnter\", toState.name)?.(toState, fromState);\n }\n\n compileHook(\"onNavigate\", toState.name)?.(toState, fromState);\n },\n };\n}\n\nexport function lifecyclePluginFactory(): PluginFactory {\n return createPlugin;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/factory.ts"],"sourcesContent":["import { getPluginApi, getRoutesApi } from \"@real-router/core/api\";\n\nimport type { LifecycleHook, LifecycleHookFactory } from \"./types\";\nimport type { PluginFactory, State, TreeChangedEvent } from \"@real-router/core\";\n\nconst HOOK_NAMES = [\"onEnter\", \"onStay\", \"onLeave\", \"onNavigate\"] as const;\n\nfunction createPlugin(\n ...args: Parameters<PluginFactory>\n): ReturnType<PluginFactory> {\n const [router, getDependency] = args;\n const api = getPluginApi(router);\n const compiledHooks = new Map<\n string,\n { hook: LifecycleHook; factory: LifecycleHookFactory }\n >();\n\n // Drop compiled hooks for routes removed from the tree. `compileHook` can\n // never reach them again (no navigation to a removed route), so the entries\n // would be dead memory until teardown. `add`/`update` need no handling —\n // `compileHook` already revalidates lazily via the cached `factory` reference.\n const forgetRoute = (routeName: string): void => {\n for (const hookName of HOOK_NAMES) {\n compiledHooks.delete(`${hookName}:${routeName}`);\n }\n };\n\n const onTreeChanged = (event: TreeChangedEvent): void => {\n switch (event.op) {\n case \"remove\": {\n for (const route of event.removedSubtree) {\n forgetRoute(route.name);\n }\n\n break;\n }\n case \"replace\": {\n for (const route of event.removed) {\n forgetRoute(route.name);\n }\n\n break;\n }\n case \"clear\": {\n compiledHooks.clear();\n\n break;\n }\n // \"add\" / \"update\": lazy factory-reference revalidation handles these.\n }\n };\n\n const removeChangesSubscription =\n getRoutesApi(router).subscribeChanges(onTreeChanged);\n\n function compileHook(\n hookName: \"onEnter\" | \"onStay\" | \"onLeave\" | \"onNavigate\",\n routeName: string,\n ): LifecycleHook | undefined {\n const key = `${hookName}:${routeName}`;\n const config = api.getRouteConfig(routeName);\n const factory =\n typeof config?.[hookName] === \"function\"\n ? (config[hookName] as LifecycleHookFactory)\n : undefined;\n\n if (!factory) {\n compiledHooks.delete(key);\n\n return undefined;\n }\n\n const cached = compiledHooks.get(key);\n\n if (cached?.factory === factory) {\n return cached.hook;\n }\n\n const hook = factory(router, getDependency);\n\n compiledHooks.set(key, { hook, factory });\n\n return hook;\n }\n\n return {\n onTransitionLeaveApprove: (\n toState: State,\n fromState: State | undefined,\n ) => {\n if (fromState && toState.name !== fromState.name) {\n compileHook(\"onLeave\", fromState.name)?.(toState, fromState);\n }\n },\n\n onTransitionSuccess: (toState: State, fromState: State | undefined) => {\n if (toState.name === fromState?.name) {\n compileHook(\"onStay\", toState.name)?.(toState, fromState);\n } else {\n compileHook(\"onEnter\", toState.name)?.(toState, fromState);\n }\n\n compileHook(\"onNavigate\", toState.name)?.(toState, fromState);\n },\n\n teardown: () => {\n removeChangesSubscription();\n },\n };\n}\n\nexport function lifecyclePluginFactory(): PluginFactory {\n return createPlugin;\n}\n"],"mappings":"uEAKA,MAAM,EAAa,CAAC,UAAW,SAAU,UAAW,YAAY,EAEhE,SAAS,EACP,GAAG,EACwB,CAC3B,GAAM,CAAC,EAAQ,GAAiB,EAC1B,EAAM,EAAa,CAAM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json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/lifecycle-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "Route-level lifecycle hooks: onEnter, onStay, onLeave",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"homepage": "https://github.com/greydragon888/real-router",
|
|
46
46
|
"sideEffects": false,
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@real-router/core": "^0.
|
|
49
|
-
"@real-router/types": "^0.
|
|
48
|
+
"@real-router/core": "^0.56.0",
|
|
49
|
+
"@real-router/types": "^0.36.0"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"test": "vitest run",
|
package/src/factory.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { getPluginApi } from "@real-router/core/api";
|
|
1
|
+
import { getPluginApi, getRoutesApi } from "@real-router/core/api";
|
|
2
2
|
|
|
3
3
|
import type { LifecycleHook, LifecycleHookFactory } from "./types";
|
|
4
|
-
import type { PluginFactory, State } from "@real-router/core";
|
|
4
|
+
import type { PluginFactory, State, TreeChangedEvent } from "@real-router/core";
|
|
5
|
+
|
|
6
|
+
const HOOK_NAMES = ["onEnter", "onStay", "onLeave", "onNavigate"] as const;
|
|
5
7
|
|
|
6
8
|
function createPlugin(
|
|
7
9
|
...args: Parameters<PluginFactory>
|
|
@@ -13,6 +15,44 @@ function createPlugin(
|
|
|
13
15
|
{ hook: LifecycleHook; factory: LifecycleHookFactory }
|
|
14
16
|
>();
|
|
15
17
|
|
|
18
|
+
// Drop compiled hooks for routes removed from the tree. `compileHook` can
|
|
19
|
+
// never reach them again (no navigation to a removed route), so the entries
|
|
20
|
+
// would be dead memory until teardown. `add`/`update` need no handling —
|
|
21
|
+
// `compileHook` already revalidates lazily via the cached `factory` reference.
|
|
22
|
+
const forgetRoute = (routeName: string): void => {
|
|
23
|
+
for (const hookName of HOOK_NAMES) {
|
|
24
|
+
compiledHooks.delete(`${hookName}:${routeName}`);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const onTreeChanged = (event: TreeChangedEvent): void => {
|
|
29
|
+
switch (event.op) {
|
|
30
|
+
case "remove": {
|
|
31
|
+
for (const route of event.removedSubtree) {
|
|
32
|
+
forgetRoute(route.name);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
case "replace": {
|
|
38
|
+
for (const route of event.removed) {
|
|
39
|
+
forgetRoute(route.name);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case "clear": {
|
|
45
|
+
compiledHooks.clear();
|
|
46
|
+
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
// "add" / "update": lazy factory-reference revalidation handles these.
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const removeChangesSubscription =
|
|
54
|
+
getRoutesApi(router).subscribeChanges(onTreeChanged);
|
|
55
|
+
|
|
16
56
|
function compileHook(
|
|
17
57
|
hookName: "onEnter" | "onStay" | "onLeave" | "onNavigate",
|
|
18
58
|
routeName: string,
|
|
@@ -62,6 +102,10 @@ function createPlugin(
|
|
|
62
102
|
|
|
63
103
|
compileHook("onNavigate", toState.name)?.(toState, fromState);
|
|
64
104
|
},
|
|
105
|
+
|
|
106
|
+
teardown: () => {
|
|
107
|
+
removeChangesSubscription();
|
|
108
|
+
},
|
|
65
109
|
};
|
|
66
110
|
}
|
|
67
111
|
|