@vibes.diy/vibe-runtime 14.3.332 → 14.3.334

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.
@@ -44,6 +44,9 @@ export declare class IndexedDbStore implements FireflyQueryDatabase {
44
44
  private readonly docs;
45
45
  private readonly listeners;
46
46
  private db?;
47
+ private reopening?;
48
+ private closed;
49
+ private writeChain;
47
50
  private readonly readyPromise;
48
51
  private readObserver?;
49
52
  setReadObserver(observer: (slot: string, docs: readonly DocWithId[]) => void): void;
@@ -69,6 +72,10 @@ export declare class IndexedDbStore implements FireflyQueryDatabase {
69
72
  ready(): Promise<void>;
70
73
  close(): Promise<void>;
71
74
  destroy(): Promise<void>;
75
+ private serialized;
76
+ private writeTx;
77
+ private runTx;
78
+ private reconnect;
72
79
  applyRemote(entries: RemoteSyncEntry[]): Promise<void>;
73
80
  applySnapshot(entries: SnapshotEntry[], cursors: Record<string, number> | Map<string, number>): Promise<void>;
74
81
  getCursors(): Promise<Map<string, number>>;
@@ -1,4 +1,4 @@
1
- import { exception2Result } from "@adviser/cement";
1
+ import { Result, exception2Result } from "@adviser/cement";
2
2
  import { type } from "arktype";
3
3
  import { materializeQuery, newOptimisticId, } from "./firefly-database.js";
4
4
  import { safeLocalStorage } from "./safe-local-storage.js";
@@ -25,13 +25,105 @@ function requestResult(request) {
25
25
  request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
26
26
  });
27
27
  }
28
+ class TransactionAbortedError extends Error {
29
+ constructor() {
30
+ super("IndexedDB transaction aborted");
31
+ this.name = "TransactionAbortedError";
32
+ }
33
+ }
28
34
  function transactionDone(tx) {
29
35
  return new Promise((resolve, reject) => {
30
36
  tx.oncomplete = () => resolve();
31
- tx.onerror = () => reject(tx.error ?? new Error("IndexedDB transaction failed"));
32
- tx.onabort = () => reject(tx.error ?? new Error("IndexedDB transaction aborted"));
37
+ tx.onerror = (event) => {
38
+ if (event.target?.error?.name === "AbortError")
39
+ return;
40
+ reject(tx.error ?? new Error("IndexedDB transaction failed"));
41
+ };
42
+ tx.onabort = () => reject(tx.error ?? new TransactionAbortedError());
33
43
  });
34
44
  }
45
+ function isConnectionLoss(err) {
46
+ const name = errorName(err);
47
+ return name === "TransactionAbortedError" || name === "InvalidStateError" || name === "AbortError";
48
+ }
49
+ function errorName(err) {
50
+ return err?.name;
51
+ }
52
+ function storeClosedError(name) {
53
+ return new Error(`IndexedDB store ${name} is closed`);
54
+ }
55
+ class SyncStage {
56
+ changed = [];
57
+ stagedDocs = new Map();
58
+ stagedMetas = new Map();
59
+ liveDocs;
60
+ liveMetas;
61
+ constructor(live) {
62
+ this.liveDocs = live.docs;
63
+ this.liveMetas = live.syncMeta;
64
+ }
65
+ hasDoc(docId) {
66
+ const staged = this.stagedDocs.get(docId);
67
+ if (staged === undefined)
68
+ return this.liveDocs.has(docId);
69
+ return staged !== null;
70
+ }
71
+ meta(docId) {
72
+ const staged = this.stagedMetas.get(docId);
73
+ if (staged === undefined)
74
+ return this.liveMetas.get(docId);
75
+ return staged === null ? undefined : staged;
76
+ }
77
+ putDoc(doc) {
78
+ this.stagedDocs.set(doc._id, doc);
79
+ }
80
+ deleteDoc(docId) {
81
+ this.stagedDocs.set(docId, null);
82
+ }
83
+ putMeta(meta) {
84
+ this.stagedMetas.set(meta.docId, meta);
85
+ }
86
+ deleteMeta(docId) {
87
+ this.stagedMetas.set(docId, null);
88
+ }
89
+ writeTo(stores) {
90
+ for (const [docId, doc] of this.stagedDocs) {
91
+ if (doc === null)
92
+ stores.docs.delete(docId);
93
+ else
94
+ stores.docs.put(doc);
95
+ }
96
+ for (const [docId, meta] of this.stagedMetas) {
97
+ if (meta === null)
98
+ stores.syncMeta.delete(docId);
99
+ else
100
+ stores.syncMeta.put(meta);
101
+ }
102
+ }
103
+ commitTo(live) {
104
+ for (const [docId, doc] of this.stagedDocs) {
105
+ if (doc === null)
106
+ live.docs.delete(docId);
107
+ else
108
+ live.docs.set(docId, doc);
109
+ }
110
+ for (const [docId, meta] of this.stagedMetas) {
111
+ if (meta === null)
112
+ live.syncMeta.delete(docId);
113
+ else
114
+ live.syncMeta.set(docId, meta);
115
+ }
116
+ }
117
+ }
118
+ async function deleteQueueRows(queueStore, docId) {
119
+ const keysReq = queueStore.getAllKeys();
120
+ const rowsReq = queueStore.getAll();
121
+ const keys = await requestResult(keysReq);
122
+ const rows = await requestResult(rowsReq);
123
+ for (let i = 0; i < rows.length; i++)
124
+ if (rows[i].docId === docId)
125
+ queueStore.delete(keys[i]);
126
+ }
35
127
  function physicalName(app, name) {
36
128
  return `${DB_PREFIX}:${encodeURIComponent(app)}:${encodeURIComponent(name)}`;
37
129
  }
@@ -106,6 +198,9 @@ export class IndexedDbStore {
106
198
  docs = new Map();
107
199
  listeners = new Set();
108
200
  db;
201
+ reopening;
202
+ closed = false;
203
+ writeChain = Promise.resolve();
109
204
  readyPromise;
110
205
  readObserver;
111
206
  setReadObserver(observer) {
@@ -245,10 +340,13 @@ export class IndexedDbStore {
245
340
  }
246
341
  async close() {
247
342
  await this.ready();
343
+ this.closed = true;
248
344
  this.db?.close();
249
345
  }
250
346
  async destroy() {
251
347
  await this.ready();
348
+ this.closed = true;
349
+ await this.writeChain;
252
350
  this.db?.close();
253
351
  this.db = undefined;
254
352
  this.docs.clear();
@@ -260,14 +358,70 @@ export class IndexedDbStore {
260
358
  await requestResult(this.factory.deleteDatabase(physicalName(this.app, this.name)));
261
359
  this.notify([]);
262
360
  }
361
+ serialized(fn) {
362
+ const run = this.writeChain.then(() => this.closed ? Promise.resolve(Result.Err(storeClosedError(this.name))) : fn());
363
+ this.writeChain = run.then(() => undefined, () => undefined);
364
+ return run.then((r) => (r.isOk() ? r.Ok() : Promise.reject(r.Err())));
365
+ }
366
+ async writeTx(db, storeNames, fill) {
367
+ const first = await this.runTx(db, storeNames, fill);
368
+ if (first.isOk() || this.closed || !isConnectionLoss(first.Err()))
369
+ return first;
370
+ vibeLog("warn", "store-tx-retry", { db: this.name, error: String(first.Err()) });
371
+ const fresh = await this.reconnect(db);
372
+ if (fresh.isErr())
373
+ return Result.Err(fresh.Err());
374
+ return this.runTx(fresh.Ok(), storeNames, fill);
375
+ }
376
+ async runTx(db, storeNames, fill) {
377
+ const opened = exception2Result(() => db.transaction(storeNames, "readwrite"));
378
+ if (opened.isErr())
379
+ return Result.Err(opened.Err());
380
+ const tx = opened.Ok();
381
+ const done = exception2Result(() => transactionDone(tx));
382
+ const filled = await exception2Result(() => fill(tx));
383
+ if (filled.isErr()) {
384
+ const err = filled.Err();
385
+ if (errorName(err) === "AbortError") {
386
+ const outcome = await done;
387
+ if (outcome.isErr())
388
+ return Result.Err(outcome.Err());
389
+ return Result.Err(new Error("IndexedDB write body aborted", { cause: err }));
390
+ }
391
+ exception2Result(() => tx.abort());
392
+ await done;
393
+ return Result.Err(err);
394
+ }
395
+ return done;
396
+ }
397
+ reconnect(lost) {
398
+ if (this.db !== undefined && this.db !== lost)
399
+ return Promise.resolve(Result.Ok(this.db));
400
+ if (this.reopening === undefined) {
401
+ exception2Result(() => lost.close());
402
+ this.reopening = exception2Result(() => openDatabase(this.factory, this.app, this.name)).then((opened) => {
403
+ this.reopening = undefined;
404
+ if (opened.isErr())
405
+ return Result.Err(opened.Err());
406
+ const { db } = opened.Ok();
407
+ if (this.closed) {
408
+ db.close();
409
+ return Result.Err(new Error(`IndexedDB store ${this.name} closed during reopen`));
410
+ }
411
+ this.db = db;
412
+ return Result.Ok(db);
413
+ });
414
+ }
415
+ return this.reopening;
416
+ }
263
417
  async applyRemote(entries) {
264
418
  await this.ready();
265
- const queuedDocIds = new Set(this.pushQueue.map((e) => e.docId));
266
- const changed = [];
267
- const apply = (docsStore, syncStore) => {
419
+ return this.serialized(async () => {
420
+ const queuedDocIds = new Set(this.pushQueue.map((e) => e.docId));
421
+ const stage = new SyncStage({ docs: this.docs, syncMeta: this.syncMeta });
268
422
  for (const entry of entries) {
269
423
  if (entry.op === "put") {
270
- const existing = this.syncMeta.get(entry.docId);
424
+ const existing = stage.meta(entry.docId);
271
425
  if (existing !== undefined && existing.seq > entry.seq)
272
426
  continue;
273
427
  if (existing !== undefined && existing.seq === entry.seq) {
@@ -277,117 +431,99 @@ export class IndexedDbStore {
277
431
  merged.push(ch);
278
432
  if (merged.length === existing.channels.length)
279
433
  continue;
280
- const widened = { ...existing, channels: merged };
281
- this.syncMeta.set(entry.docId, widened);
282
- syncStore?.put(widened);
434
+ stage.putMeta({ ...existing, channels: merged });
283
435
  continue;
284
436
  }
285
437
  const meta = { docId: entry.docId, seq: entry.seq, channels: [...entry.channels] };
286
438
  if (queuedDocIds.has(entry.docId)) {
287
- this.syncMeta.set(entry.docId, meta);
288
- syncStore?.put(meta);
439
+ stage.putMeta(meta);
289
440
  continue;
290
441
  }
291
442
  const saved = { ...entry.doc, _id: entry.docId };
292
- this.docs.set(entry.docId, saved);
293
- this.syncMeta.set(entry.docId, meta);
294
- docsStore?.put(saved);
295
- syncStore?.put(meta);
296
- changed.push(saved);
443
+ stage.putDoc(saved);
444
+ stage.putMeta(meta);
445
+ stage.changed.push(saved);
297
446
  }
298
447
  else if (entry.op === "delete") {
299
448
  if (queuedDocIds.has(entry.docId)) {
300
- this.syncMeta.delete(entry.docId);
301
- syncStore?.delete(entry.docId);
449
+ stage.deleteMeta(entry.docId);
302
450
  continue;
303
451
  }
304
- if (this.docs.has(entry.docId))
305
- changed.push({ _id: entry.docId, _deleted: true });
306
- this.docs.delete(entry.docId);
307
- this.syncMeta.delete(entry.docId);
308
- docsStore?.delete(entry.docId);
309
- syncStore?.delete(entry.docId);
452
+ if (stage.hasDoc(entry.docId))
453
+ stage.changed.push({ _id: entry.docId, _deleted: true });
454
+ stage.deleteDoc(entry.docId);
455
+ stage.deleteMeta(entry.docId);
310
456
  }
311
457
  else {
312
- const meta = this.syncMeta.get(entry.docId);
458
+ const meta = stage.meta(entry.docId);
313
459
  if (meta === undefined)
314
460
  continue;
315
461
  const channels = meta.channels.filter((c) => c !== entry.channel);
316
462
  if (channels.length === 0 && !queuedDocIds.has(entry.docId)) {
317
- this.docs.delete(entry.docId);
318
- this.syncMeta.delete(entry.docId);
319
- docsStore?.delete(entry.docId);
320
- syncStore?.delete(entry.docId);
321
- changed.push({ _id: entry.docId, _deleted: true });
463
+ stage.deleteDoc(entry.docId);
464
+ stage.deleteMeta(entry.docId);
465
+ stage.changed.push({ _id: entry.docId, _deleted: true });
322
466
  }
323
467
  else {
324
- const updated = { ...meta, channels };
325
- this.syncMeta.set(entry.docId, updated);
326
- syncStore?.put(updated);
468
+ stage.putMeta({ ...meta, channels });
327
469
  }
328
470
  }
329
471
  }
330
- };
331
- if (this.db !== undefined) {
332
- const tx = this.db.transaction([DOCS, SYNC_META], "readwrite");
333
- apply(tx.objectStore(DOCS), tx.objectStore(SYNC_META));
334
- await transactionDone(tx);
335
- }
336
- else {
337
- apply();
338
- }
339
- this.receipts.clearRefused(changed.map((d) => d._id));
340
- if (changed.length > 0)
341
- this.notify(changed);
472
+ if (this.db !== undefined) {
473
+ const written = await this.writeTx(this.db, [DOCS, SYNC_META], (tx) => stage.writeTo({ docs: tx.objectStore(DOCS), syncMeta: tx.objectStore(SYNC_META) }));
474
+ if (written.isErr())
475
+ return written;
476
+ }
477
+ stage.commitTo({ docs: this.docs, syncMeta: this.syncMeta });
478
+ this.receipts.clearRefused(stage.changed.map((d) => d._id));
479
+ if (stage.changed.length > 0)
480
+ this.notify(stage.changed);
481
+ return Result.Ok(undefined);
482
+ });
342
483
  }
343
484
  async applySnapshot(entries, cursors) {
344
485
  await this.ready();
345
- const queuedDocIds = new Set(this.pushQueue.map((e) => e.docId));
346
- const snapshotIds = new Set(entries.map((e) => e.docId));
347
- const nextCursors = cursors instanceof Map ? new Map(cursors) : new Map(Object.entries(cursors));
348
- const changed = [];
349
- const run = (docsStore, syncStore, cursorStore) => {
350
- for (const docId of [...this.docs.keys()]) {
486
+ return this.serialized(async () => {
487
+ const queuedDocIds = new Set(this.pushQueue.map((e) => e.docId));
488
+ const snapshotIds = new Set(entries.map((e) => e.docId));
489
+ const nextCursors = cursors instanceof Map ? new Map(cursors) : new Map(Object.entries(cursors));
490
+ const stage = new SyncStage({ docs: this.docs, syncMeta: this.syncMeta });
491
+ for (const docId of this.docs.keys()) {
351
492
  if (snapshotIds.has(docId) || queuedDocIds.has(docId) || !this.syncMeta.has(docId))
352
493
  continue;
353
- this.docs.delete(docId);
354
- this.syncMeta.delete(docId);
355
- docsStore?.delete(docId);
356
- syncStore?.delete(docId);
357
- changed.push({ _id: docId, _deleted: true });
494
+ stage.deleteDoc(docId);
495
+ stage.deleteMeta(docId);
496
+ stage.changed.push({ _id: docId, _deleted: true });
358
497
  }
359
498
  for (const entry of entries) {
360
499
  const meta = { docId: entry.docId, seq: entry.seq, channels: [...entry.channels] };
361
500
  if (queuedDocIds.has(entry.docId)) {
362
- this.syncMeta.set(entry.docId, meta);
363
- syncStore?.put(meta);
501
+ stage.putMeta(meta);
364
502
  continue;
365
503
  }
366
504
  const saved = { ...entry.doc, _id: entry.docId };
367
- this.docs.set(entry.docId, saved);
368
- this.syncMeta.set(entry.docId, meta);
369
- docsStore?.put(saved);
370
- syncStore?.put(meta);
371
- changed.push(saved);
505
+ stage.putDoc(saved);
506
+ stage.putMeta(meta);
507
+ stage.changed.push(saved);
372
508
  }
373
- this.cursors = nextCursors;
374
- if (cursorStore !== undefined) {
375
- cursorStore.clear();
376
- for (const [channel, seq] of this.cursors)
377
- cursorStore.put(seq, channel);
509
+ if (this.db !== undefined) {
510
+ const written = await this.writeTx(this.db, [DOCS, SYNC_META, CURSORS], (tx) => {
511
+ stage.writeTo({ docs: tx.objectStore(DOCS), syncMeta: tx.objectStore(SYNC_META) });
512
+ const cursorStore = tx.objectStore(CURSORS);
513
+ cursorStore.clear();
514
+ for (const [channel, seq] of nextCursors)
515
+ cursorStore.put(seq, channel);
516
+ });
517
+ if (written.isErr())
518
+ return written;
378
519
  }
379
- };
380
- if (this.db !== undefined) {
381
- const tx = this.db.transaction([DOCS, SYNC_META, CURSORS], "readwrite");
382
- run(tx.objectStore(DOCS), tx.objectStore(SYNC_META), tx.objectStore(CURSORS));
383
- await transactionDone(tx);
384
- }
385
- else {
386
- run();
387
- }
388
- this.receipts.clearRefused(changed.map((d) => d._id));
389
- if (changed.length > 0)
390
- this.notify(changed);
520
+ stage.commitTo({ docs: this.docs, syncMeta: this.syncMeta });
521
+ this.cursors = nextCursors;
522
+ this.receipts.clearRefused(stage.changed.map((d) => d._id));
523
+ if (stage.changed.length > 0)
524
+ this.notify(stage.changed);
525
+ return Result.Ok(undefined);
526
+ });
391
527
  }
392
528
  async getCursors() {
393
529
  await this.ready();
@@ -395,58 +531,64 @@ export class IndexedDbStore {
395
531
  }
396
532
  async setCursors(cursors) {
397
533
  await this.ready();
398
- this.cursors = cursors instanceof Map ? new Map(cursors) : new Map(Object.entries(cursors));
399
- if (this.db !== undefined) {
400
- const tx = this.db.transaction(CURSORS, "readwrite");
401
- const store = tx.objectStore(CURSORS);
402
- store.clear();
403
- for (const [channel, seq] of this.cursors)
404
- store.put(seq, channel);
405
- await transactionDone(tx);
406
- }
534
+ return this.serialized(async () => {
535
+ const next = cursors instanceof Map ? new Map(cursors) : new Map(Object.entries(cursors));
536
+ if (this.db !== undefined) {
537
+ const written = await this.writeTx(this.db, [CURSORS], (tx) => {
538
+ const store = tx.objectStore(CURSORS);
539
+ store.clear();
540
+ for (const [channel, seq] of next)
541
+ store.put(seq, channel);
542
+ });
543
+ if (written.isErr())
544
+ return written;
545
+ }
546
+ this.cursors = next;
547
+ return Result.Ok(undefined);
548
+ });
407
549
  }
408
550
  async dropCursor(channel) {
409
551
  await this.ready();
410
- this.cursors.delete(channel);
411
- if (this.db !== undefined) {
412
- const tx = this.db.transaction(CURSORS, "readwrite");
413
- tx.objectStore(CURSORS).delete(channel);
414
- await transactionDone(tx);
415
- }
552
+ return this.serialized(async () => {
553
+ if (this.db !== undefined) {
554
+ const written = await this.writeTx(this.db, [CURSORS], (tx) => {
555
+ tx.objectStore(CURSORS).delete(channel);
556
+ });
557
+ if (written.isErr())
558
+ return written;
559
+ }
560
+ this.cursors.delete(channel);
561
+ return Result.Ok(undefined);
562
+ });
416
563
  }
417
564
  async evictChannel(channel) {
418
565
  await this.ready();
419
- const queuedDocIds = new Set(this.pushQueue.map((e) => e.docId));
420
- const changed = [];
421
- const run = (docsStore, syncStore) => {
422
- for (const [docId, meta] of [...this.syncMeta]) {
566
+ return this.serialized(async () => {
567
+ const queuedDocIds = new Set(this.pushQueue.map((e) => e.docId));
568
+ const stage = new SyncStage({ docs: this.docs, syncMeta: this.syncMeta });
569
+ for (const [docId, meta] of this.syncMeta) {
423
570
  if (!meta.channels.includes(channel))
424
571
  continue;
425
572
  const channels = meta.channels.filter((c) => c !== channel);
426
573
  if (channels.length === 0 && !queuedDocIds.has(docId)) {
427
- this.docs.delete(docId);
428
- this.syncMeta.delete(docId);
429
- docsStore?.delete(docId);
430
- syncStore?.delete(docId);
431
- changed.push({ _id: docId, _deleted: true });
574
+ stage.deleteDoc(docId);
575
+ stage.deleteMeta(docId);
576
+ stage.changed.push({ _id: docId, _deleted: true });
432
577
  }
433
578
  else {
434
- const updated = { ...meta, channels };
435
- this.syncMeta.set(docId, updated);
436
- syncStore?.put(updated);
579
+ stage.putMeta({ ...meta, channels });
437
580
  }
438
581
  }
439
- };
440
- if (this.db !== undefined) {
441
- const tx = this.db.transaction([DOCS, SYNC_META], "readwrite");
442
- run(tx.objectStore(DOCS), tx.objectStore(SYNC_META));
443
- await transactionDone(tx);
444
- }
445
- else {
446
- run();
447
- }
448
- if (changed.length > 0)
449
- this.notify(changed);
582
+ if (this.db !== undefined) {
583
+ const written = await this.writeTx(this.db, [DOCS, SYNC_META], (tx) => stage.writeTo({ docs: tx.objectStore(DOCS), syncMeta: tx.objectStore(SYNC_META) }));
584
+ if (written.isErr())
585
+ return written;
586
+ }
587
+ stage.commitTo({ docs: this.docs, syncMeta: this.syncMeta });
588
+ if (stage.changed.length > 0)
589
+ this.notify(stage.changed);
590
+ return Result.Ok(undefined);
591
+ });
450
592
  }
451
593
  async peekPushQueue() {
452
594
  await this.ready();
@@ -454,69 +596,75 @@ export class IndexedDbStore {
454
596
  }
455
597
  async dequeuePush(ids) {
456
598
  await this.ready();
457
- const drop = new Set(ids);
458
- this.pushQueue = this.pushQueue.filter((e) => !drop.has(e.id));
459
- if (this.db !== undefined) {
460
- const tx = this.db.transaction(PUSH_QUEUE, "readwrite");
461
- const store = tx.objectStore(PUSH_QUEUE);
462
- for (const id of ids)
463
- store.delete(id);
464
- await transactionDone(tx);
465
- }
599
+ return this.serialized(async () => {
600
+ if (this.db !== undefined) {
601
+ const written = await this.writeTx(this.db, [PUSH_QUEUE], (tx) => {
602
+ const store = tx.objectStore(PUSH_QUEUE);
603
+ for (const id of ids)
604
+ store.delete(id);
605
+ });
606
+ if (written.isErr())
607
+ return written;
608
+ }
609
+ const drop = new Set(ids);
610
+ this.pushQueue = this.pushQueue.filter((e) => !drop.has(e.id));
611
+ return Result.Ok(undefined);
612
+ });
466
613
  }
467
614
  async recordAccepted(docId, seq) {
468
615
  await this.ready();
469
- const existing = this.syncMeta.get(docId);
470
- const meta = existing !== undefined ? { ...existing, seq } : { docId, seq, channels: [] };
471
- this.syncMeta.set(docId, meta);
472
- if (this.db !== undefined) {
473
- const tx = this.db.transaction(SYNC_META, "readwrite");
474
- tx.objectStore(SYNC_META).put(meta);
475
- await transactionDone(tx);
476
- }
616
+ return this.serialized(async () => {
617
+ const existing = this.syncMeta.get(docId);
618
+ const meta = existing !== undefined ? { ...existing, seq } : { docId, seq, channels: [] };
619
+ if (this.db !== undefined) {
620
+ const written = await this.writeTx(this.db, [SYNC_META], (tx) => {
621
+ tx.objectStore(SYNC_META).put(meta);
622
+ });
623
+ if (written.isErr())
624
+ return written;
625
+ }
626
+ this.syncMeta.set(docId, meta);
627
+ return Result.Ok(undefined);
628
+ });
477
629
  }
478
630
  async revertDoc(docId) {
479
631
  await this.ready();
480
- const existed = this.docs.has(docId);
481
- this.docs.delete(docId);
482
- this.syncMeta.delete(docId);
483
- this.pushQueue = this.pushQueue.filter((e) => e.docId !== docId);
484
- if (this.db !== undefined) {
485
- const tx = this.db.transaction([DOCS, SYNC_META, PUSH_QUEUE], "readwrite");
486
- tx.objectStore(DOCS).delete(docId);
487
- tx.objectStore(SYNC_META).delete(docId);
488
- const queueStore = tx.objectStore(PUSH_QUEUE);
489
- const keysReq = queueStore.getAllKeys();
490
- const rowsReq = queueStore.getAll();
491
- const keys = await requestResult(keysReq);
492
- const rows = await requestResult(rowsReq);
493
- for (let i = 0; i < rows.length; i++)
494
- if (rows[i].docId === docId)
495
- queueStore.delete(keys[i]);
496
- await transactionDone(tx);
497
- }
498
- if (existed)
499
- this.notify([{ _id: docId, _deleted: true }]);
632
+ return this.serialized(async () => {
633
+ if (this.db !== undefined) {
634
+ const written = await this.writeTx(this.db, [DOCS, SYNC_META, PUSH_QUEUE], async (tx) => {
635
+ tx.objectStore(DOCS).delete(docId);
636
+ tx.objectStore(SYNC_META).delete(docId);
637
+ await deleteQueueRows(tx.objectStore(PUSH_QUEUE), docId);
638
+ });
639
+ if (written.isErr())
640
+ return written;
641
+ }
642
+ const existed = this.docs.has(docId);
643
+ this.docs.delete(docId);
644
+ this.syncMeta.delete(docId);
645
+ this.pushQueue = this.pushQueue.filter((e) => e.docId !== docId);
646
+ if (existed)
647
+ this.notify([{ _id: docId, _deleted: true }]);
648
+ return Result.Ok(undefined);
649
+ });
500
650
  }
501
651
  async overwriteFromServer(docId, doc) {
502
652
  await this.ready();
503
- const saved = { ...doc, _id: docId };
504
- this.docs.set(docId, saved);
505
- this.pushQueue = this.pushQueue.filter((e) => e.docId !== docId);
506
- if (this.db !== undefined) {
507
- const tx = this.db.transaction([DOCS, PUSH_QUEUE], "readwrite");
508
- tx.objectStore(DOCS).put(saved);
509
- const queueStore = tx.objectStore(PUSH_QUEUE);
510
- const keysReq = queueStore.getAllKeys();
511
- const rowsReq = queueStore.getAll();
512
- const keys = await requestResult(keysReq);
513
- const rows = await requestResult(rowsReq);
514
- for (let i = 0; i < rows.length; i++)
515
- if (rows[i].docId === docId)
516
- queueStore.delete(keys[i]);
517
- await transactionDone(tx);
518
- }
519
- this.notify([saved]);
653
+ return this.serialized(async () => {
654
+ const saved = { ...doc, _id: docId };
655
+ if (this.db !== undefined) {
656
+ const written = await this.writeTx(this.db, [DOCS, PUSH_QUEUE], async (tx) => {
657
+ tx.objectStore(DOCS).put(saved);
658
+ await deleteQueueRows(tx.objectStore(PUSH_QUEUE), docId);
659
+ });
660
+ if (written.isErr())
661
+ return written;
662
+ }
663
+ this.docs.set(docId, saved);
664
+ this.pushQueue = this.pushQueue.filter((e) => e.docId !== docId);
665
+ this.notify([saved]);
666
+ return Result.Ok(undefined);
667
+ });
520
668
  }
521
669
  async getSyncMeta(docId) {
522
670
  await this.ready();
@@ -574,23 +722,27 @@ export class IndexedDbStore {
574
722
  const id = doc._id ?? newOptimisticId();
575
723
  const saved = { ...(await uploadDocFiles(stripClientOnlyDocFields(doc))), _id: id };
576
724
  const opId = opts?.opId ?? newOpId();
577
- let queued;
578
- if (this.db !== undefined) {
579
- const tx = this.db.transaction([DOCS, PUSH_QUEUE], "readwrite");
580
- tx.objectStore(DOCS).put(saved);
581
- if (this.persistent)
582
- queued = this.writeQueueEntry(tx.objectStore(PUSH_QUEUE), id, false, opId);
583
- await transactionDone(tx);
584
- }
585
- else if (this.persistent) {
586
- queued = { id: ++this.pushSeq, docId: id, deleted: false, opId };
587
- }
588
- this.docs.set(id, saved);
589
- if (queued !== undefined)
590
- this.pushQueue.push(queued);
591
- const synced = this.openReceipt(opId, id, queued !== undefined);
592
- this.notify([saved], opts?.origin !== undefined ? { origin: opts.origin } : undefined);
593
- return { id, ok: true, synced };
725
+ return this.serialized(async () => {
726
+ let queued;
727
+ if (this.db !== undefined) {
728
+ const written = await this.writeTx(this.db, [DOCS, PUSH_QUEUE], (tx) => {
729
+ tx.objectStore(DOCS).put(saved);
730
+ if (this.persistent)
731
+ queued = this.writeQueueEntry(tx.objectStore(PUSH_QUEUE), id, false, opId);
732
+ });
733
+ if (written.isErr())
734
+ return Result.Err(written.Err());
735
+ }
736
+ else if (this.persistent) {
737
+ queued = { id: ++this.pushSeq, docId: id, deleted: false, opId };
738
+ }
739
+ this.docs.set(id, saved);
740
+ if (queued !== undefined)
741
+ this.pushQueue.push(queued);
742
+ const synced = this.openReceipt(opId, id, queued !== undefined);
743
+ this.notify([saved], opts?.origin !== undefined ? { origin: opts.origin } : undefined);
744
+ return Result.Ok({ id, ok: true, synced });
745
+ });
594
746
  }
595
747
  onSyncStatus(fn) {
596
748
  return this.receipts.onDocChange(fn);
@@ -609,26 +761,30 @@ export class IndexedDbStore {
609
761
  async del(id, opts) {
610
762
  noteLocalWrite();
611
763
  await this.ready();
612
- const opId = opts?.opId ?? newOpId();
613
- let queued;
614
- if (this.db !== undefined) {
615
- const tx = this.db.transaction([DOCS, SYNC_META, PUSH_QUEUE], "readwrite");
616
- tx.objectStore(DOCS).delete(id);
617
- tx.objectStore(SYNC_META).delete(id);
618
- if (this.persistent)
619
- queued = this.writeQueueEntry(tx.objectStore(PUSH_QUEUE), id, true, opId);
620
- await transactionDone(tx);
621
- }
622
- else if (this.persistent) {
623
- queued = { id: ++this.pushSeq, docId: id, deleted: true, opId };
624
- }
625
- this.docs.delete(id);
626
- this.syncMeta.delete(id);
627
- if (queued !== undefined)
628
- this.pushQueue.push(queued);
629
- const synced = this.openReceipt(opId, id, queued !== undefined);
630
- this.notify([{ _id: id, _deleted: true }], opts?.origin !== undefined ? { origin: opts.origin } : undefined);
631
- return { id, ok: true, synced };
764
+ return this.serialized(async () => {
765
+ const opId = opts?.opId ?? newOpId();
766
+ let queued;
767
+ if (this.db !== undefined) {
768
+ const written = await this.writeTx(this.db, [DOCS, SYNC_META, PUSH_QUEUE], (tx) => {
769
+ tx.objectStore(DOCS).delete(id);
770
+ tx.objectStore(SYNC_META).delete(id);
771
+ if (this.persistent)
772
+ queued = this.writeQueueEntry(tx.objectStore(PUSH_QUEUE), id, true, opId);
773
+ });
774
+ if (written.isErr())
775
+ return Result.Err(written.Err());
776
+ }
777
+ else if (this.persistent) {
778
+ queued = { id: ++this.pushSeq, docId: id, deleted: true, opId };
779
+ }
780
+ this.docs.delete(id);
781
+ this.syncMeta.delete(id);
782
+ if (queued !== undefined)
783
+ this.pushQueue.push(queued);
784
+ const synced = this.openReceipt(opId, id, queued !== undefined);
785
+ this.notify([{ _id: id, _deleted: true }], opts?.origin !== undefined ? { origin: opts.origin } : undefined);
786
+ return Result.Ok({ id, ok: true, synced });
787
+ });
632
788
  }
633
789
  writeQueueEntry(store, docId, deleted, opId = newOpId()) {
634
790
  const entry = { id: 0, docId, deleted, opId };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vibes.diy/vibe-runtime",
3
- "version": "14.3.332",
3
+ "version": "14.3.334",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -36,9 +36,9 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@adviser/cement": "~0.5.37",
39
- "@vibes.diy/api-types": "14.3.332",
40
- "@vibes.diy/base": "14.3.332",
41
- "@vibes.diy/vibe-types": "14.3.332",
39
+ "@vibes.diy/api-types": "14.3.334",
40
+ "@vibes.diy/base": "14.3.334",
41
+ "@vibes.diy/vibe-types": "14.3.334",
42
42
  "arktype": "~2.2.9",
43
43
  "charwise": "~3.0.1",
44
44
  "es-module-lexer": "~3.0.4",
package/sync-engine.js CHANGED
@@ -490,8 +490,13 @@ export class SyncEngine {
490
490
  writes.push({ docId, doc: stripClientOnlyDocFields(doc), baseSeq });
491
491
  }
492
492
  this.store.receipts.supersede(skipOpIds);
493
- if (skipIds.length > 0)
494
- await this.store.dequeuePush(skipIds);
493
+ if (skipIds.length > 0) {
494
+ const rSkip = await exception2Result(() => this.store.dequeuePush(skipIds));
495
+ if (rSkip.isErr()) {
496
+ this.schedulePushRetry();
497
+ return;
498
+ }
499
+ }
495
500
  if (this.stopped)
496
501
  return;
497
502
  if (writes.length === 0) {
@@ -527,6 +532,9 @@ export class SyncEngine {
527
532
  const applied = await this.applyVerdicts({ writes, idsByDoc, opIdsByDoc, accepted, rejected });
528
533
  if (applied !== undefined && applied.dequeueIds.length > 0)
529
534
  await this.store.dequeuePush(applied.dequeueIds);
535
+ if (applied !== undefined)
536
+ for (const docId of applied.acceptedDocIds)
537
+ this.anonymousDocIds.delete(docId);
530
538
  return applied;
531
539
  });
532
540
  release();
@@ -553,6 +561,7 @@ export class SyncEngine {
553
561
  async applyVerdicts(args) {
554
562
  const { writes, idsByDoc, opIdsByDoc, accepted, rejected } = args;
555
563
  const dequeueIds = [];
564
+ const acceptedDocIds = [];
556
565
  let refusedAny = false;
557
566
  let retryAny = false;
558
567
  for (const docId of writes.map((w) => w.docId)) {
@@ -565,7 +574,7 @@ export class SyncEngine {
565
574
  return undefined;
566
575
  this.store.receipts.accept({ docId, opIds, seq });
567
576
  dequeueIds.push(...ids);
568
- this.anonymousDocIds.delete(docId);
577
+ acceptedDocIds.push(docId);
569
578
  this.retryDocIds.delete(docId);
570
579
  continue;
571
580
  }
@@ -612,7 +621,7 @@ export class SyncEngine {
612
621
  this.retryDocIds.add(docId);
613
622
  retryAny = true;
614
623
  }
615
- return { dequeueIds, refusedAny, retryAny };
624
+ return { dequeueIds, acceptedDocIds, refusedAny, retryAny };
616
625
  }
617
626
  async convergeRefused(docId) {
618
627
  const res = await exception2Result(() => this.api.getDoc(docId, this.dbName));