@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.
@@ -1,4 +1,4 @@
1
- import { api, VitrinkaApiError } from './api';
1
+ import { api, permanentStatus, VitrinkaApiError } from './api';
2
2
  import { notify } from './state';
3
3
  import { getRecorderStorage } from './storage';
4
4
  export const RECENTS_KEY = 'recents';
@@ -47,6 +47,53 @@ export function updateRecent(sessionId, patch) {
47
47
  return;
48
48
  write(list.map((r) => (r.sessionId === sessionId ? { ...r, ...patch } : r)));
49
49
  }
50
+ /** A session answer's board link: the server's `boardUrl`, or an older server's `board.url`. */
51
+ export function boardUrlOf(s) {
52
+ return s?.boardUrl || s?.board?.url || undefined;
53
+ }
54
+ /** How often, and how long, a saved session is read until its board exists (the extension's D11 numbers). */
55
+ export const BOARD_POLL_MS = 1_500;
56
+ export const BOARD_WAIT_MS = 10 * 60_000;
57
+ const awaiting = new Set();
58
+ /**
59
+ * The server answered a stop (or a report) before the session's board was
60
+ * built — it projects after the stop, stills first, 30–60 s. Read the
61
+ * session until it names its board and put the link on the recent, where
62
+ * the Saved face and the menu pick it up. Ends on a failed or empty
63
+ * projection, a gone or refused session, or after `forMs`; a transient
64
+ * error keeps waiting. Never rejects.
65
+ */
66
+ export async function awaitBoard(sessionId, { everyMs = BOARD_POLL_MS, forMs = BOARD_WAIT_MS } = {}) {
67
+ if (awaiting.has(sessionId))
68
+ return;
69
+ awaiting.add(sessionId);
70
+ const until = Date.now() + forMs;
71
+ try {
72
+ while (Date.now() < until) {
73
+ await new Promise((res) => setTimeout(res, everyMs));
74
+ try {
75
+ const s = await api('GET', `/api/v1/sessions/${sessionId}`);
76
+ const boardUrl = boardUrlOf(s);
77
+ if (boardUrl)
78
+ return updateRecent(sessionId, { boardUrl });
79
+ if (s.deletedAt)
80
+ return updateRecent(sessionId, { status: 'deleted' });
81
+ if (s.projection?.state === 'failed' || s.projection?.state === 'empty')
82
+ return;
83
+ }
84
+ catch (e) {
85
+ if (e instanceof VitrinkaApiError && e.status === 404)
86
+ return updateRecent(sessionId, { status: 'deleted' });
87
+ if (e instanceof VitrinkaApiError && permanentStatus(e.status))
88
+ return;
89
+ console.warn(`vitrinka: waiting for the board of session ${sessionId}`, e);
90
+ }
91
+ }
92
+ }
93
+ finally {
94
+ awaiting.delete(sessionId);
95
+ }
96
+ }
50
97
  /** Ids already asked this document — a refused read is not retried on every menu open. */
51
98
  const asked = new Set();
52
99
  /**
@@ -59,11 +106,14 @@ export async function refreshRecents(live) {
59
106
  asked.add(r.sessionId);
60
107
  try {
61
108
  const s = await api('GET', `/api/v1/sessions/${r.sessionId}`);
62
- const boardUrl = s.boardUrl ?? s.board?.url;
109
+ const boardUrl = boardUrlOf(s);
63
110
  if (s.deletedAt)
64
111
  updateRecent(r.sessionId, { status: 'deleted' });
65
112
  else if (boardUrl)
66
113
  updateRecent(r.sessionId, { boardUrl, ...(s.status === 'done' ? { status: 'saved' } : {}) });
114
+ // Still projecting (the document that waited for it reloaded or left): wait here instead.
115
+ else if (s.projection?.state === 'running')
116
+ void awaitBoard(r.sessionId);
67
117
  }
68
118
  catch (e) {
69
119
  if (e instanceof VitrinkaApiError && e.status === 404)
@@ -73,8 +123,14 @@ export async function refreshRecents(live) {
73
123
  }
74
124
  }
75
125
  }
126
+ /** Another tab wrote the list (its stop saved a recording): read it afresh. */
127
+ export function forgetRecents() {
128
+ cache = undefined;
129
+ notify();
130
+ }
76
131
  /** Test-only: forget the in-memory list (storage stays). */
77
132
  export function __resetRecentsForTests() {
78
133
  cache = undefined;
79
134
  asked.clear();
135
+ awaiting.clear();
80
136
  }
@@ -29,6 +29,7 @@ export declare function holdReport(on: boolean): void;
29
29
  */
30
30
  export declare function sendReport(note: ReportNote): Promise<{
31
31
  boardUrl?: string;
32
+ sessionId?: string;
32
33
  }>;
33
34
  /** The provider unmounted: forget the buffer and any held clip; a remount re-arms. */
34
35
  export declare function dropFlight(): void;
@@ -4,7 +4,7 @@ import { checkoutRRWeb } from './capture/rrweb';
4
4
  import { recorderConfig, vitrinkaLinked } from './config';
5
5
  import { clipHasSnapshot, flightActive, startFlightBuffer, stopFlightBuffer, takeFlightClip } from './flight';
6
6
  import { EVENTS_PER_POST, getState, MAX_BATCH_BYTES, splitRRWebEvents, utf8Bytes } from './queue';
7
- import { noteRecent } from './recents';
7
+ import { awaitBoard, boardUrlOf, noteRecent } from './recents';
8
8
  import { addAnnotation, createSession, imagePixels } from './session';
9
9
  import { currentRoute, notify } from './state';
10
10
  // -- the gate ----------------------------------------------------------------
@@ -196,7 +196,7 @@ const steps = [
196
196
  (j) => postEvents(j.sessionId, [j.note]),
197
197
  async (j) => {
198
198
  const done = await api('PATCH', `/api/v1/sessions/${j.sessionId}`, { status: 'done' });
199
- const url = done.board?.url;
199
+ const url = boardUrlOf(done);
200
200
  if (url)
201
201
  j.boardUrl = url;
202
202
  },
@@ -244,7 +244,9 @@ export async function sendReport(note) {
244
244
  status: 'saved',
245
245
  ...(j.boardUrl ? { boardUrl: j.boardUrl } : {}),
246
246
  });
247
- return j.boardUrl ? { boardUrl: j.boardUrl } : {};
247
+ if (!j.boardUrl)
248
+ void awaitBoard(j.sessionId);
249
+ return { ...(j.boardUrl ? { boardUrl: j.boardUrl } : {}), sessionId: j.sessionId };
248
250
  }
249
251
  /** The provider unmounted: forget the buffer and any held clip; a remount re-arms. */
250
252
  export function dropFlight() {
@@ -17,8 +17,8 @@ import { type SessionState } from './queue';
17
17
  export { currentRoute, notify, subscribe } from './state';
18
18
  export type { SessionDone } from '../protocol';
19
19
  /** Sent as `meta.recorder`; bumped with the package version. */
20
- export declare const RECORDER_VERSION = "0.3.1";
21
- export declare const RECORDER_ID = "web/0.3.1";
20
+ export declare const RECORDER_VERSION = "0.3.3";
21
+ export declare const RECORDER_ID = "web/0.3.3";
22
22
  /** What `POST /api/v1/sessions` answers (the fields this recorder keeps). */
23
23
  export interface SessionOut {
24
24
  id: string;
@@ -30,6 +30,14 @@ export interface SessionOut {
30
30
  workspace?: string;
31
31
  }
32
32
  export declare function recoverRedactionPolicy(): void;
33
+ /**
34
+ * Keep this document on the recording its other tabs share: the session
35
+ * record and the recents live in storage every tab of the origin reads, but
36
+ * each document caches them. A Stop in one tab ends the session in the
37
+ * others (their pills go idle, their lanes stop sending into it), a Start
38
+ * is joined as a reload joins it, a pause follows. Returns the unsubscribe.
39
+ */
40
+ export declare function followOtherTabs(): () => void;
33
41
  export declare function elapsedOf(rec: SessionState | null): number;
34
42
  export interface StartOptions {
35
43
  title?: string;
@@ -1,12 +1,13 @@
1
1
  import { api, fetchPolicy, permanentStatus, VitrinkaApiError } from './api';
2
2
  import { redactUrl, setRedactionPolicy } from './capture/redact';
3
3
  import { recorderConfig } from './config';
4
- import { armReconcile, capturesSettled, disarmReconcile, drainBuffer, getState, pushEvent, queuedCount, resetHealth, resetIdle, resetQueues, setState, } from './queue';
5
- import { noteRecent, updateRecent } from './recents';
4
+ import { adoptStoredState, armReconcile, capturesSettled, disarmReconcile, drainBuffer, getState, markStopping, pushEvent, queuedCount, REC_KEY, resetHealth, resetIdle, resetQueues, setState, stoppingElsewhere, } from './queue';
5
+ import { awaitBoard, boardUrlOf, forgetRecents, noteRecent, RECENTS_KEY, updateRecent } from './recents';
6
6
  import { currentRoute, notify } from './state';
7
+ import { getRecorderStorage } from './storage';
7
8
  export { currentRoute, notify, subscribe } from './state';
8
9
  /** Sent as `meta.recorder`; bumped with the package version. */
9
- export const RECORDER_VERSION = '0.3.1';
10
+ export const RECORDER_VERSION = '0.3.3';
10
11
  export const RECORDER_ID = `web/${RECORDER_VERSION}`;
11
12
  /**
12
13
  * Re-apply a RECOVERED session's redaction policy after a reload — and
@@ -37,6 +38,39 @@ export function recoverRedactionPolicy() {
37
38
  policyRecoveryInFlight = false;
38
39
  });
39
40
  }
41
+ /**
42
+ * Keep this document on the recording its other tabs share: the session
43
+ * record and the recents live in storage every tab of the origin reads, but
44
+ * each document caches them. A Stop in one tab ends the session in the
45
+ * others (their pills go idle, their lanes stop sending into it), a Start
46
+ * is joined as a reload joins it, a pause follows. Returns the unsubscribe.
47
+ */
48
+ export function followOtherTabs() {
49
+ const storage = getRecorderStorage();
50
+ const offRec = storage.watch?.(REC_KEY, (raw) => {
51
+ const had = getState()?.policy;
52
+ const change = adoptStoredState(raw);
53
+ if (change === 'unchanged')
54
+ return;
55
+ if (change === 'ended')
56
+ setRedactionPolicy(null);
57
+ if (change === 'joined') {
58
+ recoverRedactionPolicy();
59
+ armReconcile();
60
+ }
61
+ // The starting tab writes the workspace policy after its first record:
62
+ // its rules apply here the moment they arrive.
63
+ const policy = getState()?.policy;
64
+ if (change === 'updated' && had === undefined && policy !== undefined)
65
+ setRedactionPolicy(policy);
66
+ notify();
67
+ });
68
+ const offRecents = storage.watch?.(RECENTS_KEY, forgetRecents);
69
+ return () => {
70
+ offRec?.();
71
+ offRecents?.();
72
+ };
73
+ }
40
74
  export function elapsedOf(rec) {
41
75
  if (!rec)
42
76
  return 0;
@@ -142,8 +176,10 @@ async function attachTags(sessionId, tags) {
142
176
  }
143
177
  export async function togglePause() {
144
178
  const rec = getState();
145
- if (!rec || rec.dead)
179
+ if (!rec || rec.dead || stoppingElsewhere(rec))
146
180
  return false;
181
+ // A stale mark: the tab that was stopping it is gone, so this is a plain pause.
182
+ delete rec.stopping;
147
183
  rec.paused = !rec.paused;
148
184
  if (rec.paused) {
149
185
  rec.activeMs = (rec.activeMs || 0) + (rec.resumeAt ? Date.now() - Date.parse(rec.resumeAt) : 0);
@@ -211,6 +247,17 @@ export async function stopSession() {
211
247
  return null;
212
248
  // The recording's length is what the tester saw at Stop, not Stop + drain.
213
249
  const durationMs = elapsedOf(rec);
250
+ // The other tabs leave rec at the Stop, not when the save ends; a stop
251
+ // that keeps the session hands it back to them as it now is.
252
+ markStopping(rec.sessionId, durationMs);
253
+ try {
254
+ return await finishStop(rec, durationMs);
255
+ }
256
+ finally {
257
+ markStopping(null);
258
+ }
259
+ }
260
+ async function finishStop(rec, durationMs) {
214
261
  disarmReconcile();
215
262
  for (const fn of beforeStopHooks) {
216
263
  try {
@@ -236,7 +283,9 @@ export async function stopSession() {
236
283
  console.warn('vitrinka: stopping with unsettled captures — a late event may not make this session');
237
284
  }
238
285
  if (!(await drainBuffer())) {
239
- const held = getState();
286
+ const live = getState();
287
+ // Another tab may have moved on to a session of its own meanwhile — never this stop's to hold.
288
+ const held = live?.sessionId === rec.sessionId ? live : null;
240
289
  if (held?.dead)
241
290
  completeDeadStop(held.deadReason);
242
291
  if (held && !held.paused) {
@@ -264,11 +313,17 @@ export async function stopSession() {
264
313
  }
265
314
  }
266
315
  // A permanent refusal still ends the recording locally, but it was not saved.
267
- const boardUrl = done?.board?.url ?? rec.boardUrl;
316
+ const boardUrl = boardUrlOf(done) ?? rec.boardUrl;
268
317
  updateRecent(rec.sessionId, { status: done ? 'saved' : 'unsaved', durationMs, ...(boardUrl ? { boardUrl } : {}) });
269
- setState(null);
270
- setRedactionPolicy(null);
271
- resetQueues();
318
+ if (done && !boardUrl)
319
+ void awaitBoard(rec.sessionId);
320
+ // Another tab may have ended this session or started the next one while
321
+ // the stop was out: the state and the queues are then not this stop's to clear.
322
+ if (getState()?.sessionId === rec.sessionId) {
323
+ setState(null);
324
+ setRedactionPolicy(null);
325
+ resetQueues();
326
+ }
272
327
  notify();
273
328
  return done;
274
329
  }
@@ -18,6 +18,12 @@ export interface RecorderStorage {
18
18
  set(key: string, value: string): void;
19
19
  /** Delete a key; a no-op when absent. */
20
20
  remove(key: string): void;
21
+ /**
22
+ * Hear another document (a second tab) write `key` — `null` when it was
23
+ * removed; returns the unsubscribe. Optional: a driver no other document
24
+ * shares (memory) has nothing to hear.
25
+ */
26
+ watch?(key: string, onChange: (value: string | null) => void): () => void;
21
27
  }
22
28
  /**
23
29
  * Install a storage driver. Call before the recorder mounts; calling after
@@ -47,6 +47,17 @@ export function localRecorderStorage() {
47
47
  getString: (k) => ls.getItem(PREFIX + k),
48
48
  set: (k, v) => ls.setItem(PREFIX + k, v),
49
49
  remove: (k) => ls.removeItem(PREFIX + k),
50
+ // localStorage is shared by every tab of the origin; its `storage`
51
+ // event reaches the OTHER documents only. A clear() (key null) is not
52
+ // a write of `k` and is ignored.
53
+ watch: (k, onChange) => {
54
+ const on = (e) => {
55
+ if (e.storageArea === ls && e.key === PREFIX + k)
56
+ onChange(e.newValue);
57
+ };
58
+ globalThis.addEventListener('storage', on);
59
+ return () => globalThis.removeEventListener('storage', on);
60
+ },
50
61
  };
51
62
  }
52
63
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitrinka/web",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "vitrinka toolkit for React DOM apps — the journey recorder: capture manual-testing sessions (rrweb DOM stream, clicks, navigation, network, console, notes) straight into vitrinka boards.",
5
5
  "license": "Elastic-2.0",
6
6
  "repository": {