@rpcbase/router 0.61.0 → 0.63.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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +64 -2
- package/dist/navigationGuards.d.ts +18 -0
- package/dist/navigationGuards.d.ts.map +1 -0
- package/package.json +4 -2
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,oBAAoB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useLocation } from "react-router";
|
|
2
2
|
export * from "react-router";
|
|
3
|
-
import { lazy, useEffect } from "react";
|
|
3
|
+
import { lazy, useEffect, useRef } from "react";
|
|
4
4
|
const loadRoute = (importPromise) => {
|
|
5
5
|
const Component = lazy(async () => {
|
|
6
6
|
const module = await importPromise;
|
|
@@ -51,7 +51,69 @@ const useApplyMeta = ({
|
|
|
51
51
|
loadMeta();
|
|
52
52
|
}, [location.pathname, defaultTitle, defaultMeta, pagesMeta]);
|
|
53
53
|
};
|
|
54
|
+
const guards = /* @__PURE__ */ new Map();
|
|
55
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
56
|
+
const notify = () => {
|
|
57
|
+
listeners.forEach((listener) => {
|
|
58
|
+
listener();
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const isSameGuard = (a, b) => a.id === b.id && a.enabled === b.enabled && a.priority === b.priority && a.message === b.message && a.blockOnSearch === b.blockOnSearch && a.shouldBlockNavigation === b.shouldBlockNavigation && a.shouldBlockUnload === b.shouldBlockUnload;
|
|
62
|
+
const upsertNavigationGuard = (guard) => {
|
|
63
|
+
const prev = guards.get(guard.id);
|
|
64
|
+
if (prev && isSameGuard(prev, guard)) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
guards.set(guard.id, guard);
|
|
68
|
+
notify();
|
|
69
|
+
};
|
|
70
|
+
const removeNavigationGuard = (id) => {
|
|
71
|
+
const didDelete = guards.delete(id);
|
|
72
|
+
if (didDelete) {
|
|
73
|
+
notify();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const getNavigationGuards = () => Array.from(guards.values());
|
|
77
|
+
const subscribeNavigationGuards = (listener) => {
|
|
78
|
+
listeners.add(listener);
|
|
79
|
+
return () => {
|
|
80
|
+
listeners.delete(listener);
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const useRegisterNavigationGuard = (guard) => {
|
|
84
|
+
const latestGuardRef = useRef(guard);
|
|
85
|
+
latestGuardRef.current = guard;
|
|
86
|
+
const lastIdRef = useRef(null);
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
const lastId = lastIdRef.current;
|
|
89
|
+
if (lastId && lastId !== guard.id) {
|
|
90
|
+
removeNavigationGuard(lastId);
|
|
91
|
+
}
|
|
92
|
+
lastIdRef.current = guard.id;
|
|
93
|
+
if (!guard.enabled) {
|
|
94
|
+
removeNavigationGuard(guard.id);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
upsertNavigationGuard(latestGuardRef.current);
|
|
98
|
+
return () => {
|
|
99
|
+
removeNavigationGuard(guard.id);
|
|
100
|
+
};
|
|
101
|
+
}, [
|
|
102
|
+
guard.id,
|
|
103
|
+
guard.enabled,
|
|
104
|
+
guard.message,
|
|
105
|
+
guard.priority,
|
|
106
|
+
guard.blockOnSearch,
|
|
107
|
+
guard.shouldBlockNavigation,
|
|
108
|
+
guard.shouldBlockUnload
|
|
109
|
+
]);
|
|
110
|
+
};
|
|
54
111
|
export {
|
|
112
|
+
getNavigationGuards,
|
|
55
113
|
loadRoute,
|
|
56
|
-
|
|
114
|
+
removeNavigationGuard,
|
|
115
|
+
subscribeNavigationGuards,
|
|
116
|
+
upsertNavigationGuard,
|
|
117
|
+
useApplyMeta,
|
|
118
|
+
useRegisterNavigationGuard
|
|
57
119
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BlockerFunction } from 'react-router';
|
|
2
|
+
export type NavigationGuard = {
|
|
3
|
+
id: string;
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
priority?: number;
|
|
6
|
+
message: string;
|
|
7
|
+
blockOnSearch?: boolean;
|
|
8
|
+
shouldBlockNavigation: BlockerFunction;
|
|
9
|
+
shouldBlockUnload: boolean;
|
|
10
|
+
};
|
|
11
|
+
type NavigationGuardsListener = () => void;
|
|
12
|
+
export declare const upsertNavigationGuard: (guard: NavigationGuard) => void;
|
|
13
|
+
export declare const removeNavigationGuard: (id: string) => void;
|
|
14
|
+
export declare const getNavigationGuards: () => NavigationGuard[];
|
|
15
|
+
export declare const subscribeNavigationGuards: (listener: NavigationGuardsListener) => (() => void);
|
|
16
|
+
export declare const useRegisterNavigationGuard: (guard: NavigationGuard) => void;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=navigationGuards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigationGuards.d.ts","sourceRoot":"","sources":["../src/navigationGuards.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAGnD,MAAM,MAAM,eAAe,GAAG;IAC5B,EAAE,EAAE,MAAM,CAAA;IACV,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,MAAM,CAAA;IACf,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,qBAAqB,EAAE,eAAe,CAAA;IACtC,iBAAiB,EAAE,OAAO,CAAA;CAC3B,CAAA;AAED,KAAK,wBAAwB,GAAG,MAAM,IAAI,CAAA;AAoB1C,eAAO,MAAM,qBAAqB,GAAI,OAAO,eAAe,KAAG,IAQ9D,CAAA;AAED,eAAO,MAAM,qBAAqB,GAAI,IAAI,MAAM,KAAG,IAKlD,CAAA;AAED,eAAO,MAAM,mBAAmB,QAAO,eAAe,EACzB,CAAA;AAE7B,eAAO,MAAM,yBAAyB,GACpC,UAAU,wBAAwB,KACjC,CAAC,MAAM,IAAI,CAKb,CAAA;AAED,eAAO,MAAM,0BAA0B,GAAI,OAAO,eAAe,KAAG,IA+BnE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.63.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -38,7 +38,9 @@
|
|
|
38
38
|
"package.json",
|
|
39
39
|
"dist/**/*"
|
|
40
40
|
],
|
|
41
|
-
"output": [
|
|
41
|
+
"output": [
|
|
42
|
+
"publish-output.txt"
|
|
43
|
+
],
|
|
42
44
|
"env": {
|
|
43
45
|
"NPM_RELEASE_CHANNEL": {
|
|
44
46
|
"external": true
|