@rpcbase/client 0.336.0 → 0.338.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.js
CHANGED
|
@@ -112,17 +112,19 @@ const getWindowState = () => {
|
|
|
112
112
|
const key = "__rpcbaseNavigationGuardWindowState";
|
|
113
113
|
const globalAny = window;
|
|
114
114
|
if (globalAny[key]) {
|
|
115
|
-
|
|
115
|
+
const existing = globalAny[key];
|
|
116
|
+
if (typeof existing.suppressBeforeUnloadFromKey === "undefined") {
|
|
117
|
+
existing.suppressBeforeUnloadFromKey = null;
|
|
118
|
+
}
|
|
119
|
+
return existing;
|
|
116
120
|
}
|
|
117
121
|
const created = {
|
|
118
|
-
|
|
119
|
-
|
|
122
|
+
suppressBeforeUnloadFromKey: null,
|
|
123
|
+
nativePromptActive: false
|
|
120
124
|
};
|
|
121
125
|
globalAny[key] = created;
|
|
122
126
|
return created;
|
|
123
127
|
};
|
|
124
|
-
const NATIVE_PROMPT_COOLDOWN_MS = 1e3;
|
|
125
|
-
const SUPPRESS_BEFOREUNLOAD_MS = 1e3;
|
|
126
128
|
const getGlobalGuardWeakSet = () => {
|
|
127
129
|
const key = "__rpcbaseNavigationGuardInstalledRouters";
|
|
128
130
|
const globalAny = globalThis;
|
|
@@ -146,9 +148,18 @@ const installGlobalNavigationGuard = (router) => {
|
|
|
146
148
|
let lastPromptedLocationKey = null;
|
|
147
149
|
router.getBlocker(blockerKey, (args) => {
|
|
148
150
|
lastArgs = args;
|
|
151
|
+
if (windowState?.nativePromptActive && args.historyAction !== "POP") {
|
|
152
|
+
windowState.nativePromptActive = false;
|
|
153
|
+
}
|
|
149
154
|
return pickNavigationGuard(args) !== null;
|
|
150
155
|
});
|
|
151
156
|
router.subscribe((state) => {
|
|
157
|
+
if (windowState?.suppressBeforeUnloadFromKey && state?.location) {
|
|
158
|
+
const currentKey = getLocationDedupKey(state.location);
|
|
159
|
+
if (currentKey !== windowState.suppressBeforeUnloadFromKey) {
|
|
160
|
+
windowState.suppressBeforeUnloadFromKey = null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
152
163
|
const blocker = state.blockers.get(blockerKey);
|
|
153
164
|
if (!blocker || blocker.state !== "blocked") {
|
|
154
165
|
lastPromptedLocationKey = null;
|
|
@@ -160,12 +171,17 @@ const installGlobalNavigationGuard = (router) => {
|
|
|
160
171
|
return;
|
|
161
172
|
}
|
|
162
173
|
lastPromptedLocationKey = dedupKey;
|
|
163
|
-
if (windowState &&
|
|
174
|
+
if (windowState?.nativePromptActive && lastArgs?.historyAction === "POP") {
|
|
175
|
+
windowState.nativePromptActive = false;
|
|
164
176
|
blocker.reset();
|
|
165
177
|
return;
|
|
166
178
|
}
|
|
167
179
|
const args = lastArgs;
|
|
168
|
-
|
|
180
|
+
if (!args) {
|
|
181
|
+
blocker.proceed();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const guard = pickNavigationGuard(args);
|
|
169
185
|
if (!guard) {
|
|
170
186
|
blocker.proceed();
|
|
171
187
|
return;
|
|
@@ -173,7 +189,7 @@ const installGlobalNavigationGuard = (router) => {
|
|
|
173
189
|
const ok = window.confirm(guard.message);
|
|
174
190
|
if (ok) {
|
|
175
191
|
if (windowState) {
|
|
176
|
-
windowState.
|
|
192
|
+
windowState.suppressBeforeUnloadFromKey = getLocationDedupKey(args.currentLocation);
|
|
177
193
|
}
|
|
178
194
|
blocker.proceed();
|
|
179
195
|
} else {
|
|
@@ -187,15 +203,15 @@ const installGlobalNavigationGuard = (router) => {
|
|
|
187
203
|
globalAny[key] = true;
|
|
188
204
|
window.addEventListener("beforeunload", (event) => {
|
|
189
205
|
const state = getWindowState();
|
|
190
|
-
if (state && state.
|
|
191
|
-
state.
|
|
206
|
+
if (state && state.suppressBeforeUnloadFromKey) {
|
|
207
|
+
state.suppressBeforeUnloadFromKey = null;
|
|
192
208
|
return;
|
|
193
209
|
}
|
|
194
210
|
if (!hasUnloadBlockers()) {
|
|
195
211
|
return;
|
|
196
212
|
}
|
|
197
213
|
if (state) {
|
|
198
|
-
state.
|
|
214
|
+
state.nativePromptActive = true;
|
|
199
215
|
}
|
|
200
216
|
event.preventDefault();
|
|
201
217
|
event.returnValue = "";
|
|
@@ -360,13 +376,13 @@ const initWithRoutes = async (routesElement, opts) => {
|
|
|
360
376
|
}
|
|
361
377
|
return false;
|
|
362
378
|
};
|
|
363
|
-
const phReactHandler = (
|
|
379
|
+
const phReactHandler = (reactContext) => (error, errorInfo) => {
|
|
364
380
|
const err = toError(error);
|
|
365
381
|
posthog.captureException(err, {
|
|
366
|
-
|
|
367
|
-
|
|
382
|
+
reactContext,
|
|
383
|
+
componentStack: errorInfo?.componentStack
|
|
368
384
|
});
|
|
369
|
-
if (
|
|
385
|
+
if (reactContext === "uncaught") {
|
|
370
386
|
console.warn("Uncaught error", err, errorInfo?.componentStack);
|
|
371
387
|
}
|
|
372
388
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"installGlobalNavigationGuard.d.ts","sourceRoot":"","sources":["../../src/navigationGuard/installGlobalNavigationGuard.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"installGlobalNavigationGuard.d.ts","sourceRoot":"","sources":["../../src/navigationGuard/installGlobalNavigationGuard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,UAAU,EAAwB,MAAM,iBAAiB,CAAA;AA4F5F,eAAO,MAAM,4BAA4B,GAAI,QAAQ,UAAU,KAAG,IAoGjE,CAAA"}
|