@real-router/lifecycle-plugin 0.1.2 → 0.1.4
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 -0
- package/dist/esm/index.d.mts.map +1 -0
- package/package.json +7 -5
- package/src/factory.ts +47 -0
- package/src/index.ts +20 -0
- package/src/types.ts +10 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;;;AAMA;;KAAY,aAAA,IACV,OAAA,EAAS,KAAA,EACT,SAAA,EAAW,KAAA;;;iBCoCG,sBAAA,CAAA,GAA0B,aAAA;;;;ADtC1C;;;;YEGY,KAAA;IFFV;IEIE,OAAA,GAAU,aAAA;IFHZ;IEKE,MAAA,GAAS,aAAA;IFLiB;IEO1B,OAAA,GAAU,aAAA;EAAA;AAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/types.ts","../../src/factory.ts","../../src/index.ts"],"mappings":";;;;;AAMA;;KAAY,aAAA,IACV,OAAA,EAAS,KAAA,EACT,SAAA,EAAW,KAAA;;;iBCoCG,sBAAA,CAAA,GAA0B,aAAA;;;;ADtC1C;;;;YEGY,KAAA;IFFV;IEIE,OAAA,GAAU,aAAA;IFHZ;IEKE,MAAA,GAAS,aAAA;IFLiB;IEO1B,OAAA,GAAU,aAAA;EAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/lifecycle-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "Route-level lifecycle hooks: onEnter, onStay, onLeave",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
"types": "./dist/esm/index.d.mts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"development": "./src/index.ts",
|
|
12
11
|
"types": {
|
|
13
12
|
"import": "./dist/esm/index.d.mts",
|
|
14
13
|
"require": "./dist/cjs/index.d.ts"
|
|
@@ -18,7 +17,8 @@
|
|
|
18
17
|
}
|
|
19
18
|
},
|
|
20
19
|
"files": [
|
|
21
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
22
|
],
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -44,14 +44,16 @@
|
|
|
44
44
|
"homepage": "https://github.com/greydragon888/real-router",
|
|
45
45
|
"sideEffects": false,
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@real-router/core": "^0.
|
|
47
|
+
"@real-router/core": "^0.46.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsdown --config-loader unrun",
|
|
51
51
|
"test": "vitest run",
|
|
52
|
+
"test:properties": "vitest run --config vitest.config.properties.mts",
|
|
53
|
+
"test:stress": "vitest --config vitest.config.stress.mts --run",
|
|
52
54
|
"type-check": "tsc --noEmit",
|
|
53
55
|
"lint": "eslint --cache --ext .ts src/ tests/ --fix --max-warnings 0",
|
|
54
|
-
"lint:package": "
|
|
56
|
+
"lint:package": "publint",
|
|
55
57
|
"lint:types": "attw --pack .",
|
|
56
58
|
"build:dist-only": "tsdown --config-loader unrun"
|
|
57
59
|
}
|
package/src/factory.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { getPluginApi } from "@real-router/core/api";
|
|
2
|
+
|
|
3
|
+
import type { LifecycleHook } from "./types";
|
|
4
|
+
import type { PluginFactory, State } from "@real-router/core";
|
|
5
|
+
import type { PluginApi } from "@real-router/core/api";
|
|
6
|
+
|
|
7
|
+
function createInvokeHook(api: PluginApi) {
|
|
8
|
+
return (
|
|
9
|
+
hookName: "onEnter" | "onStay" | "onLeave",
|
|
10
|
+
routeName: string,
|
|
11
|
+
toState: State,
|
|
12
|
+
fromState: State | undefined,
|
|
13
|
+
): void => {
|
|
14
|
+
const hook = api.getRouteConfig(routeName)?.[hookName];
|
|
15
|
+
|
|
16
|
+
if (typeof hook === "function") {
|
|
17
|
+
(hook as LifecycleHook)(toState, fromState);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function createPlugin(router: Parameters<PluginFactory>[0]) {
|
|
23
|
+
const invokeHook = createInvokeHook(getPluginApi(router));
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
onTransitionLeaveApprove: (
|
|
27
|
+
toState: State,
|
|
28
|
+
fromState: State | undefined,
|
|
29
|
+
) => {
|
|
30
|
+
if (fromState && toState.name !== fromState.name) {
|
|
31
|
+
invokeHook("onLeave", fromState.name, toState, fromState);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
onTransitionSuccess: (toState: State, fromState: State | undefined) => {
|
|
36
|
+
if (toState.name === fromState?.name) {
|
|
37
|
+
invokeHook("onStay", toState.name, toState, fromState);
|
|
38
|
+
} else {
|
|
39
|
+
invokeHook("onEnter", toState.name, toState, fromState);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function lifecyclePluginFactory(): PluginFactory {
|
|
46
|
+
return createPlugin;
|
|
47
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { LifecycleHook } from "./types";
|
|
2
|
+
|
|
3
|
+
export { lifecyclePluginFactory } from "./factory";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Module augmentation for real-router.
|
|
7
|
+
* Extends Route interface with lifecycle hooks.
|
|
8
|
+
*/
|
|
9
|
+
declare module "@real-router/core" {
|
|
10
|
+
interface Route {
|
|
11
|
+
/** Called when this route segment is newly activated (entered). */
|
|
12
|
+
onEnter?: LifecycleHook;
|
|
13
|
+
/** Called when this route segment stays active but params changed. */
|
|
14
|
+
onStay?: LifecycleHook;
|
|
15
|
+
/** Called when this route segment is deactivated (left). */
|
|
16
|
+
onLeave?: LifecycleHook;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type { LifecycleHook } from "./types";
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { State } from "@real-router/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Lifecycle hook callback for route transitions.
|
|
5
|
+
* Fire-and-forget: return values are ignored, errors are caught and warned.
|
|
6
|
+
*/
|
|
7
|
+
export type LifecycleHook = (
|
|
8
|
+
toState: State,
|
|
9
|
+
fromState: State | undefined,
|
|
10
|
+
) => void;
|