@vitrinka/web 0.3.1 → 0.3.3
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/CHANGELOG.md +49 -0
- package/README.md +5 -0
- package/build/hud.d.ts +1 -1
- package/build/hud.iife.js +7 -7
- package/build/protocol/index.d.ts +11 -1
- package/build/recorder/RecorderProvider.js +4 -1
- package/build/recorder/control.js +2 -1
- package/build/recorder/hud/Hud.js +2 -2
- package/build/recorder/hud/RecorderPill.js +12 -4
- package/build/recorder/hud/controller.d.ts +11 -6
- package/build/recorder/hud/flow.d.ts +5 -1
- package/build/recorder/hud/flow.js +6 -1
- package/build/recorder/page-controller.js +5 -5
- package/build/recorder/queue.d.ts +38 -0
- package/build/recorder/queue.js +153 -4
- package/build/recorder/recents.d.ts +32 -0
- package/build/recorder/recents.js +58 -2
- package/build/recorder/report.d.ts +1 -0
- package/build/recorder/report.js +5 -3
- package/build/recorder/session.d.ts +10 -2
- package/build/recorder/session.js +64 -9
- package/build/recorder/storage/index.d.ts +6 -0
- package/build/recorder/storage/index.js +11 -0
- package/package.json +1 -1
|
@@ -65,10 +65,20 @@ export interface SessionReconcileResponse {
|
|
|
65
65
|
status?: string;
|
|
66
66
|
deletedAt?: string | null;
|
|
67
67
|
}
|
|
68
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* PATCH response when a session completes. The board is projected AFTER the
|
|
70
|
+
* stop, so `boardUrl` is usually absent here: `projection` says how far it
|
|
71
|
+
* got, and the session read names the board once it exists.
|
|
72
|
+
*/
|
|
69
73
|
export interface SessionDone {
|
|
70
74
|
boardSlug?: string;
|
|
75
|
+
/** The board's address, minted by the server; only once the board exists. */
|
|
76
|
+
boardUrl?: string;
|
|
77
|
+
/** An older server's spelling of `boardUrl`. */
|
|
71
78
|
board?: {
|
|
72
79
|
url?: string;
|
|
73
80
|
};
|
|
81
|
+
projection?: {
|
|
82
|
+
state?: string;
|
|
83
|
+
};
|
|
74
84
|
}
|
|
@@ -18,7 +18,7 @@ import { installUnauthorizedHandler } from './link';
|
|
|
18
18
|
import { insideHud } from './hud/host';
|
|
19
19
|
import { armReconcile, flush, getState, persistNow, reconcile, scheduleFlush } from './queue';
|
|
20
20
|
import { dropFlight, syncFlight } from './report';
|
|
21
|
-
import { onBeforeStop, recoverRedactionPolicy } from './session';
|
|
21
|
+
import { followOtherTabs, onBeforeStop, recoverRedactionPolicy } from './session';
|
|
22
22
|
import { annotateState, setTabIdentity, subscribe } from './state';
|
|
23
23
|
const TAB_KEY = 'vitrinka.tab';
|
|
24
24
|
/** One lane per browser tab: a per-tab id kept in sessionStorage (survives reloads). */
|
|
@@ -84,6 +84,8 @@ export function RecorderProvider({ config, children, }) {
|
|
|
84
84
|
};
|
|
85
85
|
const unsubscribe = subscribe(syncLanes);
|
|
86
86
|
const offBeforeStop = onBeforeStop(flushRRWeb);
|
|
87
|
+
// Another tab's Start, pause or Stop reaches this one (syncLanes follows).
|
|
88
|
+
const unfollow = followOtherTabs();
|
|
87
89
|
// A reload mid-session: the durable tail needs a drain, the reconcile
|
|
88
90
|
// poll re-arming, the policy re-applying, and the new document is a nav.
|
|
89
91
|
scheduleFlush();
|
|
@@ -113,6 +115,7 @@ export function RecorderProvider({ config, children, }) {
|
|
|
113
115
|
document.removeEventListener('visibilitychange', onVisible);
|
|
114
116
|
unsubscribe();
|
|
115
117
|
offBeforeStop();
|
|
118
|
+
unfollow();
|
|
116
119
|
uninstallClicks();
|
|
117
120
|
uninstallControl();
|
|
118
121
|
uninstall401();
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* the HUD, and read a status snapshot back.
|
|
6
6
|
*/
|
|
7
7
|
import { getState, health } from './queue';
|
|
8
|
+
import { boardUrlOf } from './recents';
|
|
8
9
|
import { addNote, elapsedOf, startSession, stopSession, togglePause } from './session';
|
|
9
10
|
export function snapshot() {
|
|
10
11
|
const rec = getState();
|
|
@@ -42,7 +43,7 @@ export function installControl() {
|
|
|
42
43
|
async stop() {
|
|
43
44
|
const captured = snapshot();
|
|
44
45
|
const done = await stopSession();
|
|
45
|
-
return { ...captured, boardUrl: done
|
|
46
|
+
return { ...captured, boardUrl: boardUrlOf(done) ?? captured.boardUrl };
|
|
46
47
|
},
|
|
47
48
|
note(text) {
|
|
48
49
|
const t = text.trim();
|
|
@@ -201,7 +201,7 @@ export function Hud({ controller, hostMount, defaultTitle, storage }) {
|
|
|
201
201
|
return;
|
|
202
202
|
sent.then((res) => {
|
|
203
203
|
setReportDraft('');
|
|
204
|
-
settle({ type: 'saved', ...
|
|
204
|
+
settle({ type: 'saved', ...res });
|
|
205
205
|
}, (e) => {
|
|
206
206
|
console.warn('vitrinka: report —', errorText(e));
|
|
207
207
|
settle({ type: 'failed', message: errorText(e) });
|
|
@@ -259,7 +259,7 @@ export function Hud({ controller, hostMount, defaultTitle, storage }) {
|
|
|
259
259
|
const runStop = useCallback(() => {
|
|
260
260
|
const t0 = Date.now();
|
|
261
261
|
const settle = (ev) => setTimeout(() => dispatch(ev), Math.max(0, MIN_SAVING_MS - (Date.now() - t0)));
|
|
262
|
-
controller.stop().then((r) => settle({ type: 'saved', ...
|
|
262
|
+
controller.stop().then((r) => settle({ type: 'saved', ...r }), (e) => {
|
|
263
263
|
console.warn('vitrinka: stop —', errorText(e));
|
|
264
264
|
settle({ type: 'failed', message: errorText(e) });
|
|
265
265
|
});
|
|
@@ -139,10 +139,13 @@ export function RecorderPill(p) {
|
|
|
139
139
|
const rec = p.snap.recording;
|
|
140
140
|
const annotating = p.snap.annotating;
|
|
141
141
|
const hold = p.menuOpen || p.composing || annotating || face === 'flow';
|
|
142
|
-
// Content a closing segment keeps for its closing frames
|
|
142
|
+
// Content a closing segment keeps for its closing frames — an ended
|
|
143
|
+
// recording's clock stands where it stopped, never ticking on.
|
|
143
144
|
const lastRec = useRef(rec);
|
|
144
145
|
if (rec)
|
|
145
146
|
lastRec.current = rec;
|
|
147
|
+
else if (lastRec.current && lastRec.current.resumedAt !== null)
|
|
148
|
+
lastRec.current = { ...lastRec.current, activeMs: elapsedOf(lastRec.current, p.now), resumedAt: null };
|
|
146
149
|
const lastFlow = useRef(p.flow);
|
|
147
150
|
if (p.flow.face !== 'none')
|
|
148
151
|
lastFlow.current = p.flow;
|
|
@@ -247,8 +250,10 @@ export function RecorderPill(p) {
|
|
|
247
250
|
return (_jsx("button", { type: "button", className: "tab", "aria-label": "Show recorder", "data-state": bad ? 'bad' : rec && !rec.paused ? 'rec' : 'idle', ...p.handle, onClick: () => p.onMove({ spot: untuck(p.place) }) }));
|
|
248
251
|
}
|
|
249
252
|
const chip = shownRec ? syncChip(shownRec.sync, p.now) : null;
|
|
250
|
-
|
|
251
|
-
const
|
|
253
|
+
// The state is the LIVE recording's: an ended one lends its closing frames content, never its state.
|
|
254
|
+
const live = rec ? syncChip(rec.sync, p.now).kind : null;
|
|
255
|
+
const bad = live === 'offline' || live === 'error';
|
|
256
|
+
const state = bad ? 'bad' : !rec || rec.paused || rec.dead ? 'paused' : 'rec';
|
|
252
257
|
const unlinked = !p.snap.linked;
|
|
253
258
|
const puckLabel = unlinked ? 'Link recorder' : 'Start recording';
|
|
254
259
|
const confirming = p.flow.face === 'confirm';
|
|
@@ -297,7 +302,10 @@ export function RecorderPill(p) {
|
|
|
297
302
|
};
|
|
298
303
|
const flowShown = shownFlow.face;
|
|
299
304
|
const confirmAction = shownFlow.face === 'confirm' ? shownFlow.action : 'stop';
|
|
300
|
-
|
|
305
|
+
// Saved before its board was built: the link arrives on the recent once the server names it.
|
|
306
|
+
const savedUrl = shownFlow.face === 'saved'
|
|
307
|
+
? (shownFlow.boardUrl ?? p.snap.recents.find((r) => r.sessionId === shownFlow.sessionId)?.boardUrl)
|
|
308
|
+
: undefined;
|
|
301
309
|
const failure = shownFlow.face === 'failed' ? shownFlow.message : '';
|
|
302
310
|
// A bug report's send: "Sending…", "Sent", "Couldn't send" — and it can always be retried.
|
|
303
311
|
const report = 'report' in shownFlow && shownFlow.report === true;
|
|
@@ -136,6 +136,15 @@ export interface HudReport {
|
|
|
136
136
|
/** The marked element's selector; '' for a region or no mark. */
|
|
137
137
|
selector: string;
|
|
138
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* What a stop or an idle report saved. `boardUrl` when the board already
|
|
141
|
+
* exists; else `sessionId` names the recent whose link the Saved face takes
|
|
142
|
+
* once the server has built the board.
|
|
143
|
+
*/
|
|
144
|
+
export interface HudSaved {
|
|
145
|
+
boardUrl?: string;
|
|
146
|
+
sessionId?: string;
|
|
147
|
+
}
|
|
139
148
|
export interface HudController {
|
|
140
149
|
getSnapshot(): HudSnapshot;
|
|
141
150
|
subscribe(listener: () => void): () => void;
|
|
@@ -144,9 +153,7 @@ export interface HudController {
|
|
|
144
153
|
}): Promise<void>;
|
|
145
154
|
togglePause(): Promise<void>;
|
|
146
155
|
/** Drains, then closes the session; rejects (session kept) while unreachable. */
|
|
147
|
-
stop(): Promise<
|
|
148
|
-
boardUrl?: string;
|
|
149
|
-
}>;
|
|
156
|
+
stop(): Promise<HudSaved>;
|
|
150
157
|
note(text: string): void;
|
|
151
158
|
annotate(a: HudAnnotation): void;
|
|
152
159
|
setAnnotating(active: boolean): void;
|
|
@@ -170,8 +177,6 @@ export interface HudController {
|
|
|
170
177
|
* second call after a rejection retries the same report. Optional: a host
|
|
171
178
|
* without it never offers the menu row.
|
|
172
179
|
*/
|
|
173
|
-
report?(r: HudReport): Promise<
|
|
174
|
-
boardUrl?: string;
|
|
175
|
-
}>;
|
|
180
|
+
report?(r: HudReport): Promise<HudSaved>;
|
|
176
181
|
}
|
|
177
182
|
export declare const DEFAULT_PREFS: HudPrefs;
|
|
@@ -25,9 +25,12 @@ export type Flow = {
|
|
|
25
25
|
face: 'saving';
|
|
26
26
|
total: number;
|
|
27
27
|
report?: true;
|
|
28
|
-
}
|
|
28
|
+
}
|
|
29
|
+
/** `sessionId` lets a face saved before its board existed take the link from recents. */
|
|
30
|
+
| {
|
|
29
31
|
face: 'saved';
|
|
30
32
|
boardUrl?: string;
|
|
33
|
+
sessionId?: string;
|
|
31
34
|
report?: true;
|
|
32
35
|
} | {
|
|
33
36
|
face: 'failed';
|
|
@@ -52,6 +55,7 @@ export type FlowEvent = {
|
|
|
52
55
|
} | {
|
|
53
56
|
type: 'saved';
|
|
54
57
|
boardUrl?: string;
|
|
58
|
+
sessionId?: string;
|
|
55
59
|
} | {
|
|
56
60
|
type: 'failed';
|
|
57
61
|
message: string;
|
|
@@ -34,7 +34,12 @@ export function flowReducer(flow, ev) {
|
|
|
34
34
|
return flow.face === 'failed' ? { face: 'saving', total: ev.queued, ...reportOf(flow) } : flow;
|
|
35
35
|
case 'saved':
|
|
36
36
|
return flow.face === 'saving'
|
|
37
|
-
? {
|
|
37
|
+
? {
|
|
38
|
+
face: 'saved',
|
|
39
|
+
...(ev.boardUrl ? { boardUrl: ev.boardUrl } : {}),
|
|
40
|
+
...(ev.sessionId ? { sessionId: ev.sessionId } : {}),
|
|
41
|
+
...reportOf(flow),
|
|
42
|
+
}
|
|
38
43
|
: flow;
|
|
39
44
|
case 'failed':
|
|
40
45
|
return flow.face === 'saving' ? { face: 'failed', message: ev.message, ...reportOf(flow) } : flow;
|
|
@@ -2,7 +2,7 @@ import { readLink, recorderConfig, vitrinkaLinked } from './config';
|
|
|
2
2
|
import { forgetLink, linkDevice } from './link';
|
|
3
3
|
import { cachedAccount, cachedPrefs, fetchMe, savePrefs } from './me';
|
|
4
4
|
import { getState, health } from './queue';
|
|
5
|
-
import { readRecents, refreshRecents } from './recents';
|
|
5
|
+
import { boardUrlOf, readRecents, refreshRecents } from './recents';
|
|
6
6
|
import { canReport, holdReport, sendReport } from './report';
|
|
7
7
|
import { addAnnotation, addNote, RECORDER_ID, startSession, stopSession, togglePause } from './session';
|
|
8
8
|
import { annotateState, setAnnotating, subscribe } from './state';
|
|
@@ -71,13 +71,13 @@ export function createPageController() {
|
|
|
71
71
|
},
|
|
72
72
|
async stop() {
|
|
73
73
|
const boardUrl = getState()?.boardUrl;
|
|
74
|
-
const
|
|
74
|
+
const sessionId = getState()?.sessionId;
|
|
75
75
|
const done = await stopSession();
|
|
76
76
|
// null for a live session = the server refused to close it (ended locally, not saved).
|
|
77
|
-
if (
|
|
77
|
+
if (sessionId && done === null)
|
|
78
78
|
throw new Error('the server refused to close this session — it ended locally, not saved');
|
|
79
|
-
const url = done
|
|
80
|
-
return url ? { boardUrl: url } : {};
|
|
79
|
+
const url = boardUrlOf(done) ?? boardUrl;
|
|
80
|
+
return { ...(url ? { boardUrl: url } : {}), ...(sessionId ? { sessionId } : {}) };
|
|
81
81
|
},
|
|
82
82
|
note: (text) => addNote(text),
|
|
83
83
|
annotate: ({ text, rect, selector, task }) => addAnnotation(text, rect, selector, { task }),
|
|
@@ -61,8 +61,27 @@ export interface SessionState {
|
|
|
61
61
|
* reload re-applies the same rules instead of silently reverting.
|
|
62
62
|
*/
|
|
63
63
|
policy?: RedactionPolicy | null;
|
|
64
|
+
/**
|
|
65
|
+
* When (ISO) another tab last renewed its claim to be stopping this
|
|
66
|
+
* session: the copy it stores reads as paused with its clock frozen at the
|
|
67
|
+
* Stop, so every other tab stops capturing and painting rec at once, not
|
|
68
|
+
* when the save ends. The stopping tab renews the mark every
|
|
69
|
+
* `STOPPING_RENEW_MS` for as long as its Stop is out (however long a
|
|
70
|
+
* request hangs); pause and resume are refused on a mark younger than
|
|
71
|
+
* `STOPPING_TTL_MS` (`stoppingElsewhere`). An older one outlived its tab
|
|
72
|
+
* and is an ordinary pause. Stop always works.
|
|
73
|
+
*/
|
|
74
|
+
stopping?: string;
|
|
64
75
|
}
|
|
76
|
+
/** How often a tab renews its `stopping` mark while its Stop is out. */
|
|
77
|
+
export declare const STOPPING_RENEW_MS = 20000;
|
|
78
|
+
/** A mark not renewed for this long outlived its tab: several missed renewals, and past the one wake a minute Chrome allows a long-hidden tab. */
|
|
79
|
+
export declare const STOPPING_TTL_MS = 90000;
|
|
80
|
+
/** Is another tab still stopping `rec` (a fresh `stopping` mark)? */
|
|
81
|
+
export declare function stoppingElsewhere(rec: SessionState): boolean;
|
|
65
82
|
export type { RecorderEvent };
|
|
83
|
+
/** Storage key of the session record — shared by every tab of the origin. */
|
|
84
|
+
export declare const REC_KEY = "rec";
|
|
66
85
|
/**
|
|
67
86
|
* The LIVE session record — a mutable alias of the cache, not a copy. Treat it
|
|
68
87
|
* as READ-ONLY unless you pass the object you mutated straight to `setState()`
|
|
@@ -70,6 +89,25 @@ export type { RecorderEvent };
|
|
|
70
89
|
*/
|
|
71
90
|
export declare function getState(): SessionState | null;
|
|
72
91
|
export declare function setState(rec: SessionState | null): void;
|
|
92
|
+
/**
|
|
93
|
+
* This tab started (`sessionId`) or finished (null) stopping a session. Only
|
|
94
|
+
* the STORED record changes: this tab's own cache stays live, so the tail
|
|
95
|
+
* its Stop still drains (rrweb's last events, settled captures) lands.
|
|
96
|
+
* Finishing writes the record back without the mark.
|
|
97
|
+
*/
|
|
98
|
+
export declare function markStopping(sessionId: string | null, activeMs?: number, renewMs?: number): void;
|
|
99
|
+
export type StoredChange = 'joined' | 'updated' | 'ended' | 'unchanged';
|
|
100
|
+
/**
|
|
101
|
+
* Another document of this origin (a second tab) wrote the session record.
|
|
102
|
+
* The store is shared, this cache is not — a tab that joined a recording on
|
|
103
|
+
* load would otherwise keep painting and capturing it after another tab
|
|
104
|
+
* stopped it. So: a stop there ends the session here, dropping this tab's
|
|
105
|
+
* undelivered tail (the server no longer accepts it); a session started
|
|
106
|
+
* there is joined, as a reload joins it; the same session takes the stored
|
|
107
|
+
* record (a pause, a dead verdict) but never a lower seq than this tab
|
|
108
|
+
* already allocated. Nothing is written back.
|
|
109
|
+
*/
|
|
110
|
+
export declare function adoptStoredState(raw: string | null): StoredChange;
|
|
73
111
|
/** Write the in-memory buffer (and the pending chunks) to storage NOW. */
|
|
74
112
|
export declare function persistNow(): void;
|
|
75
113
|
export declare function queuedCount(): number;
|
package/build/recorder/queue.js
CHANGED
|
@@ -39,6 +39,14 @@ export const RECONCILE_MS = 10_000;
|
|
|
39
39
|
const OFFLINE_AFTER_MS = 15_000;
|
|
40
40
|
const BACKLOG_ITEMS = 40;
|
|
41
41
|
const kv = () => getRecorderStorage();
|
|
42
|
+
/** How often a tab renews its `stopping` mark while its Stop is out. */
|
|
43
|
+
export const STOPPING_RENEW_MS = 20_000;
|
|
44
|
+
/** A mark not renewed for this long outlived its tab: several missed renewals, and past the one wake a minute Chrome allows a long-hidden tab. */
|
|
45
|
+
export const STOPPING_TTL_MS = 90_000;
|
|
46
|
+
/** Is another tab still stopping `rec` (a fresh `stopping` mark)? */
|
|
47
|
+
export function stoppingElsewhere(rec) {
|
|
48
|
+
return rec.stopping !== undefined && Date.now() - Date.parse(rec.stopping) < STOPPING_TTL_MS;
|
|
49
|
+
}
|
|
42
50
|
function readJson(key, fallback) {
|
|
43
51
|
const raw = kv().getString(key);
|
|
44
52
|
if (!raw)
|
|
@@ -65,6 +73,8 @@ function safeSet(key, value) {
|
|
|
65
73
|
return false;
|
|
66
74
|
}
|
|
67
75
|
}
|
|
76
|
+
/** Storage key of the session record — shared by every tab of the origin. */
|
|
77
|
+
export const REC_KEY = 'rec';
|
|
68
78
|
// The session record is small and read on every event — cache it in memory and
|
|
69
79
|
// write through, so the capture hot path never parses JSON.
|
|
70
80
|
let recCache;
|
|
@@ -75,15 +85,104 @@ let recCache;
|
|
|
75
85
|
*/
|
|
76
86
|
export function getState() {
|
|
77
87
|
if (recCache === undefined)
|
|
78
|
-
recCache = readJson(
|
|
88
|
+
recCache = readJson(REC_KEY, null);
|
|
79
89
|
return recCache;
|
|
80
90
|
}
|
|
81
91
|
export function setState(rec) {
|
|
82
92
|
recCache = rec;
|
|
83
93
|
if (rec === null)
|
|
84
|
-
kv().remove(
|
|
94
|
+
kv().remove(REC_KEY);
|
|
85
95
|
else
|
|
86
|
-
safeSet(
|
|
96
|
+
safeSet(REC_KEY, JSON.stringify(stored(rec)));
|
|
97
|
+
}
|
|
98
|
+
/** The session this tab is stopping, its clock at the Stop, and when its mark was last renewed. */
|
|
99
|
+
let stoppingHere = null;
|
|
100
|
+
let stoppingLease = null;
|
|
101
|
+
/** What the other tabs read of `rec`: while this tab stops it, the frozen `stopping` copy. */
|
|
102
|
+
function stored(rec) {
|
|
103
|
+
if (stoppingHere?.sessionId !== rec.sessionId)
|
|
104
|
+
return rec;
|
|
105
|
+
return { ...rec, paused: true, activeMs: stoppingHere.activeMs, resumeAt: null, stopping: stoppingHere.at };
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* This tab started (`sessionId`) or finished (null) stopping a session. Only
|
|
109
|
+
* the STORED record changes: this tab's own cache stays live, so the tail
|
|
110
|
+
* its Stop still drains (rrweb's last events, settled captures) lands.
|
|
111
|
+
* Finishing writes the record back without the mark.
|
|
112
|
+
*/
|
|
113
|
+
export function markStopping(sessionId, activeMs = 0, renewMs = STOPPING_RENEW_MS) {
|
|
114
|
+
const was = stoppingHere?.sessionId;
|
|
115
|
+
if (stoppingLease)
|
|
116
|
+
clearInterval(stoppingLease);
|
|
117
|
+
stoppingLease = null;
|
|
118
|
+
stoppingHere = sessionId ? { sessionId, activeMs, at: new Date().toISOString() } : null;
|
|
119
|
+
// The lease: a Stop whose request hangs is still this tab's, never mistaken for a gone tab's.
|
|
120
|
+
if (sessionId)
|
|
121
|
+
stoppingLease = setInterval(() => renewStopping(sessionId), renewMs);
|
|
122
|
+
const live = getState();
|
|
123
|
+
if (!live || (live.sessionId !== sessionId && live.sessionId !== was))
|
|
124
|
+
return;
|
|
125
|
+
delete live.stopping;
|
|
126
|
+
setState(live);
|
|
127
|
+
}
|
|
128
|
+
/** Re-stamp this tab's `stopping` mark while its Stop is still out. */
|
|
129
|
+
function renewStopping(sessionId) {
|
|
130
|
+
if (stoppingHere?.sessionId !== sessionId)
|
|
131
|
+
return;
|
|
132
|
+
stoppingHere.at = new Date().toISOString();
|
|
133
|
+
const live = getState();
|
|
134
|
+
if (live?.sessionId === sessionId)
|
|
135
|
+
setState(live);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Another document of this origin (a second tab) wrote the session record.
|
|
139
|
+
* The store is shared, this cache is not — a tab that joined a recording on
|
|
140
|
+
* load would otherwise keep painting and capturing it after another tab
|
|
141
|
+
* stopped it. So: a stop there ends the session here, dropping this tab's
|
|
142
|
+
* undelivered tail (the server no longer accepts it); a session started
|
|
143
|
+
* there is joined, as a reload joins it; the same session takes the stored
|
|
144
|
+
* record (a pause, a dead verdict) but never a lower seq than this tab
|
|
145
|
+
* already allocated. Nothing is written back.
|
|
146
|
+
*/
|
|
147
|
+
export function adoptStoredState(raw) {
|
|
148
|
+
let next = null;
|
|
149
|
+
if (raw) {
|
|
150
|
+
try {
|
|
151
|
+
next = JSON.parse(raw);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return 'unchanged';
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const cur = getState();
|
|
158
|
+
if (!next) {
|
|
159
|
+
if (!cur)
|
|
160
|
+
return 'unchanged';
|
|
161
|
+
recCache = null;
|
|
162
|
+
dropLocalTail();
|
|
163
|
+
disarmReconcile();
|
|
164
|
+
return 'ended';
|
|
165
|
+
}
|
|
166
|
+
if (cur?.sessionId === next.sessionId) {
|
|
167
|
+
next.seq = Math.max(next.seq, cur.seq);
|
|
168
|
+
// A policy this tab already holds outlives a record written before it.
|
|
169
|
+
if (next.policy === undefined && cur.policy !== undefined)
|
|
170
|
+
next.policy = cur.policy;
|
|
171
|
+
// A session this tab is stopping comes back as the frozen copy it wrote
|
|
172
|
+
// (another tab wrote its cache back): this tab's clock and capture stay live.
|
|
173
|
+
if (stoppingHere?.sessionId === next.sessionId) {
|
|
174
|
+
next.paused = cur.paused;
|
|
175
|
+
next.activeMs = cur.activeMs;
|
|
176
|
+
next.resumeAt = cur.resumeAt;
|
|
177
|
+
delete next.stopping;
|
|
178
|
+
}
|
|
179
|
+
recCache = next;
|
|
180
|
+
return 'updated';
|
|
181
|
+
}
|
|
182
|
+
recCache = next;
|
|
183
|
+
// A tail this tab still held belongs to another session, never to this one.
|
|
184
|
+
dropLocalTail();
|
|
185
|
+
return 'joined';
|
|
87
186
|
}
|
|
88
187
|
/**
|
|
89
188
|
* The event buffer lives in memory and is FLUSHED TO STORAGE on a debounce
|
|
@@ -92,6 +191,12 @@ export function setState(rec) {
|
|
|
92
191
|
const PERSIST_DEBOUNCE_MS = 700;
|
|
93
192
|
let bufferCache;
|
|
94
193
|
let persistTimer = null;
|
|
194
|
+
/**
|
|
195
|
+
* This tab let go of its tail for another tab's session (dropLocalTail): its
|
|
196
|
+
* empty caches must not overwrite the stored tail that tab owns — not even
|
|
197
|
+
* on pagehide — until this tab queues work of its own again.
|
|
198
|
+
*/
|
|
199
|
+
let tailDropped = false;
|
|
95
200
|
function getBuffer() {
|
|
96
201
|
if (bufferCache === undefined)
|
|
97
202
|
bufferCache = readJson('buffer', []);
|
|
@@ -107,10 +212,11 @@ function persistBuffer() {
|
|
|
107
212
|
clearTimeout(persistTimer);
|
|
108
213
|
persistTimer = null;
|
|
109
214
|
}
|
|
110
|
-
if (bufferCache !== undefined)
|
|
215
|
+
if (bufferCache !== undefined && !tailDropped)
|
|
111
216
|
safeSet('buffer', JSON.stringify(bufferCache));
|
|
112
217
|
}
|
|
113
218
|
function schedulePersist() {
|
|
219
|
+
tailDropped = false;
|
|
114
220
|
if (persistTimer)
|
|
115
221
|
return;
|
|
116
222
|
persistTimer = setTimeout(() => {
|
|
@@ -121,6 +227,7 @@ function schedulePersist() {
|
|
|
121
227
|
}
|
|
122
228
|
function setBuffer(buffer) {
|
|
123
229
|
bufferCache = buffer;
|
|
230
|
+
tailDropped = false;
|
|
124
231
|
persistBuffer();
|
|
125
232
|
}
|
|
126
233
|
// -- pending rrweb chunks ----------------------------------------------------
|
|
@@ -135,6 +242,8 @@ function getChunks() {
|
|
|
135
242
|
}
|
|
136
243
|
/** Best-effort: persisted while small, memory-only (and said so) beyond the budget. */
|
|
137
244
|
function persistChunks() {
|
|
245
|
+
if (tailDropped)
|
|
246
|
+
return;
|
|
138
247
|
const chunks = getChunks();
|
|
139
248
|
if (chunks.length === 0) {
|
|
140
249
|
kv().remove('chunks');
|
|
@@ -149,6 +258,7 @@ function persistChunks() {
|
|
|
149
258
|
function setChunks(chunks) {
|
|
150
259
|
chunkCache = chunks;
|
|
151
260
|
chunkBytes = chunks.reduce((n, c) => n + c.body.length, 0);
|
|
261
|
+
tailDropped = false;
|
|
152
262
|
persistChunks();
|
|
153
263
|
}
|
|
154
264
|
export function queuedCount() {
|
|
@@ -308,6 +418,14 @@ export async function capturesSettled(deadlineMs = CAPTURES_SETTLE_MS) {
|
|
|
308
418
|
}
|
|
309
419
|
return true;
|
|
310
420
|
}
|
|
421
|
+
/**
|
|
422
|
+
* Did this tab's session change while an upload for `rec` was out — ended
|
|
423
|
+
* or replaced by another tab (adoptStoredState)? Then the upload's outcome
|
|
424
|
+
* must not touch the queue, which belongs to the next session now.
|
|
425
|
+
*/
|
|
426
|
+
function movedOn(rec) {
|
|
427
|
+
return getState()?.sessionId !== rec.sessionId;
|
|
428
|
+
}
|
|
311
429
|
/** Is `id` the session currently in state AND still accepting capture? */
|
|
312
430
|
export function isSessionLive(id) {
|
|
313
431
|
const rec = getState();
|
|
@@ -467,6 +585,10 @@ async function drainPending(limit = 5) {
|
|
|
467
585
|
}
|
|
468
586
|
try {
|
|
469
587
|
const up = await uploadChunk(rec.sessionId, item.seq, item.body);
|
|
588
|
+
// Another tab ended or replaced the session while this upload was out:
|
|
589
|
+
// its tail is gone here, and nothing of it may re-enter the queue.
|
|
590
|
+
if (movedOn(rec))
|
|
591
|
+
return getChunks().length === 0;
|
|
470
592
|
// The event row joins the buffer only now, keeping its original seq —
|
|
471
593
|
// a late retry fills the stream's gap.
|
|
472
594
|
pushRawEvent({
|
|
@@ -482,6 +604,8 @@ async function drainPending(limit = 5) {
|
|
|
482
604
|
gone.add(item.seq);
|
|
483
605
|
}
|
|
484
606
|
catch (e) {
|
|
607
|
+
if (movedOn(rec))
|
|
608
|
+
return getChunks().length === 0;
|
|
485
609
|
if (e instanceof VitrinkaApiError && permanentStatus(e.status)) {
|
|
486
610
|
console.warn(`vitrinka: rrweb chunk seq ${item.seq} rejected permanently (${e.status}) — dropped`);
|
|
487
611
|
gone.add(item.seq);
|
|
@@ -570,6 +694,8 @@ async function flushInner(opts) {
|
|
|
570
694
|
await api('POST', `/api/v1/sessions/${rec.sessionId}/events`, { events: batch }, opts);
|
|
571
695
|
}
|
|
572
696
|
catch (e) {
|
|
697
|
+
if (movedOn(rec))
|
|
698
|
+
return false;
|
|
573
699
|
if (e instanceof VitrinkaApiError && permanentStatus(e.status)) {
|
|
574
700
|
markSessionDead(`server rejected this session (${e.status})`);
|
|
575
701
|
return false;
|
|
@@ -580,6 +706,10 @@ async function flushInner(opts) {
|
|
|
580
706
|
scheduleFlush();
|
|
581
707
|
return false;
|
|
582
708
|
}
|
|
709
|
+
// Acked for a session this tab no longer holds: the buffer is another
|
|
710
|
+
// session's now, and its seqs restart — never filter it by these.
|
|
711
|
+
if (movedOn(rec))
|
|
712
|
+
return true;
|
|
583
713
|
noteSync();
|
|
584
714
|
serverMaxSeq = Math.max(serverMaxSeq, batch[batch.length - 1]?.seq ?? serverMaxSeq);
|
|
585
715
|
notify();
|
|
@@ -611,6 +741,20 @@ export function resetQueues() {
|
|
|
611
741
|
setBuffer([]);
|
|
612
742
|
setChunks([]);
|
|
613
743
|
}
|
|
744
|
+
/**
|
|
745
|
+
* Forget this tab's buffers WITHOUT writing storage: another tab owns the
|
|
746
|
+
* stored record now, and its own tail there must survive.
|
|
747
|
+
*/
|
|
748
|
+
function dropLocalTail() {
|
|
749
|
+
if (persistTimer) {
|
|
750
|
+
clearTimeout(persistTimer);
|
|
751
|
+
persistTimer = null;
|
|
752
|
+
}
|
|
753
|
+
bufferCache = [];
|
|
754
|
+
chunkCache = [];
|
|
755
|
+
chunkBytes = 0;
|
|
756
|
+
tailDropped = true;
|
|
757
|
+
}
|
|
614
758
|
/** Test-only: drop all module state so suites cannot leak into each other. */
|
|
615
759
|
export function __resetForTests() {
|
|
616
760
|
__dropCachesForTests();
|
|
@@ -639,6 +783,11 @@ export function __dropCachesForTests() {
|
|
|
639
783
|
flushTimer = null;
|
|
640
784
|
}
|
|
641
785
|
flushBusy = false;
|
|
786
|
+
tailDropped = false;
|
|
787
|
+
stoppingHere = null;
|
|
788
|
+
if (stoppingLease)
|
|
789
|
+
clearInterval(stoppingLease);
|
|
790
|
+
stoppingLease = null;
|
|
642
791
|
recCache = undefined;
|
|
643
792
|
bufferCache = undefined;
|
|
644
793
|
chunkCache = undefined;
|
|
@@ -14,10 +14,42 @@ export declare function readRecents(): readonly HudRecent[];
|
|
|
14
14
|
export declare function noteRecent(entry: HudRecent): void;
|
|
15
15
|
/** Patch a recording in place; a no-op for one not listed. */
|
|
16
16
|
export declare function updateRecent(sessionId: string, patch: Partial<Omit<HudRecent, 'sessionId'>>): void;
|
|
17
|
+
/** What the reconcile read answers (the fields a recent can use). */
|
|
18
|
+
interface SessionRead {
|
|
19
|
+
status?: string;
|
|
20
|
+
deletedAt?: string | null;
|
|
21
|
+
boardUrl?: string;
|
|
22
|
+
board?: {
|
|
23
|
+
url?: string;
|
|
24
|
+
};
|
|
25
|
+
projection?: {
|
|
26
|
+
state?: string;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** A session answer's board link: the server's `boardUrl`, or an older server's `board.url`. */
|
|
30
|
+
export declare function boardUrlOf(s: Pick<SessionRead, 'boardUrl' | 'board'> | null | undefined): string | undefined;
|
|
31
|
+
/** How often, and how long, a saved session is read until its board exists (the extension's D11 numbers). */
|
|
32
|
+
export declare const BOARD_POLL_MS = 1500;
|
|
33
|
+
export declare const BOARD_WAIT_MS: number;
|
|
34
|
+
/**
|
|
35
|
+
* The server answered a stop (or a report) before the session's board was
|
|
36
|
+
* built — it projects after the stop, stills first, 30–60 s. Read the
|
|
37
|
+
* session until it names its board and put the link on the recent, where
|
|
38
|
+
* the Saved face and the menu pick it up. Ends on a failed or empty
|
|
39
|
+
* projection, a gone or refused session, or after `forMs`; a transient
|
|
40
|
+
* error keeps waiting. Never rejects.
|
|
41
|
+
*/
|
|
42
|
+
export declare function awaitBoard(sessionId: string, { everyMs, forMs }?: {
|
|
43
|
+
everyMs?: number;
|
|
44
|
+
forMs?: number;
|
|
45
|
+
}): Promise<void>;
|
|
17
46
|
/**
|
|
18
47
|
* Fill the board links the list lacks — `live` (the recording in progress)
|
|
19
48
|
* is skipped, its link arrives with its stop. Never rejects.
|
|
20
49
|
*/
|
|
21
50
|
export declare function refreshRecents(live: string | null): Promise<void>;
|
|
51
|
+
/** Another tab wrote the list (its stop saved a recording): read it afresh. */
|
|
52
|
+
export declare function forgetRecents(): void;
|
|
22
53
|
/** Test-only: forget the in-memory list (storage stays). */
|
|
23
54
|
export declare function __resetRecentsForTests(): void;
|
|
55
|
+
export {};
|