@baukit/data-contracts 0.7.1 → 0.7.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/dist/erasure.d.ts +1 -1
- package/dist/erasure.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/contracts.ts +160 -0
- package/src/durable-draft.test.ts +1214 -0
- package/src/durable-draft.ts +502 -0
- package/src/erasure.test.ts +229 -0
- package/src/erasure.ts +162 -0
- package/src/export.test.ts +167 -0
- package/src/export.ts +164 -0
- package/src/identity.test.ts +465 -0
- package/src/identity.ts +637 -0
- package/src/import-envelope.test.ts +290 -0
- package/src/import-envelope.ts +264 -0
- package/src/import-envelope.vitest.ts +115 -0
- package/src/index.ts +7 -0
- package/src/limits-policy.test.ts +145 -0
- package/src/limits.test.ts +170 -0
- package/src/limits.ts +329 -0
- package/src/memory.test.ts +49 -0
- package/src/memory.ts +375 -0
- package/src/revisioned-drafts.fixture.test.ts +247 -0
- package/src/revisioned-writes.test.ts +684 -0
- package/src/revisioned-writes.ts +374 -0
- package/src/vitest.ts +614 -0
- package/src/web-storage.test.ts +95 -0
- package/src/web-storage.ts +88 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import { type JsonValue, type KeyValueStore, normalizeStorageError } from './contracts.js';
|
|
2
|
+
|
|
3
|
+
/** The result of decoding a stored draft value. Return `corrupt` for any value that fails validation. */
|
|
4
|
+
export type DraftDecodeResult<TValue> =
|
|
5
|
+
| { readonly kind: 'decoded'; readonly value: TValue }
|
|
6
|
+
| { readonly kind: 'corrupt' }
|
|
7
|
+
| { readonly kind: 'unsupported-version' };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Converts a form value to and from stored JSON. The helper stores `{ version, value }` and never
|
|
11
|
+
* calls `decode` for a version above `version`. `decode` receives unchecked JSON and must validate
|
|
12
|
+
* it; an older `version` must be upgraded to the current value shape.
|
|
13
|
+
*/
|
|
14
|
+
export interface DraftCodec<TValue> {
|
|
15
|
+
/** The positive integer written with every save. */
|
|
16
|
+
readonly version: number;
|
|
17
|
+
encode(value: TValue): JsonValue;
|
|
18
|
+
decode(value: JsonValue, version: number): DraftDecodeResult<TValue>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type DraftPersistenceOperation = 'read' | 'write' | 'delete';
|
|
22
|
+
|
|
23
|
+
/** A storage failure. The message names only the operation, never keys or draft content. */
|
|
24
|
+
export class DraftPersistenceError extends Error {
|
|
25
|
+
public override readonly name = 'DraftPersistenceError';
|
|
26
|
+
public readonly code = 'draft_persistence_failed';
|
|
27
|
+
|
|
28
|
+
public constructor(
|
|
29
|
+
public readonly operation: DraftPersistenceOperation,
|
|
30
|
+
options?: ErrorOptions,
|
|
31
|
+
) {
|
|
32
|
+
super(`Draft ${operation} failed.`, options);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** What `open` found in storage. */
|
|
37
|
+
export type DraftRecovery = 'none' | 'restored' | 'corrupt' | 'unsupported-version' | 'unavailable';
|
|
38
|
+
|
|
39
|
+
export type DraftPersistence = 'loading' | 'idle' | 'saving' | 'clearing' | 'failed';
|
|
40
|
+
|
|
41
|
+
export type DraftSubmission = 'none' | 'confirmed';
|
|
42
|
+
|
|
43
|
+
export interface ClosedDurableDraftSnapshot {
|
|
44
|
+
readonly open: false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface OpenDurableDraftSnapshot<TScope, TValue> {
|
|
48
|
+
readonly open: true;
|
|
49
|
+
readonly scope: TScope;
|
|
50
|
+
readonly value: TValue;
|
|
51
|
+
/** Increments on every local change. Pass it to `clear` after a submission. */
|
|
52
|
+
readonly localRevision: number;
|
|
53
|
+
/** True when `value` differs from what storage holds for this scope. */
|
|
54
|
+
readonly dirty: boolean;
|
|
55
|
+
readonly recovery: DraftRecovery;
|
|
56
|
+
readonly persistence: DraftPersistence;
|
|
57
|
+
readonly error: DraftPersistenceError | null;
|
|
58
|
+
/** `confirmed` once the product reported a successful server submission through `clear`. */
|
|
59
|
+
readonly submission: DraftSubmission;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type DurableDraftSnapshot<TScope, TValue> =
|
|
63
|
+
ClosedDurableDraftSnapshot | OpenDurableDraftSnapshot<TScope, TValue>;
|
|
64
|
+
|
|
65
|
+
export type DraftClearRequest =
|
|
66
|
+
| { readonly reason: 'discarded' }
|
|
67
|
+
| { readonly reason: 'submitted'; readonly localRevision: number };
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `stale` means the scope changed, closed, or was reported inactive by `isScopeActive` before the
|
|
71
|
+
* operation started, so storage was not touched.
|
|
72
|
+
*/
|
|
73
|
+
export type DraftSaveOutcome = 'saved' | 'clean' | 'blocked' | 'stale';
|
|
74
|
+
export type DraftClearOutcome = 'cleared' | 'newer-edits-kept' | 'stale';
|
|
75
|
+
export type DraftMoveOutcome = 'moved' | 'blocked' | 'stale';
|
|
76
|
+
|
|
77
|
+
export interface DurableDraftOptions<TScope, TValue> {
|
|
78
|
+
readonly store: KeyValueStore;
|
|
79
|
+
/** Encodes the scope to a storage key. Include the identity and document. */
|
|
80
|
+
readonly key: (scope: TScope) => string;
|
|
81
|
+
readonly codec: DraftCodec<TValue>;
|
|
82
|
+
/**
|
|
83
|
+
* Called before every write or delete. Return false when this editor no longer owns the scope,
|
|
84
|
+
* for example after another editor took a lease on it; the operation then resolves `stale`.
|
|
85
|
+
* Reads are not checked. Every scope is active when omitted.
|
|
86
|
+
*/
|
|
87
|
+
readonly isScopeActive?: (scope: TScope) => boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface DurableDraft<TScope, TValue> {
|
|
91
|
+
getSnapshot(): DurableDraftSnapshot<TScope, TValue>;
|
|
92
|
+
subscribe(listener: () => void): () => void;
|
|
93
|
+
/** Switches to a scope and reads its stored draft. Late work of the previous scope is ignored. */
|
|
94
|
+
open(scope: TScope, initial: TValue): Promise<void>;
|
|
95
|
+
/** Replaces the value in memory. Debounce and call `save` to persist it. */
|
|
96
|
+
update(value: TValue): void;
|
|
97
|
+
/**
|
|
98
|
+
* Writes the current value. Resolves `blocked` without writing while recovery is `corrupt` or
|
|
99
|
+
* `unsupported-version`. Rejects with `DraftPersistenceError` when storage fails.
|
|
100
|
+
*/
|
|
101
|
+
save(): Promise<DraftSaveOutcome>;
|
|
102
|
+
/**
|
|
103
|
+
* Deletes the stored draft. `submitted` keeps the value visible and keeps newer edits made after
|
|
104
|
+
* `localRevision`; `discarded` returns to the initial value. Rejects with a `delete`
|
|
105
|
+
* `DraftPersistenceError` that stays in the snapshot.
|
|
106
|
+
*/
|
|
107
|
+
clear(request: DraftClearRequest): Promise<DraftClearOutcome>;
|
|
108
|
+
/**
|
|
109
|
+
* Moves the open draft to another scope, for example from a "new document" scope to the document
|
|
110
|
+
* the server created. When the value is dirty or stored, it is first written under the new key,
|
|
111
|
+
* replacing what was there. The old key is then deleted in every case. Value, revision, and
|
|
112
|
+
* submission stay as they are, and later saves go to the new key. Resolves `blocked` while recovery is `corrupt` or
|
|
113
|
+
* `unsupported-version`. Rejects with `DraftPersistenceError` when storage fails; the draft then
|
|
114
|
+
* stays on the old scope and the move can be retried.
|
|
115
|
+
*/
|
|
116
|
+
move(scope: TScope): Promise<DraftMoveOutcome>;
|
|
117
|
+
/** Closes the scope without saving and resolves after in-flight storage work settles. */
|
|
118
|
+
close(): Promise<void>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
interface Session<TScope, TValue> {
|
|
122
|
+
scope: TScope;
|
|
123
|
+
key: string;
|
|
124
|
+
readonly initial: TValue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
type LoadResult<TValue> =
|
|
128
|
+
| { readonly recovery: 'none' }
|
|
129
|
+
| { readonly recovery: 'restored'; readonly value: TValue; readonly upgraded: boolean }
|
|
130
|
+
| { readonly recovery: 'corrupt' | 'unsupported-version' };
|
|
131
|
+
|
|
132
|
+
function isEnvelope(stored: JsonValue): stored is { version: number; value: JsonValue } {
|
|
133
|
+
if (typeof stored !== 'object' || stored === null || Array.isArray(stored)) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
const version = stored['version'];
|
|
137
|
+
return (
|
|
138
|
+
typeof version === 'number' && Number.isSafeInteger(version) && version > 0 && 'value' in stored
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function decodeStored<TValue>(
|
|
143
|
+
stored: JsonValue | undefined,
|
|
144
|
+
codec: DraftCodec<TValue>,
|
|
145
|
+
): LoadResult<TValue> {
|
|
146
|
+
if (stored === undefined) {
|
|
147
|
+
return { recovery: 'none' };
|
|
148
|
+
}
|
|
149
|
+
if (!isEnvelope(stored)) {
|
|
150
|
+
return { recovery: 'corrupt' };
|
|
151
|
+
}
|
|
152
|
+
if (stored.version > codec.version) {
|
|
153
|
+
return { recovery: 'unsupported-version' };
|
|
154
|
+
}
|
|
155
|
+
let result: DraftDecodeResult<TValue>;
|
|
156
|
+
try {
|
|
157
|
+
result = codec.decode(stored.value, stored.version);
|
|
158
|
+
} catch {
|
|
159
|
+
return { recovery: 'corrupt' };
|
|
160
|
+
}
|
|
161
|
+
if (result.kind !== 'decoded') {
|
|
162
|
+
return { recovery: result.kind };
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
recovery: 'restored',
|
|
166
|
+
value: result.value,
|
|
167
|
+
upgraded: stored.version < codec.version,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isBlocked(recovery: DraftRecovery): boolean {
|
|
172
|
+
return recovery === 'corrupt' || recovery === 'unsupported-version';
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function persistenceError(
|
|
176
|
+
operation: DraftPersistenceOperation,
|
|
177
|
+
cause: unknown,
|
|
178
|
+
): DraftPersistenceError {
|
|
179
|
+
return new DraftPersistenceError(operation, { cause: normalizeStorageError(cause) });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function asError(error: unknown): Error {
|
|
183
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const CLOSED: ClosedDurableDraftSnapshot = { open: false };
|
|
187
|
+
|
|
188
|
+
class DefaultDurableDraft<TScope, TValue> implements DurableDraft<TScope, TValue> {
|
|
189
|
+
readonly #store: KeyValueStore;
|
|
190
|
+
readonly #key: (scope: TScope) => string;
|
|
191
|
+
readonly #codec: DraftCodec<TValue>;
|
|
192
|
+
readonly #isScopeActive: (scope: TScope) => boolean;
|
|
193
|
+
readonly #listeners = new Set<() => void>();
|
|
194
|
+
#session: Session<TScope, TValue> | null = null;
|
|
195
|
+
#persistedRevision: number | null = 0;
|
|
196
|
+
/** True while the current key holds a draft this session read or wrote. */
|
|
197
|
+
#stored = false;
|
|
198
|
+
#tail: Promise<unknown> = Promise.resolve();
|
|
199
|
+
#snapshot: DurableDraftSnapshot<TScope, TValue> = CLOSED;
|
|
200
|
+
|
|
201
|
+
constructor(options: DurableDraftOptions<TScope, TValue>) {
|
|
202
|
+
const { version } = options.codec;
|
|
203
|
+
if (!Number.isSafeInteger(version) || version < 1) {
|
|
204
|
+
throw new RangeError('Draft codec version must be a positive integer.');
|
|
205
|
+
}
|
|
206
|
+
this.#store = options.store;
|
|
207
|
+
this.#key = options.key;
|
|
208
|
+
this.#codec = options.codec;
|
|
209
|
+
this.#isScopeActive = options.isScopeActive ?? (() => true);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
getSnapshot(): DurableDraftSnapshot<TScope, TValue> {
|
|
213
|
+
return this.#snapshot;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
subscribe(listener: () => void): () => void {
|
|
217
|
+
this.#listeners.add(listener);
|
|
218
|
+
return () => {
|
|
219
|
+
this.#listeners.delete(listener);
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
open(scope: TScope, initial: TValue): Promise<void> {
|
|
224
|
+
let key: string;
|
|
225
|
+
try {
|
|
226
|
+
key = this.#key(scope);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
return Promise.reject(asError(error));
|
|
229
|
+
}
|
|
230
|
+
const session: Session<TScope, TValue> = { scope, key, initial };
|
|
231
|
+
this.#session = session;
|
|
232
|
+
this.#persistedRevision = 0;
|
|
233
|
+
this.#stored = false;
|
|
234
|
+
this.#publish({
|
|
235
|
+
open: true,
|
|
236
|
+
scope,
|
|
237
|
+
value: initial,
|
|
238
|
+
localRevision: 0,
|
|
239
|
+
dirty: false,
|
|
240
|
+
recovery: 'none',
|
|
241
|
+
persistence: 'loading',
|
|
242
|
+
error: null,
|
|
243
|
+
submission: 'none',
|
|
244
|
+
});
|
|
245
|
+
return this.#enqueue(() => this.#load(session));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
update(value: TValue): void {
|
|
249
|
+
const current = this.#current();
|
|
250
|
+
if (current === null) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
this.#publishOpen({
|
|
254
|
+
value,
|
|
255
|
+
localRevision: current.localRevision + 1,
|
|
256
|
+
submission: 'none',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
save(): Promise<DraftSaveOutcome> {
|
|
261
|
+
const session = this.#session;
|
|
262
|
+
if (session === null) {
|
|
263
|
+
return Promise.resolve('stale');
|
|
264
|
+
}
|
|
265
|
+
return this.#enqueue(() => this.#write(session));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
clear(request: DraftClearRequest): Promise<DraftClearOutcome> {
|
|
269
|
+
const session = this.#session;
|
|
270
|
+
if (session === null) {
|
|
271
|
+
return Promise.resolve('stale');
|
|
272
|
+
}
|
|
273
|
+
return this.#enqueue(() => this.#delete(session, request));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
move(scope: TScope): Promise<DraftMoveOutcome> {
|
|
277
|
+
const session = this.#session;
|
|
278
|
+
if (session === null) {
|
|
279
|
+
return Promise.resolve('stale');
|
|
280
|
+
}
|
|
281
|
+
let key: string;
|
|
282
|
+
try {
|
|
283
|
+
key = this.#key(scope);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
return Promise.reject(asError(error));
|
|
286
|
+
}
|
|
287
|
+
return this.#enqueue(() => this.#relocate(session, scope, key));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async close(): Promise<void> {
|
|
291
|
+
this.#session = null;
|
|
292
|
+
this.#publish(CLOSED);
|
|
293
|
+
await this.#tail;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async #load(session: Session<TScope, TValue>): Promise<void> {
|
|
297
|
+
if (session !== this.#session) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
let stored: JsonValue | undefined;
|
|
301
|
+
try {
|
|
302
|
+
stored = await this.#store.get(session.key);
|
|
303
|
+
} catch (cause) {
|
|
304
|
+
this.#publishFor(session, {
|
|
305
|
+
recovery: 'unavailable',
|
|
306
|
+
persistence: 'failed',
|
|
307
|
+
error: persistenceError('read', cause),
|
|
308
|
+
});
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const current = this.#currentFor(session);
|
|
312
|
+
if (current === null) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
this.#stored = stored !== undefined;
|
|
316
|
+
const loaded = decodeStored(stored, this.#codec);
|
|
317
|
+
if (loaded.recovery === 'restored' && current.localRevision === 0) {
|
|
318
|
+
this.#persistedRevision = loaded.upgraded ? null : 0;
|
|
319
|
+
this.#publishOpen({ value: loaded.value, recovery: 'restored', persistence: 'idle' });
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
this.#publishOpen({
|
|
323
|
+
recovery: loaded.recovery === 'restored' ? 'none' : loaded.recovery,
|
|
324
|
+
persistence: 'idle',
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async #write(session: Session<TScope, TValue>): Promise<DraftSaveOutcome> {
|
|
329
|
+
const current = this.#activeFor(session);
|
|
330
|
+
if (current === null) {
|
|
331
|
+
return 'stale';
|
|
332
|
+
}
|
|
333
|
+
if (!current.dirty) {
|
|
334
|
+
return 'clean';
|
|
335
|
+
}
|
|
336
|
+
if (isBlocked(current.recovery)) {
|
|
337
|
+
return 'blocked';
|
|
338
|
+
}
|
|
339
|
+
const { localRevision } = current;
|
|
340
|
+
await this.#writeValue(session, session.key, current.value);
|
|
341
|
+
if (this.#currentFor(session) !== null) {
|
|
342
|
+
this.#persistedRevision = localRevision;
|
|
343
|
+
this.#stored = true;
|
|
344
|
+
this.#publishOpen({ persistence: 'idle', error: null });
|
|
345
|
+
}
|
|
346
|
+
return 'saved';
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async #delete(
|
|
350
|
+
session: Session<TScope, TValue>,
|
|
351
|
+
request: DraftClearRequest,
|
|
352
|
+
): Promise<DraftClearOutcome> {
|
|
353
|
+
const current = this.#activeFor(session);
|
|
354
|
+
if (current === null) {
|
|
355
|
+
return 'stale';
|
|
356
|
+
}
|
|
357
|
+
const submitted = request.reason === 'submitted';
|
|
358
|
+
if (submitted && request.localRevision !== current.localRevision) {
|
|
359
|
+
this.#publishOpen({ submission: 'confirmed' });
|
|
360
|
+
return 'newer-edits-kept';
|
|
361
|
+
}
|
|
362
|
+
const { localRevision } = current;
|
|
363
|
+
const submission: DraftSubmission = submitted ? 'confirmed' : current.submission;
|
|
364
|
+
await this.#deleteKey(session, session.key, { submission });
|
|
365
|
+
if (this.#currentFor(session) !== null) {
|
|
366
|
+
this.#applyCleared(session, localRevision, submitted);
|
|
367
|
+
}
|
|
368
|
+
return 'cleared';
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async #relocate(
|
|
372
|
+
session: Session<TScope, TValue>,
|
|
373
|
+
scope: TScope,
|
|
374
|
+
key: string,
|
|
375
|
+
): Promise<DraftMoveOutcome> {
|
|
376
|
+
const current = this.#activeFor(session);
|
|
377
|
+
if (current === null || !this.#isScopeActive(scope)) {
|
|
378
|
+
return 'stale';
|
|
379
|
+
}
|
|
380
|
+
if (isBlocked(current.recovery)) {
|
|
381
|
+
return 'blocked';
|
|
382
|
+
}
|
|
383
|
+
if (key === session.key) {
|
|
384
|
+
session.scope = scope;
|
|
385
|
+
this.#publishOpen({ scope });
|
|
386
|
+
return 'moved';
|
|
387
|
+
}
|
|
388
|
+
const carried = current.dirty || this.#stored;
|
|
389
|
+
const { localRevision } = current;
|
|
390
|
+
if (carried) {
|
|
391
|
+
await this.#writeValue(session, key, current.value);
|
|
392
|
+
}
|
|
393
|
+
await this.#deleteKey(session, session.key, {});
|
|
394
|
+
session.scope = scope;
|
|
395
|
+
session.key = key;
|
|
396
|
+
if (this.#currentFor(session) !== null) {
|
|
397
|
+
if (carried) {
|
|
398
|
+
this.#persistedRevision = localRevision;
|
|
399
|
+
}
|
|
400
|
+
this.#stored = carried;
|
|
401
|
+
this.#publishOpen({ scope, persistence: 'idle', error: null });
|
|
402
|
+
}
|
|
403
|
+
return 'moved';
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async #writeValue(session: Session<TScope, TValue>, key: string, value: TValue): Promise<void> {
|
|
407
|
+
this.#publishOpen({ persistence: 'saving', error: null });
|
|
408
|
+
try {
|
|
409
|
+
const envelope = { version: this.#codec.version, value: this.#codec.encode(value) };
|
|
410
|
+
await this.#store.set(key, envelope);
|
|
411
|
+
} catch (cause) {
|
|
412
|
+
const error = persistenceError('write', cause);
|
|
413
|
+
this.#publishFor(session, { persistence: 'failed', error });
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
async #deleteKey(
|
|
419
|
+
session: Session<TScope, TValue>,
|
|
420
|
+
key: string,
|
|
421
|
+
changes: Partial<OpenDurableDraftSnapshot<TScope, TValue>>,
|
|
422
|
+
): Promise<void> {
|
|
423
|
+
this.#publishFor(session, { ...changes, persistence: 'clearing', error: null });
|
|
424
|
+
try {
|
|
425
|
+
await this.#store.delete(key);
|
|
426
|
+
} catch (cause) {
|
|
427
|
+
const error = persistenceError('delete', cause);
|
|
428
|
+
this.#publishFor(session, { persistence: 'failed', error });
|
|
429
|
+
throw error;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
#applyCleared(session: Session<TScope, TValue>, localRevision: number, submitted: boolean): void {
|
|
434
|
+
this.#stored = false;
|
|
435
|
+
const base = { recovery: 'none', persistence: 'idle', error: null } as const;
|
|
436
|
+
if (submitted) {
|
|
437
|
+
this.#persistedRevision = localRevision;
|
|
438
|
+
this.#publishOpen(base);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const current = this.#current();
|
|
442
|
+
const nextRevision = (current?.localRevision ?? localRevision) + 1;
|
|
443
|
+
this.#persistedRevision = nextRevision;
|
|
444
|
+
this.#publishOpen({
|
|
445
|
+
...base,
|
|
446
|
+
value: session.initial,
|
|
447
|
+
localRevision: nextRevision,
|
|
448
|
+
submission: 'none',
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
#enqueue<TResult>(operation: () => Promise<TResult>): Promise<TResult> {
|
|
453
|
+
const run = this.#tail.then(operation);
|
|
454
|
+
this.#tail = run.catch(() => undefined);
|
|
455
|
+
return run;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
#current(): OpenDurableDraftSnapshot<TScope, TValue> | null {
|
|
459
|
+
return this.#snapshot.open ? this.#snapshot : null;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#currentFor(session: Session<TScope, TValue>): OpenDurableDraftSnapshot<TScope, TValue> | null {
|
|
463
|
+
return session === this.#session ? this.#current() : null;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
#activeFor(session: Session<TScope, TValue>): OpenDurableDraftSnapshot<TScope, TValue> | null {
|
|
467
|
+
const current = this.#currentFor(session);
|
|
468
|
+
return current !== null && this.#isScopeActive(session.scope) ? current : null;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
#publishFor(
|
|
472
|
+
session: Session<TScope, TValue>,
|
|
473
|
+
changes: Partial<OpenDurableDraftSnapshot<TScope, TValue>>,
|
|
474
|
+
): void {
|
|
475
|
+
if (this.#currentFor(session) !== null) {
|
|
476
|
+
this.#publishOpen(changes);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
#publishOpen(changes: Partial<OpenDurableDraftSnapshot<TScope, TValue>>): void {
|
|
481
|
+
const current = this.#current();
|
|
482
|
+
if (current === null) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const next = { ...current, ...changes };
|
|
486
|
+
this.#publish({ ...next, dirty: next.localRevision !== this.#persistedRevision });
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
#publish(snapshot: DurableDraftSnapshot<TScope, TValue>): void {
|
|
490
|
+
this.#snapshot = snapshot;
|
|
491
|
+
for (const listener of this.#listeners) {
|
|
492
|
+
listener();
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** Creates a framework-free durable draft over a `KeyValueStore`. */
|
|
498
|
+
export function createDurableDraft<TScope, TValue>(
|
|
499
|
+
options: DurableDraftOptions<TScope, TValue>,
|
|
500
|
+
): DurableDraft<TScope, TValue> {
|
|
501
|
+
return new DefaultDurableDraft(options);
|
|
502
|
+
}
|