@vitrinka/web 0.4.0 → 0.5.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/README.md +34 -1
  3. package/build/hud.d.ts +4 -1
  4. package/build/hud.iife.js +6 -6
  5. package/build/one.iife.js +1 -0
  6. package/build/recorder/RecorderProvider.js +48 -9
  7. package/build/recorder/capture/console.d.ts +2 -0
  8. package/build/recorder/capture/console.js +22 -2
  9. package/build/recorder/capture/nav.d.ts +13 -2
  10. package/build/recorder/capture/nav.js +49 -11
  11. package/build/recorder/capture/net.d.ts +2 -0
  12. package/build/recorder/capture/net.js +30 -4
  13. package/build/recorder/control.d.ts +17 -0
  14. package/build/recorder/control.js +39 -2
  15. package/build/recorder/hud/Hud.js +70 -20
  16. package/build/recorder/hud/RecorderPill.js +6 -2
  17. package/build/recorder/hud/chords.d.ts +10 -0
  18. package/build/recorder/hud/chords.js +34 -0
  19. package/build/recorder/hud/controller.d.ts +24 -0
  20. package/build/recorder/hud/flow.d.ts +22 -1
  21. package/build/recorder/hud/flow.js +16 -1
  22. package/build/recorder/index.d.ts +1 -0
  23. package/build/recorder/index.js +1 -0
  24. package/build/recorder/lead.d.ts +90 -0
  25. package/build/recorder/lead.js +404 -0
  26. package/build/recorder/one-iife.d.ts +13 -0
  27. package/build/recorder/one-iife.js +13 -0
  28. package/build/recorder/one.d.ts +170 -0
  29. package/build/recorder/one.js +292 -0
  30. package/build/recorder/page-controller.js +23 -2
  31. package/build/recorder/queue.js +5 -2
  32. package/build/recorder/report.js +9 -2
  33. package/build/recorder/session.d.ts +22 -4
  34. package/build/recorder/session.js +70 -20
  35. package/build/recorder/state.d.ts +9 -0
  36. package/build/recorder/state.js +7 -0
  37. package/build/recorder/version.d.ts +3 -0
  38. package/build/recorder/version.js +3 -0
  39. package/package.json +5 -3
@@ -0,0 +1,404 @@
1
+ import { announce, answeredBy, ask, documentEnv, isLive, markPage, nonce, readClaim, releaseClaim, sendCommand, serveCommands, serveHello, takeClaim, unmarkPage, updateClaim, watchClaim, } from './one';
2
+ import { getState, stoppingElsewhere } from './queue';
3
+ import { leadState, notify, subscribe } from './state';
4
+ import { RECORDER_ID } from './version';
5
+ /**
6
+ * A Start refused because another recorder leads (D12). `code` is
7
+ * `recorder_busy`; the message is shown verbatim where Start was pressed.
8
+ */
9
+ export class RecorderBusyError extends Error {
10
+ code = 'recorder_busy';
11
+ leader;
12
+ constructor(message, leader) {
13
+ super(message);
14
+ this.name = 'RecorderBusy';
15
+ this.leader = leader;
16
+ }
17
+ }
18
+ /** How often a follower pings the holder. */
19
+ export const PING_MS = 10_000;
20
+ const self = { from: 'page', inst: nonce('p') };
21
+ let env = null;
22
+ let users = 0;
23
+ let handlers = null;
24
+ let teardown = null;
25
+ /** Extension parties (by instance) and the recording each says is live. */
26
+ const extension = new Map();
27
+ let starting = false;
28
+ let stopping = null;
29
+ let face = 'none';
30
+ let remoteStop = null;
31
+ let pingTimer = null;
32
+ // -- what this document knows --------------------------------------------------
33
+ function heldClaim() {
34
+ if (!env)
35
+ return null;
36
+ const c = readClaim(env);
37
+ return c?.id === self.inst ? c : null;
38
+ }
39
+ function extensionLive() {
40
+ for (const l of extension.values())
41
+ if (isLive(l.state))
42
+ return l;
43
+ return null;
44
+ }
45
+ /** A claim of another holder (any state: a `saved` one keeps its face, D7), else the extension elsewhere. */
46
+ function computeFollowing() {
47
+ if (!env)
48
+ return null;
49
+ const c = readClaim(env);
50
+ if (c)
51
+ return c.id === self.inst ? null : c.by;
52
+ return extensionLive() ? 'extension' : null;
53
+ }
54
+ function liveOf() {
55
+ const c = heldClaim();
56
+ return c ? { by: 'page', state: c.state, here: true, id: c.id } : null;
57
+ }
58
+ /** The claim this recorder should hold now; null = none. */
59
+ function desired(held) {
60
+ if (starting)
61
+ return { state: 'starting', sessionId: '' };
62
+ const rec = getState();
63
+ if (rec) {
64
+ const state = stopping === rec.sessionId || stoppingElsewhere(rec) ? 'stopping' : rec.paused ? 'paused' : 'recording';
65
+ return { state, sessionId: rec.sessionId };
66
+ }
67
+ // The stop's face still shows on this pill: hold the claim as `saved` (D7).
68
+ if (held && (face === 'saving' || face === 'saved' || face === 'failed'))
69
+ return { state: 'saved', sessionId: held.sessionId };
70
+ return null;
71
+ }
72
+ /** Write the claim this recorder should hold — never over another holder (D14). */
73
+ function syncClaim() {
74
+ if (!env)
75
+ return;
76
+ const cur = readClaim(env);
77
+ const held = cur?.id === self.inst ? cur : null;
78
+ const want = desired(held);
79
+ if (!want) {
80
+ if (held && releaseClaim(env, self.inst))
81
+ announce(env, self, null);
82
+ return;
83
+ }
84
+ if (held) {
85
+ if (held.state !== want.state || held.sessionId !== want.sessionId) {
86
+ updateClaim(env, self.inst, want);
87
+ announce(env, self, liveOf());
88
+ }
89
+ return;
90
+ }
91
+ // A live session of this page's own (a resume, a cross-tab join) claims an
92
+ // unheld document only: another holder keeps it, and an extension
93
+ // recording elsewhere keeps the browser — the session waits (D14).
94
+ if (want.state === 'saved' || cur || extensionLive())
95
+ return;
96
+ // Another recorder bundle in THIS document may have ended (or replaced)
97
+ // the session meanwhile, and storage events never reach the document that
98
+ // wrote them: the durable record decides before the cache resumes.
99
+ if (!starting && handlers?.resync && !handlers.resync())
100
+ return;
101
+ if (takeClaim(env, self, { id: self.inst, ...want }).ok)
102
+ announce(env, self, liveOf());
103
+ }
104
+ function armPing() {
105
+ const want = leadState.following !== null;
106
+ if (want && !pingTimer)
107
+ pingTimer = setInterval(pingLeader, PING_MS);
108
+ else if (!want && pingTimer) {
109
+ clearInterval(pingTimer);
110
+ pingTimer = null;
111
+ }
112
+ }
113
+ /** Re-derive the claim and who leads; announce a change of leader to the lanes and the HUD. */
114
+ function refresh() {
115
+ if (!env)
116
+ return;
117
+ syncClaim();
118
+ const next = computeFollowing();
119
+ if (next === leadState.following)
120
+ return;
121
+ leadState.following = next;
122
+ armPing();
123
+ notify();
124
+ }
125
+ /** Take the extension parties' word from a complete set of answers. */
126
+ function absorbAnswers(answers) {
127
+ extension.clear();
128
+ for (const h of answers)
129
+ if (h.from === 'extension' && h.live)
130
+ extension.set(h.inst, h.live);
131
+ }
132
+ /**
133
+ * Ping (D5): ask who is here, take the extension's word, clear a holder that
134
+ * no longer answers (under its own id), re-derive. A follower runs it every
135
+ * `PING_MS`, on becoming visible and before a remote command, so a page
136
+ * never waits on an extension that was reloaded or uninstalled.
137
+ */
138
+ export function pingLeader() {
139
+ if (!env)
140
+ return;
141
+ const answers = ask(env, self);
142
+ absorbAnswers(answers);
143
+ const cur = readClaim(env);
144
+ if (cur && cur.id !== self.inst && !answeredBy(answers, cur))
145
+ releaseClaim(env, cur.id);
146
+ refresh();
147
+ }
148
+ function onHello(h) {
149
+ // Another page bundle speaks through the claim; the extension's word on
150
+ // a recording elsewhere arrives only here.
151
+ if (h.from !== 'extension')
152
+ return;
153
+ if (h.gone || !h.live)
154
+ extension.delete(h.inst);
155
+ else
156
+ extension.set(h.inst, h.live);
157
+ refresh();
158
+ }
159
+ function onClaimChange() {
160
+ if (!env)
161
+ return;
162
+ const cur = readClaim(env);
163
+ // A new holder must still answer (an orphaned one never will).
164
+ if (cur && cur.id !== self.inst)
165
+ pingLeader();
166
+ else
167
+ refresh();
168
+ }
169
+ /** The holder's side of a remote command (D11): synchronous, inside its dispatch. */
170
+ function onCommand(c) {
171
+ const held = heldClaim();
172
+ if (!held)
173
+ return null;
174
+ const rec = getState();
175
+ if (!rec || held.state === 'saved')
176
+ return { ok: false, code: 'not_recording', error: 'the recording has ended' };
177
+ if (held.state === 'starting')
178
+ return { ok: false, code: 'not_recording', error: 'the recording is still starting' };
179
+ if (held.state === 'stopping')
180
+ return { ok: false, code: 'stopping', error: `still saving #${rec.sessionId}` };
181
+ try {
182
+ switch (c.op) {
183
+ case 'pause':
184
+ if (rec.dead)
185
+ return { ok: false, code: 'not_recording', error: 'the server ended this recording' };
186
+ handlers?.pause();
187
+ return { ok: true };
188
+ case 'stop':
189
+ if (remoteStop)
190
+ remoteStop();
191
+ else
192
+ handlers?.stop();
193
+ return { ok: true };
194
+ case 'note': {
195
+ const text = c.text?.trim() ?? '';
196
+ if (!text)
197
+ return { ok: false, code: 'bad_command', error: 'a note needs text' };
198
+ if (rec.dead)
199
+ return { ok: false, code: 'not_recording', error: 'the server ended this recording' };
200
+ if (rec.paused)
201
+ return { ok: false, code: 'bad_command', error: 'the recording is paused — resume it to add a note' };
202
+ handlers?.note(text);
203
+ return { ok: true };
204
+ }
205
+ }
206
+ }
207
+ catch (e) {
208
+ console.warn(`vitrinka: remote ${c.op} failed`, e);
209
+ return { ok: false, code: 'bad_command', error: e instanceof Error ? e.message : String(e) };
210
+ }
211
+ }
212
+ // -- the root's door -----------------------------------------------------------
213
+ /**
214
+ * Join the channel for this document; returns the leave. Idempotent across
215
+ * mounts (the last handlers win). `at` is the document's env unless a test
216
+ * passes its own.
217
+ */
218
+ export function startLead(h, at = documentEnv()) {
219
+ handlers = h;
220
+ if (!at)
221
+ return () => undefined;
222
+ users++;
223
+ if (!env) {
224
+ env = at;
225
+ const e = at;
226
+ // First: the extension's one.js answers only where this mark stands.
227
+ markPage(e, RECORDER_ID);
228
+ const offHello = serveHello(e, self, liveOf, onHello);
229
+ const offCommands = serveCommands(e, self, onCommand);
230
+ const offWatch = watchClaim(e, onClaimChange);
231
+ const offState = subscribe(refresh);
232
+ const doc = globalThis.document;
233
+ const onVisible = () => {
234
+ if (doc?.visibilityState === 'visible' && leadState.following)
235
+ pingLeader();
236
+ };
237
+ doc?.addEventListener('visibilitychange', onVisible);
238
+ teardown = () => {
239
+ doc?.removeEventListener('visibilitychange', onVisible);
240
+ offState();
241
+ offWatch();
242
+ offCommands();
243
+ releaseClaim(e, self.inst);
244
+ // Another recorder bundle still mounted here keeps the mark.
245
+ const others = ask(e, self).some((x) => x.from === 'page');
246
+ announce(e, self, null, { gone: true });
247
+ offHello();
248
+ if (!others)
249
+ unmarkPage(e);
250
+ };
251
+ // The mount's ask: who records, here or elsewhere — then claim a resumed session, or follow.
252
+ pingLeader();
253
+ }
254
+ let left = false;
255
+ return () => {
256
+ if (left)
257
+ return;
258
+ left = true;
259
+ users--;
260
+ if (users > 0)
261
+ return;
262
+ teardown?.();
263
+ resetLead();
264
+ };
265
+ }
266
+ /** The root left: forget the channel. The HUD's registrations (its face, its Stop flow) are the controller's and stay. */
267
+ function resetLead() {
268
+ teardown = null;
269
+ env = null;
270
+ handlers = null;
271
+ extension.clear();
272
+ starting = false;
273
+ stopping = null;
274
+ if (pingTimer)
275
+ clearInterval(pingTimer);
276
+ pingTimer = null;
277
+ if (leadState.following !== null) {
278
+ leadState.following = null;
279
+ notify();
280
+ }
281
+ }
282
+ // -- the session's doors -------------------------------------------------------
283
+ function busy(leader, here, sessionId) {
284
+ const n = sessionId ? ` (#${sessionId})` : '';
285
+ if (leader === 'page')
286
+ return new RecorderBusyError(`another recorder on this page is already recording${n} — use its pill, or stop it first`, 'page');
287
+ return here
288
+ ? new RecorderBusyError(`the Vitrinka extension is already recording this page${n} — use its pill, or stop it from the extension`, 'extension')
289
+ : new RecorderBusyError('the Vitrinka extension is recording in another tab — stop it there first', 'extension');
290
+ }
291
+ /**
292
+ * The Start check-and-set (D4, D12): synchronous, before any network call.
293
+ * Throws a `RecorderBusyError` while another recorder leads — a live claim
294
+ * whose holder answers, or the extension recording in another tab — else
295
+ * claims the document as `starting`. A no-op without a mounted root.
296
+ */
297
+ export function claimForStart() {
298
+ if (!env)
299
+ return;
300
+ if (starting)
301
+ throw new Error('vitrinka: a session is already starting');
302
+ const answers = ask(env, self);
303
+ absorbAnswers(answers);
304
+ const cur = readClaim(env);
305
+ if (cur && cur.id !== self.inst && isLive(cur.state)) {
306
+ if (answeredBy(answers, cur)) {
307
+ refresh();
308
+ throw busy(cur.by, true, cur.sessionId);
309
+ }
310
+ releaseClaim(env, cur.id);
311
+ }
312
+ const ext = extensionLive();
313
+ if (ext) {
314
+ refresh();
315
+ throw busy('extension', ext.here, '');
316
+ }
317
+ // A session of this page's own — live, or waiting while another recorder's
318
+ // Saved face still shows (D14) — is never replaced by a new one.
319
+ if (getState())
320
+ throw new Error('vitrinka: a session is already recording');
321
+ const taken = takeClaim(env, self, { id: self.inst, state: 'starting' });
322
+ if (!taken.ok) {
323
+ refresh();
324
+ throw busy(taken.holder.by, true, taken.holder.sessionId);
325
+ }
326
+ starting = true;
327
+ announce(env, self, liveOf());
328
+ refresh();
329
+ }
330
+ /** The Start settled: created (the claim moves on with the session) or not (it goes). */
331
+ export function settleStart(created) {
332
+ if (!starting)
333
+ return;
334
+ starting = false;
335
+ if (!created && env && getState() === null)
336
+ releaseClaim(env, self.inst);
337
+ refresh();
338
+ }
339
+ /** This document's Stop is draining `sessionId` (null: it settled). */
340
+ export function setLeadStopping(sessionId) {
341
+ stopping = sessionId;
342
+ refresh();
343
+ }
344
+ // -- the controller's doors ----------------------------------------------------
345
+ /** The HUD's flow face (D7). */
346
+ export function holdFace(f) {
347
+ if (face === f)
348
+ return;
349
+ face = f;
350
+ refresh();
351
+ }
352
+ /** The HUD's Stop flow, run by a remote stop (D11); returns the unregister. */
353
+ export function setRemoteStop(run) {
354
+ remoteStop = run;
355
+ return () => {
356
+ if (remoteStop === run)
357
+ remoteStop = null;
358
+ };
359
+ }
360
+ export function leadStatus() {
361
+ const claim = env ? readClaim(env) : null;
362
+ const following = leadState.following;
363
+ const leader = following ?? (claim?.id === self.inst || getState() ? 'page' : null);
364
+ return { leader, following, claim };
365
+ }
366
+ function refusal(op, code, error, holder) {
367
+ if (code === 'not_recording')
368
+ return op === 'note' ? 'the recording ended before the note arrived' : (error ?? 'the recording has ended');
369
+ if (code === 'stopping')
370
+ return `still saving${holder.sessionId ? ` #${holder.sessionId}` : ''}`;
371
+ return error ?? 'the recorder refused the command';
372
+ }
373
+ /**
374
+ * A follower's pause, stop or note, acted on by the leader (D11). Pings the
375
+ * holder first; throws with the reason when it is gone, refused, or the
376
+ * extension leads from another tab (whose endpoint here takes no command).
377
+ */
378
+ export function sendToLeader(op, text) {
379
+ if (!env)
380
+ throw new Error('vitrinka: no session is recording');
381
+ pingLeader();
382
+ const holder = readClaim(env);
383
+ if (!holder || holder.id === self.inst || !isLive(holder.state)) {
384
+ if (extensionLive())
385
+ throw new Error('the Vitrinka extension is recording in another tab — use it there');
386
+ throw new Error(op === 'note' ? 'the recording ended before the note arrived' : 'vitrinka: no session is recording');
387
+ }
388
+ const ack = sendCommand(env, self, op, text);
389
+ if (!ack) {
390
+ // No synchronous answer: the holder is gone (D5).
391
+ releaseClaim(env, holder.id);
392
+ refresh();
393
+ throw new Error('the recorder that held this recording is gone');
394
+ }
395
+ if (!ack.ok)
396
+ throw new Error(refusal(op, ack.code, ack.error, holder));
397
+ }
398
+ export function __resetLeadForTests() {
399
+ teardown?.();
400
+ users = 0;
401
+ resetLead();
402
+ face = 'none';
403
+ remoteStop = null;
404
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Entry of `build/one.iife.js`: the ONE recorder protocol (`one.ts`) as one
3
+ * self-contained script — no React, no imports — for the browser extension's
4
+ * content scripts, which have no bundler. It defines `globalThis.VitrinkaOne`:
5
+ *
6
+ * const env = VitrinkaOne.documentEnv();
7
+ * const claim = VitrinkaOne.readClaim(env);
8
+ *
9
+ * The extension vendors it beside `build/hud.iife.js` under the same pin
10
+ * (`tools/vendor-hud`), so both worlds speak one codec.
11
+ */
12
+ import * as one from './one';
13
+ export type VitrinkaOneGlobal = typeof one;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Entry of `build/one.iife.js`: the ONE recorder protocol (`one.ts`) as one
3
+ * self-contained script — no React, no imports — for the browser extension's
4
+ * content scripts, which have no bundler. It defines `globalThis.VitrinkaOne`:
5
+ *
6
+ * const env = VitrinkaOne.documentEnv();
7
+ * const claim = VitrinkaOne.readClaim(env);
8
+ *
9
+ * The extension vendors it beside `build/hud.iife.js` under the same pin
10
+ * (`tools/vendor-hud`), so both worlds speak one codec.
11
+ */
12
+ import * as one from './one';
13
+ globalThis.VitrinkaOne = Object.freeze({ ...one });
@@ -0,0 +1,170 @@
1
+ /** Protocol version; a reader takes the fields it knows from a higher one. */
2
+ export declare const ONE_VERSION = 1;
3
+ /** `<html>` attribute: a page recorder is mounted here (its recorder id, e.g. `web/0.5.0`). */
4
+ export declare const PAGE_ATTR = "data-vitrinka-page";
5
+ /** `<html>` attribute: the claim (JSON). */
6
+ export declare const LEAD_ATTR = "data-vitrinka-lead";
7
+ export declare const HELLO_EVENT = "vitrinka:hello";
8
+ export declare const COMMAND_EVENT = "vitrinka:command";
9
+ export declare const ACK_EVENT = "vitrinka:ack";
10
+ export type OneParty = 'page' | 'extension';
11
+ /** `saved` = the leader's pill still shows Saving / Saved / failed after a stop (D7); every other state is live. */
12
+ export type ClaimState = 'starting' | 'recording' | 'paused' | 'stopping' | 'saved';
13
+ export type CommandOp = 'pause' | 'stop' | 'note';
14
+ export type AckCode = 'not_recording' | 'stopping' | 'not_here' | 'bad_command';
15
+ /** Who records this document. No title, workspace, URL or credential: any script on the page reads it. */
16
+ export interface Claim {
17
+ v: number;
18
+ by: OneParty;
19
+ /** The holder: a page recorder instance's nonce, or the extension recording's generation. */
20
+ id: string;
21
+ state: ClaimState;
22
+ /** `''` while `starting`. */
23
+ sessionId: string;
24
+ /** Epoch ms the claim was taken. */
25
+ since: number;
26
+ }
27
+ /** What a party says about the recording it knows of. */
28
+ export interface Live {
29
+ by: OneParty;
30
+ state: ClaimState;
31
+ /** The sender holds this document's claim. */
32
+ here: boolean;
33
+ /** The claim's `id` when `here`. */
34
+ id?: string;
35
+ }
36
+ export interface Hello {
37
+ v: number;
38
+ from: OneParty;
39
+ /** The sender instance; every party ignores events carrying its own. */
40
+ inst: string;
41
+ /** Asks every live party to answer with its own hello, inside this dispatch. */
42
+ ask?: true;
43
+ live?: Live | null;
44
+ /** The sender is leaving. */
45
+ gone?: true;
46
+ }
47
+ export interface Command {
48
+ v: number;
49
+ from: OneParty;
50
+ inst: string;
51
+ /** This command's id; its ack carries it back. */
52
+ id: string;
53
+ op: CommandOp;
54
+ /** A note's text. */
55
+ text?: string;
56
+ }
57
+ export interface Ack {
58
+ v: number;
59
+ from: OneParty;
60
+ inst: string;
61
+ id: string;
62
+ ok: boolean;
63
+ code?: AckCode;
64
+ error?: string;
65
+ }
66
+ /** What a party answers a command with; `null` = not the holder (no answer at all). */
67
+ export type CommandReply = {
68
+ ok: true;
69
+ } | {
70
+ ok: false;
71
+ code: AckCode;
72
+ error?: string;
73
+ };
74
+ /** The attribute surface of `<html>` the claim lives on. */
75
+ export interface ClaimRoot {
76
+ getAttribute(name: string): string | null;
77
+ setAttribute(name: string, value: string): void;
78
+ removeAttribute(name: string): void;
79
+ }
80
+ export interface OneEnv {
81
+ /** `document.documentElement`. */
82
+ root: ClaimRoot;
83
+ /** `window`: where actions travel. */
84
+ bus: EventTarget;
85
+ /** Calls back after the claim attribute changed; returns the stop. Default: a MutationObserver on `root`. */
86
+ watch?: (fn: () => void) => () => void;
87
+ }
88
+ /** A party on the channel: which side it is and its instance nonce. */
89
+ export interface OneSelf {
90
+ from: OneParty;
91
+ inst: string;
92
+ }
93
+ /** The document's env; null where there is no DOM (SSR, a worker). */
94
+ export declare function documentEnv(): OneEnv | null;
95
+ /** A fresh nonce: `p.k3x9q2…` for a page instance. */
96
+ export declare function nonce(prefix: string): string;
97
+ /** Parse a claim attribute; null for anything that is not one. */
98
+ export declare function parseClaim(raw: string | null | undefined): Claim | null;
99
+ export declare function serializeClaim(c: Claim): string;
100
+ /** Live = a recording is (or is about to be) under way; `saved` is not. */
101
+ export declare function isLive(state: ClaimState | null | undefined): boolean;
102
+ export declare function parseHello(raw: unknown): Hello | null;
103
+ /** A command; null when it does not parse or names an unknown op (treated as absent). */
104
+ export declare function parseCommand(raw: unknown): Command | null;
105
+ export declare function parseAck(raw: unknown): Ack | null;
106
+ export declare function readClaim(env: OneEnv): Claim | null;
107
+ export type TakeResult = {
108
+ ok: true;
109
+ claim: Claim;
110
+ } | {
111
+ ok: false;
112
+ holder: Claim;
113
+ };
114
+ /**
115
+ * The synchronous check-and-set (D4): take this document's claim unless a
116
+ * LIVE claim of another holder stands and that holder still answers (D5).
117
+ * A `saved` claim, a stale one and the caller's own are taken over. Call it
118
+ * before any network call, inside one task.
119
+ */
120
+ export declare function takeClaim(env: OneEnv, self: OneSelf, next: {
121
+ id: string;
122
+ state: ClaimState;
123
+ sessionId?: string;
124
+ }): TakeResult;
125
+ /** Move the holder's claim to `state` (and `sessionId`); false when the claim is not `id`'s. */
126
+ export declare function updateClaim(env: OneEnv, id: string, patch: {
127
+ state: ClaimState;
128
+ sessionId?: string;
129
+ }): boolean;
130
+ /** Remove the claim only if it is `id`'s — the holder releasing, or anyone clearing a stale one. */
131
+ export declare function releaseClaim(env: OneEnv, id: string): boolean;
132
+ /** Call back on every change of the claim attribute; returns the stop. */
133
+ export declare function watchClaim(env: OneEnv, fn: () => void): () => void;
134
+ export declare function markPage(env: OneEnv, recorderId: string): void;
135
+ export declare function unmarkPage(env: OneEnv): void;
136
+ export declare function pageMarked(env: OneEnv): boolean;
137
+ /**
138
+ * Ask who is here: every live party answers inside this dispatch. Returns
139
+ * the answers (never the caller's own).
140
+ */
141
+ export declare function ask(env: OneEnv, self: OneSelf): Hello[];
142
+ /** Say what this party knows (no `ask`): an answer, a change, or `gone` on leaving. */
143
+ export declare function announce(env: OneEnv, self: OneSelf, live: Live | null, opts?: {
144
+ gone?: boolean;
145
+ }): void;
146
+ /**
147
+ * Serve the hello channel: an ask is answered at once with `live()`; any
148
+ * other hello (an answer to someone else, a change, a `gone`) reaches
149
+ * `onHello`. The party's own dispatches are ignored. `answerIf` keeps a
150
+ * party silent where it must not speak — the extension's `one.js` answers
151
+ * only in documents that carry `data-vitrinka-page` (D6).
152
+ */
153
+ export declare function serveHello(env: OneEnv, self: OneSelf, live: () => Live | null, onHello?: (h: Hello) => void, opts?: {
154
+ answerIf?: () => boolean;
155
+ }): () => void;
156
+ /** Did the claim's holder answer? Matched on its `id` when the answer names one, else on its side. */
157
+ export declare function answeredBy(answers: readonly Hello[], holder: Claim): boolean;
158
+ /** Ping the claim's holder (D5): true when it answers inside the dispatch. */
159
+ export declare function holderAnswers(env: OneEnv, self: OneSelf, holder: Claim): boolean;
160
+ /**
161
+ * Send a command to the claim's holder; its ack arrives inside this
162
+ * dispatch. Null = nobody answered (the holder is gone, D5).
163
+ */
164
+ export declare function sendCommand(env: OneEnv, self: OneSelf, op: CommandOp, text?: string): Ack | null;
165
+ /**
166
+ * Serve commands: `handle` answers a parsed command (another party's, a
167
+ * known op) with its reply — synchronously, inside the command's dispatch —
168
+ * or `null` when this party is not the holder, which sends no ack at all.
169
+ */
170
+ export declare function serveCommands(env: OneEnv, self: OneSelf, handle: (c: Command) => CommandReply | null): () => void;