asma-core-helpers 0.41.0 → 0.41.2
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/lib/collaboration/applyOperation.d.ts.map +1 -1
- package/lib/collaboration/applyOperation.js +108 -0
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/index.d.ts +1 -0
- package/lib/collaboration/index.d.ts.map +1 -1
- package/lib/collaboration/index.js +1 -0
- package/lib/collaboration/index.js.map +1 -1
- package/lib/collaboration/narrativePresence.d.ts +12 -0
- package/lib/collaboration/narrativePresence.d.ts.map +1 -0
- package/lib/collaboration/narrativePresence.js +34 -0
- package/lib/collaboration/narrativePresence.js.map +1 -0
- package/lib/collaboration/operations.d.ts +26 -1
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +1 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +44 -1
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +18 -1
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateAuthoringMeta.d.ts +4 -4
- package/lib/collaboration/templateAuthoringMeta.d.ts.map +1 -1
- package/lib/collaboration/templateAuthoringMeta.js +103 -5
- package/lib/collaboration/templateAuthoringMeta.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +9 -2
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/g-definitions.d.ts +5 -0
- package/lib/g-definitions.d.ts.map +1 -1
- package/lib/g-definitions.js +6 -0
- package/lib/g-definitions.js.map +1 -1
- package/lib/overlayBack.d.ts +103 -0
- package/lib/overlayBack.d.ts.map +1 -0
- package/lib/overlayBack.js +540 -0
- package/lib/overlayBack.js.map +1 -0
- package/package.json +2 -1
- package/src/collaboration/applyOperation.ts +114 -0
- package/src/collaboration/index.ts +1 -0
- package/src/collaboration/narrativePresence.test.ts +328 -0
- package/src/collaboration/narrativePresence.ts +42 -0
- package/src/collaboration/operations.ts +11 -0
- package/src/collaboration/schemas.ts +27 -2
- package/src/collaboration/templateAuthoringMeta.ts +107 -6
- package/src/collaboration/templateDocument.ts +14 -4
- package/src/g-definitions.ts +13 -0
- package/src/overlayBack.test.ts +742 -0
- package/src/overlayBack.ts +647 -0
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import type { History, To } from 'history'
|
|
2
|
+
|
|
3
|
+
export const OVERLAY_BACK_VERSION = 1 as const
|
|
4
|
+
export const OVERLAY_BACK_MARKER_KEY = '__asmaOverlayBack' as const
|
|
5
|
+
|
|
6
|
+
/** Same-origin, no PII: protocol version, per-document session id, per-arm marker id (SEC-002). */
|
|
7
|
+
export interface OverlayBackMarker {
|
|
8
|
+
v: typeof OVERLAY_BACK_VERSION
|
|
9
|
+
s: string
|
|
10
|
+
m: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Published at `window.__ASMA__SHELL__.overlayBack` (PAT-002). `enabled` is the kill switch —
|
|
14
|
+
* ui-core only arms a guard through `register()` once a host turns this on. */
|
|
15
|
+
export interface OverlayBackAdapter {
|
|
16
|
+
readonly v: typeof OVERLAY_BACK_VERSION
|
|
17
|
+
enabled: boolean
|
|
18
|
+
readonly pendingNavigation: boolean
|
|
19
|
+
/** Registers the current top overlay's close function. Call again (with the new top's
|
|
20
|
+
* `requestClose`) before unregistering the previous top, so the stack never looks empty
|
|
21
|
+
* while an overlay is still open. Returns the unregister function for that entry. */
|
|
22
|
+
register: (requestClose: () => false | void | Promise<unknown>) => () => void
|
|
23
|
+
/** Ask the current registry top to close after the previous top unregistered. */
|
|
24
|
+
continuePendingNavigation: () => void
|
|
25
|
+
/** Complete a pending route intent when the current top explicitly cannot close. */
|
|
26
|
+
failOpenPendingNavigation: () => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type OverlayBackHistoryState = Record<string, unknown> & {
|
|
30
|
+
usr?: unknown
|
|
31
|
+
key?: string
|
|
32
|
+
idx?: number
|
|
33
|
+
[OVERLAY_BACK_MARKER_KEY]?: OverlayBackMarker
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface GuardState {
|
|
37
|
+
marker: OverlayBackMarker
|
|
38
|
+
baseKey: string | undefined
|
|
39
|
+
requestClose: () => false | void | Promise<unknown>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type PendingNav = { kind: 'PUSH'; to: To; state: unknown } | { kind: 'REPLACE'; to: To; state: unknown }
|
|
43
|
+
|
|
44
|
+
export interface OverlayBackInternalState {
|
|
45
|
+
sessionId: string
|
|
46
|
+
idCounter: number
|
|
47
|
+
guard: GuardState | null
|
|
48
|
+
phase: 'rearming' | 'draining' | null
|
|
49
|
+
pendingNav: PendingNav | null
|
|
50
|
+
pendingDrainSnapshot: { href: string; state: OverlayBackHistoryState; scrollX: number; scrollY: number } | null
|
|
51
|
+
pendingCloseAttempt: number
|
|
52
|
+
pendingRearmRequestClose: (() => false | void | Promise<unknown>) | null
|
|
53
|
+
retiredMarker: OverlayBackMarker | null
|
|
54
|
+
retiredBaseKey: string | undefined
|
|
55
|
+
retiredBouncePending: boolean
|
|
56
|
+
disablePending: boolean
|
|
57
|
+
enabled: boolean
|
|
58
|
+
currentRequestClose: (() => false | void | Promise<unknown>) | null
|
|
59
|
+
registrations: Array<() => false | void | Promise<unknown>>
|
|
60
|
+
suspended: boolean
|
|
61
|
+
openCount: number
|
|
62
|
+
listenerInstalled: boolean
|
|
63
|
+
historyWrapped: boolean
|
|
64
|
+
/** Timestamp (ms) of the last trusted click, non-Escape keydown, touchend or pointerup. */
|
|
65
|
+
lastGestureAt: number
|
|
66
|
+
/** Timestamp (ms) of the last popstate, owned or foreign. */
|
|
67
|
+
lastPopstateAt: number
|
|
68
|
+
originalPush: History['push'] | null
|
|
69
|
+
originalReplace: History['replace'] | null
|
|
70
|
+
public: OverlayBackAdapter
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
declare global {
|
|
74
|
+
interface Window {
|
|
75
|
+
/** Internal adapter state, kept on the window so it survives HMR re-evaluation of this
|
|
76
|
+
* module and stays consistent if several bundles load their own copy of this package
|
|
77
|
+
* (CON-010). Not part of the public `overlayBack` contract. */
|
|
78
|
+
__ASMA_OVERLAY_BACK_INTERNAL__?: OverlayBackInternalState
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function generateId(): string {
|
|
83
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function nextMarkerId(internal: OverlayBackInternalState): string {
|
|
87
|
+
internal.idCounter += 1
|
|
88
|
+
return `${internal.idCounter}-${Math.random().toString(36).slice(2, 10)}`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getOrCreateInternal(win: Window): OverlayBackInternalState {
|
|
92
|
+
const existing = win.__ASMA_OVERLAY_BACK_INTERNAL__
|
|
93
|
+
if (existing) {
|
|
94
|
+
return existing
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let internal!: OverlayBackInternalState
|
|
98
|
+
const publicApi: OverlayBackAdapter = {
|
|
99
|
+
v: OVERLAY_BACK_VERSION,
|
|
100
|
+
get enabled() {
|
|
101
|
+
return internal.enabled
|
|
102
|
+
},
|
|
103
|
+
set enabled(enabled: boolean) {
|
|
104
|
+
setOverlayBackEnabled(win, internal, enabled)
|
|
105
|
+
},
|
|
106
|
+
get pendingNavigation() {
|
|
107
|
+
return internal.pendingNav !== null
|
|
108
|
+
},
|
|
109
|
+
register: (requestClose) => register(win, internal, requestClose),
|
|
110
|
+
continuePendingNavigation: () => {
|
|
111
|
+
const guard = internal.guard
|
|
112
|
+
if (internal.pendingNav && guard && internal.phase === null) {
|
|
113
|
+
requestCloseForPendingNavigation(win, internal, guard)
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
failOpenPendingNavigation: () => failOpenPendingNavigation(win, internal),
|
|
117
|
+
}
|
|
118
|
+
internal = {
|
|
119
|
+
sessionId: generateId(),
|
|
120
|
+
idCounter: 0,
|
|
121
|
+
guard: null,
|
|
122
|
+
phase: null,
|
|
123
|
+
pendingNav: null,
|
|
124
|
+
pendingDrainSnapshot: null,
|
|
125
|
+
pendingCloseAttempt: 0,
|
|
126
|
+
pendingRearmRequestClose: null,
|
|
127
|
+
retiredMarker: null,
|
|
128
|
+
retiredBaseKey: undefined,
|
|
129
|
+
retiredBouncePending: false,
|
|
130
|
+
disablePending: false,
|
|
131
|
+
enabled: false,
|
|
132
|
+
currentRequestClose: null,
|
|
133
|
+
registrations: [],
|
|
134
|
+
suspended: false,
|
|
135
|
+
openCount: 0,
|
|
136
|
+
listenerInstalled: false,
|
|
137
|
+
historyWrapped: false,
|
|
138
|
+
lastGestureAt: Number.NEGATIVE_INFINITY,
|
|
139
|
+
lastPopstateAt: Number.NEGATIVE_INFINITY,
|
|
140
|
+
originalPush: null,
|
|
141
|
+
originalReplace: null,
|
|
142
|
+
public: publicApi,
|
|
143
|
+
}
|
|
144
|
+
win.__ASMA_OVERLAY_BACK_INTERNAL__ = internal
|
|
145
|
+
return internal
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function readState(win: Window): OverlayBackHistoryState | null {
|
|
149
|
+
return (win.history.state ?? null) as OverlayBackHistoryState | null
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function readMarker(state: OverlayBackHistoryState | null): OverlayBackMarker | undefined {
|
|
153
|
+
return state?.[OVERLAY_BACK_MARKER_KEY]
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function markerMatches(marker: OverlayBackMarker | undefined, expected: OverlayBackMarker | null): boolean {
|
|
157
|
+
return !!marker && !!expected && marker.v === expected.v && marker.s === expected.s && marker.m === expected.m
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Init-time cleanup (boot row of §2.6): a reload that lands on a stale guard entry keeps
|
|
161
|
+
* `usr`/`key`/`idx` but drops the marker, so the page never looks armed for a guard nothing
|
|
162
|
+
* is managing any more. */
|
|
163
|
+
function stripStaleMarker(win: Window): void {
|
|
164
|
+
const state = readState(win)
|
|
165
|
+
if (!state || !readMarker(state)) {
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
const { [OVERLAY_BACK_MARKER_KEY]: _marker, ...rest } = state
|
|
169
|
+
try {
|
|
170
|
+
win.history.replaceState(rest, '', win.location.href)
|
|
171
|
+
} catch {
|
|
172
|
+
/* fail open: leave the stale marker rather than risk losing the entry's state */
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function reconcileAfterRestore(win: Window, internal: OverlayBackInternalState): void {
|
|
177
|
+
const guard = internal.guard
|
|
178
|
+
if (!guard) {
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
const marker = readMarker(readState(win))
|
|
182
|
+
if (markerMatches(marker, guard.marker)) {
|
|
183
|
+
return // restored exactly onto the live guard entry: stay armed
|
|
184
|
+
}
|
|
185
|
+
internal.guard = null
|
|
186
|
+
internal.phase = null
|
|
187
|
+
internal.pendingNav = null
|
|
188
|
+
internal.pendingDrainSnapshot = null
|
|
189
|
+
internal.pendingRearmRequestClose = null
|
|
190
|
+
internal.disablePending = false
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Biased toward no guard (REQ per the arming gate spec): arms only when the API says the page
|
|
194
|
+
* hasn't lost user activation, and the last trusted gesture is newer than the last popstate —
|
|
195
|
+
* otherwise an overlay that opened without a fresh click/tap/key (idle-timeout dialog, a result
|
|
196
|
+
* dialog after a long request, or one opened programmatically right after a Back press) gets no
|
|
197
|
+
* guard and falls back to today's behaviour. */
|
|
198
|
+
function armingGatePasses(win: Window, internal: OverlayBackInternalState): boolean {
|
|
199
|
+
const userActivation = (win.navigator as Navigator & { userActivation?: { isActive: boolean } }).userActivation
|
|
200
|
+
if (userActivation?.isActive === false) {
|
|
201
|
+
return false
|
|
202
|
+
}
|
|
203
|
+
return internal.lastGestureAt > internal.lastPopstateAt
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function setOverlayBackEnabled(win: Window, internal: OverlayBackInternalState, enabled: boolean): void {
|
|
207
|
+
if (internal.enabled === enabled) return
|
|
208
|
+
internal.enabled = enabled
|
|
209
|
+
if (enabled) {
|
|
210
|
+
internal.disablePending = false
|
|
211
|
+
if (internal.guard && internal.phase === 'draining' && internal.currentRequestClose) {
|
|
212
|
+
internal.pendingRearmRequestClose = internal.currentRequestClose
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
if (!internal.guard && internal.openCount > 0 && internal.currentRequestClose) {
|
|
216
|
+
arm(win, internal, internal.currentRequestClose)
|
|
217
|
+
}
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
internal.pendingRearmRequestClose = null
|
|
222
|
+
if (!internal.guard) return
|
|
223
|
+
if (internal.phase === null) {
|
|
224
|
+
drain(win, internal)
|
|
225
|
+
} else {
|
|
226
|
+
internal.disablePending = true
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function arm(win: Window, internal: OverlayBackInternalState, requestClose: () => false | void | Promise<unknown>): void {
|
|
231
|
+
if (!internal.public.enabled) {
|
|
232
|
+
return // kill switch off: registry-only, no history write
|
|
233
|
+
}
|
|
234
|
+
if (!armingGatePasses(win, internal)) {
|
|
235
|
+
return // gate fails: registry-only, no history write
|
|
236
|
+
}
|
|
237
|
+
const baseState = readState(win)
|
|
238
|
+
const marker: OverlayBackMarker = { v: OVERLAY_BACK_VERSION, s: internal.sessionId, m: nextMarkerId(internal) }
|
|
239
|
+
// CON-003: clone usr/key/idx exactly (never idx+1) so history@5's cached index never desyncs.
|
|
240
|
+
const nextState: OverlayBackHistoryState = {
|
|
241
|
+
...baseState,
|
|
242
|
+
usr: baseState?.usr,
|
|
243
|
+
key: baseState?.key,
|
|
244
|
+
idx: baseState?.idx,
|
|
245
|
+
[OVERLAY_BACK_MARKER_KEY]: marker,
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
win.history.pushState(nextState, '', win.location.href)
|
|
249
|
+
} catch {
|
|
250
|
+
return // WebKit's 100-push budget or similar: fail open, stay idle
|
|
251
|
+
}
|
|
252
|
+
internal.guard = { marker, baseKey: baseState?.key, requestClose }
|
|
253
|
+
internal.retiredMarker = null
|
|
254
|
+
internal.retiredBaseKey = undefined
|
|
255
|
+
internal.retiredBouncePending = false
|
|
256
|
+
internal.phase = null
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function finishDrain(win: Window, internal: OverlayBackInternalState): void {
|
|
260
|
+
const retiredMarker = internal.guard?.marker ?? null
|
|
261
|
+
const snapshot = internal.pendingDrainSnapshot
|
|
262
|
+
const retiredBaseKey = snapshot?.state.key ?? internal.guard?.baseKey
|
|
263
|
+
internal.pendingDrainSnapshot = null
|
|
264
|
+
if (snapshot) {
|
|
265
|
+
try {
|
|
266
|
+
win.history.replaceState(snapshot.state, '', snapshot.href)
|
|
267
|
+
} catch {
|
|
268
|
+
/* fail open: keep whatever natively landed on B */
|
|
269
|
+
}
|
|
270
|
+
if (win.scrollX !== snapshot.scrollX || win.scrollY !== snapshot.scrollY) {
|
|
271
|
+
try {
|
|
272
|
+
win.scrollTo(snapshot.scrollX, snapshot.scrollY)
|
|
273
|
+
} catch {
|
|
274
|
+
/* fail open: route state is more important than scroll restoration */
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
internal.guard = null
|
|
279
|
+
internal.retiredMarker = retiredMarker
|
|
280
|
+
internal.retiredBaseKey = retiredBaseKey
|
|
281
|
+
internal.phase = null
|
|
282
|
+
internal.disablePending = false
|
|
283
|
+
|
|
284
|
+
const pendingNav = internal.pendingNav
|
|
285
|
+
internal.pendingNav = null
|
|
286
|
+
const pendingRearm = internal.pendingRearmRequestClose
|
|
287
|
+
internal.pendingRearmRequestClose = null
|
|
288
|
+
|
|
289
|
+
if (pendingNav) {
|
|
290
|
+
queueMicrotask(() => runPendingNav(internal, pendingNav))
|
|
291
|
+
} else if (pendingRearm) {
|
|
292
|
+
arm(win, internal, pendingRearm)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function runPendingNav(internal: OverlayBackInternalState, pending: PendingNav): void {
|
|
297
|
+
if (pending.kind === 'PUSH') {
|
|
298
|
+
internal.originalPush?.(pending.to, pending.state)
|
|
299
|
+
} else {
|
|
300
|
+
internal.originalReplace?.(pending.to, pending.state)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function failOpenPendingNavigation(win: Window, internal: OverlayBackInternalState): void {
|
|
305
|
+
const pending = internal.pendingNav
|
|
306
|
+
if (!pending) return
|
|
307
|
+
const state = readState(win)
|
|
308
|
+
const marker = readMarker(state)
|
|
309
|
+
if (markerMatches(marker, internal.guard?.marker ?? null)) {
|
|
310
|
+
const { [OVERLAY_BACK_MARKER_KEY]: _marker, ...rest } = state ?? {}
|
|
311
|
+
try {
|
|
312
|
+
win.history.replaceState(rest, '', win.location.href)
|
|
313
|
+
} catch {
|
|
314
|
+
// The intent must not remain blocked merely because marker cleanup failed.
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
internal.pendingNav = null
|
|
318
|
+
internal.pendingDrainSnapshot = null
|
|
319
|
+
internal.retiredMarker = internal.guard?.marker ?? internal.retiredMarker
|
|
320
|
+
internal.retiredBaseKey = internal.guard?.baseKey ?? internal.retiredBaseKey
|
|
321
|
+
internal.guard = null
|
|
322
|
+
internal.phase = null
|
|
323
|
+
queueMicrotask(() => runPendingNav(internal, pending))
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function requestCloseForPendingNavigation(
|
|
327
|
+
win: Window,
|
|
328
|
+
internal: OverlayBackInternalState,
|
|
329
|
+
guard: GuardState,
|
|
330
|
+
): void {
|
|
331
|
+
const attempt = ++internal.pendingCloseAttempt
|
|
332
|
+
let result: false | void | Promise<unknown>
|
|
333
|
+
try {
|
|
334
|
+
result = guard.requestClose()
|
|
335
|
+
} catch (error) {
|
|
336
|
+
console.error('[overlayBack] overlay closer threw; continuing the pending navigation', error)
|
|
337
|
+
failOpenPendingNavigation(win, internal)
|
|
338
|
+
return
|
|
339
|
+
}
|
|
340
|
+
if (result === false) failOpenPendingNavigation(win, internal)
|
|
341
|
+
else {
|
|
342
|
+
const failOpenIfStalled = (): void => {
|
|
343
|
+
if (
|
|
344
|
+
internal.pendingNav &&
|
|
345
|
+
internal.guard === guard &&
|
|
346
|
+
internal.phase === null &&
|
|
347
|
+
internal.pendingCloseAttempt === attempt
|
|
348
|
+
) {
|
|
349
|
+
failOpenPendingNavigation(win, internal)
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const scheduleFailOpen = (): void => {
|
|
353
|
+
win.setTimeout(failOpenIfStalled, 0)
|
|
354
|
+
}
|
|
355
|
+
if (result && typeof (result as Promise<unknown>).then === 'function') {
|
|
356
|
+
void (result as Promise<unknown>).then(scheduleFailOpen, scheduleFailOpen)
|
|
357
|
+
return
|
|
358
|
+
}
|
|
359
|
+
win.setTimeout(() => {
|
|
360
|
+
failOpenIfStalled()
|
|
361
|
+
}, 0)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function drain(win: Window, internal: OverlayBackInternalState): void {
|
|
366
|
+
const guard = internal.guard
|
|
367
|
+
if (!guard) {
|
|
368
|
+
return
|
|
369
|
+
}
|
|
370
|
+
const state = readState(win)
|
|
371
|
+
const { [OVERLAY_BACK_MARKER_KEY]: _marker, ...rest } = state ?? {}
|
|
372
|
+
internal.pendingDrainSnapshot = {
|
|
373
|
+
href: win.location.href,
|
|
374
|
+
state: rest,
|
|
375
|
+
scrollX: win.scrollX,
|
|
376
|
+
scrollY: win.scrollY,
|
|
377
|
+
}
|
|
378
|
+
internal.phase = 'draining'
|
|
379
|
+
try {
|
|
380
|
+
win.history.go(-1)
|
|
381
|
+
} catch {
|
|
382
|
+
internal.guard = null
|
|
383
|
+
internal.phase = null
|
|
384
|
+
internal.pendingDrainSnapshot = null
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function register(
|
|
389
|
+
win: Window,
|
|
390
|
+
internal: OverlayBackInternalState,
|
|
391
|
+
requestClose: () => false | void | Promise<unknown>,
|
|
392
|
+
): () => void {
|
|
393
|
+
internal.registrations.push(requestClose)
|
|
394
|
+
internal.openCount = internal.registrations.length
|
|
395
|
+
internal.currentRequestClose = requestClose
|
|
396
|
+
if (internal.phase === 'draining') {
|
|
397
|
+
internal.pendingRearmRequestClose = requestClose
|
|
398
|
+
} else if (!internal.guard) {
|
|
399
|
+
arm(win, internal, requestClose)
|
|
400
|
+
} else {
|
|
401
|
+
internal.guard = { ...internal.guard, requestClose }
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
let unregistered = false
|
|
405
|
+
return () => {
|
|
406
|
+
if (unregistered) {
|
|
407
|
+
return
|
|
408
|
+
}
|
|
409
|
+
unregistered = true
|
|
410
|
+
const index = internal.registrations.lastIndexOf(requestClose)
|
|
411
|
+
if (index !== -1) internal.registrations.splice(index, 1)
|
|
412
|
+
internal.openCount = internal.registrations.length
|
|
413
|
+
internal.currentRequestClose = internal.registrations.at(-1) ?? null
|
|
414
|
+
if (internal.guard && internal.phase === null && internal.currentRequestClose) {
|
|
415
|
+
internal.guard = { ...internal.guard, requestClose: internal.currentRequestClose }
|
|
416
|
+
}
|
|
417
|
+
if (internal.phase === 'draining') {
|
|
418
|
+
internal.pendingRearmRequestClose = internal.currentRequestClose
|
|
419
|
+
}
|
|
420
|
+
if (internal.openCount === 0 && internal.guard && internal.phase === null) {
|
|
421
|
+
drain(win, internal)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function handlePopstate(win: Window, internal: OverlayBackInternalState, event: Event): void {
|
|
427
|
+
if (internal.suspended) {
|
|
428
|
+
return
|
|
429
|
+
}
|
|
430
|
+
if ((event as Event & { singleSpa?: boolean }).singleSpa) {
|
|
431
|
+
return // foreign single-spa reroute event: never ours to suppress
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const state = readState(win)
|
|
435
|
+
const marker = readMarker(state)
|
|
436
|
+
const guard = internal.guard
|
|
437
|
+
|
|
438
|
+
if (!guard) {
|
|
439
|
+
if (internal.retiredBouncePending) {
|
|
440
|
+
internal.retiredBouncePending = false
|
|
441
|
+
if (!marker && (state?.key ?? undefined) === internal.retiredBaseKey) {
|
|
442
|
+
;(event as PopStateEvent).stopImmediatePropagation()
|
|
443
|
+
return
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (markerMatches(marker, internal.retiredMarker)) {
|
|
447
|
+
// Forward landed on a retired guard marker (REQ-006): bounce back, delta is 0 so this
|
|
448
|
+
// never moves a real page.
|
|
449
|
+
;(event as PopStateEvent).stopImmediatePropagation()
|
|
450
|
+
internal.retiredBouncePending = true
|
|
451
|
+
try {
|
|
452
|
+
win.history.go(-1)
|
|
453
|
+
} catch {
|
|
454
|
+
internal.retiredBouncePending = false
|
|
455
|
+
/* fail open: leave the stray marker in place */
|
|
456
|
+
}
|
|
457
|
+
return
|
|
458
|
+
}
|
|
459
|
+
internal.lastPopstateAt = Date.now()
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const landedOnGuard = markerMatches(marker, guard.marker)
|
|
464
|
+
const landedOnBase = !landedOnGuard && (state?.key ?? undefined) === guard.baseKey
|
|
465
|
+
|
|
466
|
+
if (internal.phase === 'rearming') {
|
|
467
|
+
if (landedOnGuard) {
|
|
468
|
+
;(event as PopStateEvent).stopImmediatePropagation()
|
|
469
|
+
internal.phase = null
|
|
470
|
+
if (internal.openCount === 0) {
|
|
471
|
+
drain(win, internal)
|
|
472
|
+
return
|
|
473
|
+
}
|
|
474
|
+
if (internal.disablePending || !internal.public.enabled) {
|
|
475
|
+
internal.disablePending = false
|
|
476
|
+
drain(win, internal)
|
|
477
|
+
return
|
|
478
|
+
}
|
|
479
|
+
if (internal.pendingNav) requestCloseForPendingNavigation(win, internal, guard)
|
|
480
|
+
else void guard.requestClose()
|
|
481
|
+
return
|
|
482
|
+
}
|
|
483
|
+
internal.lastPopstateAt = Date.now()
|
|
484
|
+
internal.retiredMarker = guard.marker
|
|
485
|
+
internal.retiredBaseKey = guard.baseKey
|
|
486
|
+
internal.guard = null
|
|
487
|
+
internal.phase = null
|
|
488
|
+
void guard.requestClose()
|
|
489
|
+
return
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (internal.phase === 'draining') {
|
|
493
|
+
if (landedOnBase) {
|
|
494
|
+
;(event as PopStateEvent).stopImmediatePropagation()
|
|
495
|
+
finishDrain(win, internal)
|
|
496
|
+
return
|
|
497
|
+
}
|
|
498
|
+
internal.lastPopstateAt = Date.now()
|
|
499
|
+
internal.retiredMarker = guard.marker
|
|
500
|
+
internal.retiredBaseKey = guard.baseKey
|
|
501
|
+
internal.guard = null
|
|
502
|
+
internal.phase = null
|
|
503
|
+
internal.pendingDrainSnapshot = null
|
|
504
|
+
return
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// armed: expect Back to land on the base entry
|
|
508
|
+
if (landedOnBase) {
|
|
509
|
+
;(event as PopStateEvent).stopImmediatePropagation()
|
|
510
|
+
internal.phase = 'rearming'
|
|
511
|
+
try {
|
|
512
|
+
win.history.go(1)
|
|
513
|
+
} catch {
|
|
514
|
+
internal.guard = null
|
|
515
|
+
internal.phase = null
|
|
516
|
+
}
|
|
517
|
+
return
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Landed somewhere neither B nor G recognises (multi-step jump, foreign navigation): fail open.
|
|
521
|
+
internal.lastPopstateAt = Date.now()
|
|
522
|
+
internal.retiredMarker = guard.marker
|
|
523
|
+
internal.retiredBaseKey = guard.baseKey
|
|
524
|
+
internal.guard = null
|
|
525
|
+
internal.phase = null
|
|
526
|
+
void guard.requestClose()
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function pathOf(to: To, fallback: string): string {
|
|
530
|
+
if (typeof to === 'string') {
|
|
531
|
+
return to.split(/[?#]/, 1)[0] || fallback
|
|
532
|
+
}
|
|
533
|
+
return to.pathname ?? fallback
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** Must run before `createBrowserHistory()` (CON-002): the popstate listener added here has to
|
|
537
|
+
* win the registration-order race against history@5's own listener. Idempotent — a second call
|
|
538
|
+
* (HMR, several bundle copies) reuses the existing window-level state and adds no listener. */
|
|
539
|
+
export function installOverlayBackPopstateListener(win: Window): OverlayBackAdapter {
|
|
540
|
+
const internal = getOrCreateInternal(win)
|
|
541
|
+
if (internal.listenerInstalled) {
|
|
542
|
+
return internal.public
|
|
543
|
+
}
|
|
544
|
+
internal.listenerInstalled = true
|
|
545
|
+
|
|
546
|
+
stripStaleMarker(win)
|
|
547
|
+
|
|
548
|
+
const recordTrustedGesture = (event: Event) => {
|
|
549
|
+
if (event.isTrusted) {
|
|
550
|
+
internal.lastGestureAt = Date.now()
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
win.addEventListener('click', recordTrustedGesture, { capture: true })
|
|
554
|
+
win.addEventListener('touchend', recordTrustedGesture, { capture: true })
|
|
555
|
+
win.addEventListener('pointerup', recordTrustedGesture, { capture: true })
|
|
556
|
+
win.addEventListener(
|
|
557
|
+
'keydown',
|
|
558
|
+
(event) => {
|
|
559
|
+
if ((event as KeyboardEvent).key !== 'Escape') {
|
|
560
|
+
recordTrustedGesture(event)
|
|
561
|
+
}
|
|
562
|
+
},
|
|
563
|
+
{ capture: true },
|
|
564
|
+
)
|
|
565
|
+
|
|
566
|
+
win.addEventListener('popstate', (event) => handlePopstate(win, internal, event))
|
|
567
|
+
win.addEventListener('pagehide', () => {
|
|
568
|
+
internal.suspended = true
|
|
569
|
+
})
|
|
570
|
+
win.addEventListener('pageshow', (event) => {
|
|
571
|
+
if ((event as PageTransitionEvent).persisted) {
|
|
572
|
+
reconcileAfterRestore(win, internal)
|
|
573
|
+
}
|
|
574
|
+
internal.suspended = false
|
|
575
|
+
})
|
|
576
|
+
|
|
577
|
+
return internal.public
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** Wraps `push`/`replace` on the real history instance. Must run after `createBrowserHistory()`
|
|
581
|
+
* returns, since it wraps that instance's methods. Idempotent. `block` is left untouched: the
|
|
582
|
+
* suppression predicate already keeps owned hops from ever reaching history@5's own blocker
|
|
583
|
+
* check (TEST-002), and a deferred push/replace runs through the real, unwrapped `push`/`replace`
|
|
584
|
+
* so a live blocker evaluates it normally (TEST-003) — there is nothing left for a `block` wrap
|
|
585
|
+
* to do. */
|
|
586
|
+
export function finishOverlayBackInstall(win: Window, history: History): void {
|
|
587
|
+
const internal = getOrCreateInternal(win)
|
|
588
|
+
if (internal.historyWrapped) {
|
|
589
|
+
return
|
|
590
|
+
}
|
|
591
|
+
internal.historyWrapped = true
|
|
592
|
+
|
|
593
|
+
const originalPush = history.push.bind(history)
|
|
594
|
+
const originalReplace = history.replace.bind(history)
|
|
595
|
+
internal.originalPush = originalPush
|
|
596
|
+
internal.originalReplace = originalReplace
|
|
597
|
+
|
|
598
|
+
history.push = (to: To, state?: unknown) => {
|
|
599
|
+
const guard = internal.guard
|
|
600
|
+
if (!guard || internal.phase === 'draining') {
|
|
601
|
+
originalPush(to, state)
|
|
602
|
+
return
|
|
603
|
+
}
|
|
604
|
+
if (internal.pendingNav) {
|
|
605
|
+
console.error('[overlayBack] a navigation is already pending; dropping the conflicting push')
|
|
606
|
+
return
|
|
607
|
+
}
|
|
608
|
+
internal.pendingNav = { kind: 'PUSH', to, state }
|
|
609
|
+
if (internal.phase === null) requestCloseForPendingNavigation(win, internal, guard)
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
history.replace = (to: To, state?: unknown) => {
|
|
613
|
+
const guard = internal.guard
|
|
614
|
+
if (!guard || internal.phase === 'draining') {
|
|
615
|
+
originalReplace(to, state)
|
|
616
|
+
return
|
|
617
|
+
}
|
|
618
|
+
if (internal.phase === null && pathOf(to, history.location.pathname) === history.location.pathname) {
|
|
619
|
+
originalReplace(to, state)
|
|
620
|
+
const rawState = readState(win)
|
|
621
|
+
try {
|
|
622
|
+
win.history.replaceState(
|
|
623
|
+
{ ...rawState, [OVERLAY_BACK_MARKER_KEY]: guard.marker },
|
|
624
|
+
'',
|
|
625
|
+
win.location.href,
|
|
626
|
+
)
|
|
627
|
+
} catch {
|
|
628
|
+
/* fail open: the replace already landed, just without a re-merged marker */
|
|
629
|
+
}
|
|
630
|
+
return
|
|
631
|
+
}
|
|
632
|
+
if (internal.pendingNav) {
|
|
633
|
+
console.error('[overlayBack] a navigation is already pending; dropping the conflicting replace')
|
|
634
|
+
return
|
|
635
|
+
}
|
|
636
|
+
internal.pendingNav = { kind: 'REPLACE', to, state }
|
|
637
|
+
if (internal.phase === null) requestCloseForPendingNavigation(win, internal, guard)
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** Test-only introspection of the window-singleton state. The returned object is the live state
|
|
642
|
+
* (not a copy), so a test may also mutate `lastGestureAt`/`lastPopstateAt` to simulate the arming
|
|
643
|
+
* gate, since jsdom cannot dispatch a genuinely trusted gesture event. Never import this outside
|
|
644
|
+
* tests. */
|
|
645
|
+
export function __inspectOverlayBackForTests__(win: Window): OverlayBackInternalState | undefined {
|
|
646
|
+
return win.__ASMA_OVERLAY_BACK_INTERNAL__
|
|
647
|
+
}
|