@rpcbase/client 0.337.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
- return globalAny[key];
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
- suppressBeforeUnloadUntil: 0,
119
- lastBeforeUnloadAt: 0
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 && Date.now() - windowState.lastBeforeUnloadAt < NATIVE_PROMPT_COOLDOWN_MS) {
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
- const guard = args ? pickNavigationGuard(args) : null;
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.suppressBeforeUnloadUntil = Date.now() + SUPPRESS_BEFOREUNLOAD_MS;
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.suppressBeforeUnloadUntil > 0 && Date.now() < state.suppressBeforeUnloadUntil) {
191
- state.suppressBeforeUnloadUntil = 0;
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.lastBeforeUnloadAt = Date.now();
214
+ state.nativePromptActive = true;
199
215
  }
200
216
  event.preventDefault();
201
217
  event.returnValue = "";
@@ -1 +1 @@
1
- {"version":3,"file":"installGlobalNavigationGuard.d.ts","sourceRoot":"","sources":["../../src/navigationGuard/installGlobalNavigationGuard.ts"],"names":[],"mappings":"AAAA,OAAQ,EAAuB,KAAK,UAAU,EAAuB,MAAM,iBAAiB,CAAA;AA2F5F,eAAO,MAAM,4BAA4B,GAAI,QAAQ,UAAU,KAAG,IAoFjE,CAAA"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.337.0",
3
+ "version": "0.338.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"