@fireproof/core 0.14.8 → 0.15.0-dev

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.
@@ -11605,7 +11605,7 @@ replaceTraps((oldTraps) => ({
11605
11605
  }));
11606
11606
 
11607
11607
  // src/version.ts
11608
- var PACKAGE_VERSION = "0.14.8";
11608
+ var PACKAGE_VERSION = "0.15.0-dev";
11609
11609
 
11610
11610
  // src/store.ts
11611
11611
  var match = PACKAGE_VERSION.match(/^([^.]*\.[^.]*)/);
@@ -11634,11 +11634,11 @@ var MetaStore = class extends VersionedStore {
11634
11634
  }
11635
11635
  };
11636
11636
  var DataStore = class {
11637
- constructor(loader) {
11637
+ constructor(name8) {
11638
11638
  __publicField(this, "tag", "car-base");
11639
11639
  __publicField(this, "STORAGE_VERSION", STORAGE_VERSION);
11640
- __publicField(this, "loader");
11641
- this.loader = loader;
11640
+ __publicField(this, "name");
11641
+ this.name = name8;
11642
11642
  }
11643
11643
  };
11644
11644
 
@@ -11803,7 +11803,7 @@ var DataStore2 = class extends DataStore {
11803
11803
  }
11804
11804
  async _withDB(dbWorkFun) {
11805
11805
  if (!this.idb) {
11806
- const dbName = `fp.${this.STORAGE_VERSION}.${this.loader.keyId}.${this.loader.name}`;
11806
+ const dbName = `fp.${this.STORAGE_VERSION}.${this.name}`;
11807
11807
  this.idb = await openDB(dbName, 1, {
11808
11808
  upgrade(db) {
11809
11809
  db.createObjectStore("cars");
@@ -11938,7 +11938,7 @@ var Loader = class {
11938
11938
  this.name = name8;
11939
11939
  this.opts = opts || this.opts;
11940
11940
  this.metaStore = new MetaStore2(this.name);
11941
- this.carStore = new DataStore2(this);
11941
+ this.carStore = new DataStore2(this.name);
11942
11942
  this.remoteWAL = new RemoteWAL2(this);
11943
11943
  this.ready = Promise.resolve().then(async () => {
11944
11944
  if (!this.metaStore || !this.carStore || !this.remoteWAL)
@@ -21012,7 +21012,7 @@ var _DbLoader = class _DbLoader extends Loader {
21012
21012
  __publicField(this, "writing", Promise.resolve());
21013
21013
  __publicField(this, "remoteFileStore");
21014
21014
  __publicField(this, "fileStore");
21015
- this.fileStore = new DataStore2(this);
21015
+ this.fileStore = new DataStore2(this.name);
21016
21016
  this.clock = clock;
21017
21017
  }
21018
21018
  async _readyForMerge() {
@@ -21220,7 +21220,7 @@ function applyHeadQueue(worker) {
21220
21220
  const task = queue.shift();
21221
21221
  if (!task)
21222
21222
  continue;
21223
- await worker(task.id, task.newHead, task.prevHead, task.updates);
21223
+ await worker(task.newHead, task.prevHead);
21224
21224
  if (task.updates) {
21225
21225
  allUpdates.push(...task.updates);
21226
21226
  }
@@ -21250,7 +21250,7 @@ function applyHeadQueue(worker) {
21250
21250
  // src/crdt-clock.ts
21251
21251
  var CRDTClock = class {
21252
21252
  constructor() {
21253
- // todo: keep the clock of remote and local changes separate, merge on read
21253
+ // todo: track local and remote clocks independently, merge on read
21254
21254
  // that way we can drop the whole remote if we need to
21255
21255
  // should go with making sure the local clock only references locally available blocks on write
21256
21256
  __publicField(this, "head", []);
@@ -21265,68 +21265,22 @@ var CRDTClock = class {
21265
21265
  this.head = head;
21266
21266
  }
21267
21267
  async applyHead(newHead, prevHead, updates = null) {
21268
- const taskId = Math.random().toString().slice(2, 8);
21269
21268
  for await (const { updates: updatesAcc, all } of this.applyHeadQueue.push({
21270
- id: taskId,
21271
- // tblocks,
21272
21269
  newHead,
21273
21270
  prevHead,
21274
21271
  updates
21275
21272
  })) {
21276
- ;
21277
- ((updatesAcc2, all2) => {
21278
- void Promise.resolve().then(async () => {
21279
- let intUpdates = updatesAcc2;
21280
- if (this.watchers.size && !all2) {
21281
- const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
21282
- intUpdates = changes.result;
21283
- }
21284
- this.zoomers.forEach((fn) => fn());
21285
- this.notifyWatchers(intUpdates || []);
21286
- });
21287
- })([...updatesAcc], all);
21273
+ this.processUpdates(updatesAcc, all, prevHead);
21288
21274
  }
21289
21275
  }
21290
- async int_applyHead(taskId, newHead, prevHead) {
21291
- const ogHead = this.head.sort((a, b) => a.toString().localeCompare(b.toString()));
21292
- newHead = newHead.sort((a, b) => a.toString().localeCompare(b.toString()));
21293
- newHead.map(async (cid) => {
21294
- const got = await this.blocks.get(cid);
21295
- if (!got) {
21296
- throw new Error("int_applyHead missing block: " + cid.toString());
21297
- }
21298
- });
21299
- if (ogHead.toString() === newHead.toString()) {
21300
- return;
21301
- }
21302
- const ogPrev = prevHead.sort((a, b) => a.toString().localeCompare(b.toString()));
21303
- if (ogHead.toString() === ogPrev.toString()) {
21304
- this.setHead(newHead);
21305
- return;
21276
+ async processUpdates(updatesAcc, all, prevHead) {
21277
+ let internalUpdates = updatesAcc;
21278
+ if (this.watchers.size && !all) {
21279
+ const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
21280
+ internalUpdates = changes.result;
21306
21281
  }
21307
- let head = this.head;
21308
- const noLoader = false;
21309
- const withBlocks = async (fn) => {
21310
- if (!this.blocks)
21311
- throw new Error("missing blocks");
21312
- return await this.blocks.transaction(fn, void 0, { noLoader });
21313
- };
21314
- await withBlocks(async (tblocks) => {
21315
- for (const cid of newHead) {
21316
- try {
21317
- head = await advance(tblocks, head, cid);
21318
- } catch (e) {
21319
- console.error("failed to advance", cid.toString(), e);
21320
- continue;
21321
- }
21322
- }
21323
- const result = await root(tblocks, head);
21324
- for (const { cid, bytes } of [...result.additions, ...result.removals]) {
21325
- tblocks.putSync(cid, bytes);
21326
- }
21327
- return { head };
21328
- });
21329
- this.setHead(head);
21282
+ this.zoomers.forEach((fn) => fn());
21283
+ this.notifyWatchers(internalUpdates || []);
21330
21284
  }
21331
21285
  notifyWatchers(updates) {
21332
21286
  this.emptyWatchers.forEach((fn) => fn());
@@ -21341,7 +21295,62 @@ var CRDTClock = class {
21341
21295
  onZoom(fn) {
21342
21296
  this.zoomers.add(fn);
21343
21297
  }
21298
+ async int_applyHead(newHead, prevHead) {
21299
+ const ogHead = sortClockHead(this.head);
21300
+ newHead = sortClockHead(newHead);
21301
+ await validateBlocks(newHead, this.blocks);
21302
+ if (compareClockHeads(ogHead, newHead)) {
21303
+ return;
21304
+ }
21305
+ const ogPrev = sortClockHead(prevHead);
21306
+ if (compareClockHeads(ogHead, ogPrev)) {
21307
+ this.setHead(newHead);
21308
+ return;
21309
+ }
21310
+ let head = this.head;
21311
+ const noLoader = false;
21312
+ if (!this.blocks)
21313
+ throw new Error("missing blocks");
21314
+ await this.blocks.transaction(
21315
+ async (tblocks) => {
21316
+ head = await advanceBlocks(newHead, tblocks, head);
21317
+ const result = await root(tblocks, head);
21318
+ for (const { cid, bytes } of [...result.additions, ...result.removals]) {
21319
+ tblocks.putSync(cid, bytes);
21320
+ }
21321
+ return { head };
21322
+ },
21323
+ void 0,
21324
+ { noLoader }
21325
+ );
21326
+ this.setHead(head);
21327
+ }
21344
21328
  };
21329
+ function sortClockHead(clockHead) {
21330
+ return clockHead.sort((a, b) => a.toString().localeCompare(b.toString()));
21331
+ }
21332
+ async function validateBlocks(newHead, blocks) {
21333
+ newHead.map(async (cid) => {
21334
+ const got = await blocks.get(cid);
21335
+ if (!got) {
21336
+ throw new Error("int_applyHead missing block: " + cid.toString());
21337
+ }
21338
+ });
21339
+ }
21340
+ function compareClockHeads(head1, head2) {
21341
+ return head1.toString() === head2.toString();
21342
+ }
21343
+ async function advanceBlocks(newHead, tblocks, head) {
21344
+ for (const cid of newHead) {
21345
+ try {
21346
+ head = await advance(tblocks, head, cid);
21347
+ } catch (e) {
21348
+ console.error("failed to advance", cid.toString(), e);
21349
+ continue;
21350
+ }
21351
+ }
21352
+ return head;
21353
+ }
21345
21354
 
21346
21355
  // src/crdt.ts
21347
21356
  var CRDT = class {
@@ -21515,6 +21524,9 @@ var Database = class {
21515
21524
  }));
21516
21525
  return { rows, clock: head };
21517
21526
  }
21527
+ async allDocuments() {
21528
+ return this.allDocs();
21529
+ }
21518
21530
  subscribe(listener, updates) {
21519
21531
  if (updates) {
21520
21532
  if (!this._listening) {