@fireproof/core 0.14.8 → 0.15.0-dev

Sign up to get free protection for your applications and to get access to all the features.
@@ -11583,7 +11583,7 @@ replaceTraps((oldTraps) => ({
11583
11583
  }));
11584
11584
 
11585
11585
  // src/version.ts
11586
- var PACKAGE_VERSION = "0.14.8";
11586
+ var PACKAGE_VERSION = "0.15.0-dev";
11587
11587
 
11588
11588
  // src/store.ts
11589
11589
  var match = PACKAGE_VERSION.match(/^([^.]*\.[^.]*)/);
@@ -11612,11 +11612,11 @@ var MetaStore = class extends VersionedStore {
11612
11612
  }
11613
11613
  };
11614
11614
  var DataStore = class {
11615
- constructor(loader) {
11615
+ constructor(name8) {
11616
11616
  __publicField(this, "tag", "car-base");
11617
11617
  __publicField(this, "STORAGE_VERSION", STORAGE_VERSION);
11618
- __publicField(this, "loader");
11619
- this.loader = loader;
11618
+ __publicField(this, "name");
11619
+ this.name = name8;
11620
11620
  }
11621
11621
  };
11622
11622
 
@@ -11781,7 +11781,7 @@ var DataStore2 = class extends DataStore {
11781
11781
  }
11782
11782
  async _withDB(dbWorkFun) {
11783
11783
  if (!this.idb) {
11784
- const dbName = `fp.${this.STORAGE_VERSION}.${this.loader.keyId}.${this.loader.name}`;
11784
+ const dbName = `fp.${this.STORAGE_VERSION}.${this.name}`;
11785
11785
  this.idb = await openDB(dbName, 1, {
11786
11786
  upgrade(db) {
11787
11787
  db.createObjectStore("cars");
@@ -11916,7 +11916,7 @@ var Loader = class {
11916
11916
  this.name = name8;
11917
11917
  this.opts = opts || this.opts;
11918
11918
  this.metaStore = new MetaStore2(this.name);
11919
- this.carStore = new DataStore2(this);
11919
+ this.carStore = new DataStore2(this.name);
11920
11920
  this.remoteWAL = new RemoteWAL2(this);
11921
11921
  this.ready = Promise.resolve().then(async () => {
11922
11922
  if (!this.metaStore || !this.carStore || !this.remoteWAL)
@@ -20990,7 +20990,7 @@ var _DbLoader = class _DbLoader extends Loader {
20990
20990
  __publicField(this, "writing", Promise.resolve());
20991
20991
  __publicField(this, "remoteFileStore");
20992
20992
  __publicField(this, "fileStore");
20993
- this.fileStore = new DataStore2(this);
20993
+ this.fileStore = new DataStore2(this.name);
20994
20994
  this.clock = clock;
20995
20995
  }
20996
20996
  async _readyForMerge() {
@@ -21198,7 +21198,7 @@ function applyHeadQueue(worker) {
21198
21198
  const task = queue.shift();
21199
21199
  if (!task)
21200
21200
  continue;
21201
- await worker(task.id, task.newHead, task.prevHead, task.updates);
21201
+ await worker(task.newHead, task.prevHead);
21202
21202
  if (task.updates) {
21203
21203
  allUpdates.push(...task.updates);
21204
21204
  }
@@ -21228,7 +21228,7 @@ function applyHeadQueue(worker) {
21228
21228
  // src/crdt-clock.ts
21229
21229
  var CRDTClock = class {
21230
21230
  constructor() {
21231
- // todo: keep the clock of remote and local changes separate, merge on read
21231
+ // todo: track local and remote clocks independently, merge on read
21232
21232
  // that way we can drop the whole remote if we need to
21233
21233
  // should go with making sure the local clock only references locally available blocks on write
21234
21234
  __publicField(this, "head", []);
@@ -21243,68 +21243,22 @@ var CRDTClock = class {
21243
21243
  this.head = head;
21244
21244
  }
21245
21245
  async applyHead(newHead, prevHead, updates = null) {
21246
- const taskId = Math.random().toString().slice(2, 8);
21247
21246
  for await (const { updates: updatesAcc, all } of this.applyHeadQueue.push({
21248
- id: taskId,
21249
- // tblocks,
21250
21247
  newHead,
21251
21248
  prevHead,
21252
21249
  updates
21253
21250
  })) {
21254
- ;
21255
- ((updatesAcc2, all2) => {
21256
- void Promise.resolve().then(async () => {
21257
- let intUpdates = updatesAcc2;
21258
- if (this.watchers.size && !all2) {
21259
- const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
21260
- intUpdates = changes.result;
21261
- }
21262
- this.zoomers.forEach((fn) => fn());
21263
- this.notifyWatchers(intUpdates || []);
21264
- });
21265
- })([...updatesAcc], all);
21251
+ this.processUpdates(updatesAcc, all, prevHead);
21266
21252
  }
21267
21253
  }
21268
- async int_applyHead(taskId, newHead, prevHead) {
21269
- const ogHead = this.head.sort((a, b) => a.toString().localeCompare(b.toString()));
21270
- newHead = newHead.sort((a, b) => a.toString().localeCompare(b.toString()));
21271
- newHead.map(async (cid) => {
21272
- const got = await this.blocks.get(cid);
21273
- if (!got) {
21274
- throw new Error("int_applyHead missing block: " + cid.toString());
21275
- }
21276
- });
21277
- if (ogHead.toString() === newHead.toString()) {
21278
- return;
21279
- }
21280
- const ogPrev = prevHead.sort((a, b) => a.toString().localeCompare(b.toString()));
21281
- if (ogHead.toString() === ogPrev.toString()) {
21282
- this.setHead(newHead);
21283
- return;
21254
+ async processUpdates(updatesAcc, all, prevHead) {
21255
+ let internalUpdates = updatesAcc;
21256
+ if (this.watchers.size && !all) {
21257
+ const changes = await clockChangesSince(this.blocks, this.head, prevHead, {});
21258
+ internalUpdates = changes.result;
21284
21259
  }
21285
- let head = this.head;
21286
- const noLoader = false;
21287
- const withBlocks = async (fn) => {
21288
- if (!this.blocks)
21289
- throw new Error("missing blocks");
21290
- return await this.blocks.transaction(fn, void 0, { noLoader });
21291
- };
21292
- await withBlocks(async (tblocks) => {
21293
- for (const cid of newHead) {
21294
- try {
21295
- head = await advance(tblocks, head, cid);
21296
- } catch (e) {
21297
- console.error("failed to advance", cid.toString(), e);
21298
- continue;
21299
- }
21300
- }
21301
- const result = await root(tblocks, head);
21302
- for (const { cid, bytes } of [...result.additions, ...result.removals]) {
21303
- tblocks.putSync(cid, bytes);
21304
- }
21305
- return { head };
21306
- });
21307
- this.setHead(head);
21260
+ this.zoomers.forEach((fn) => fn());
21261
+ this.notifyWatchers(internalUpdates || []);
21308
21262
  }
21309
21263
  notifyWatchers(updates) {
21310
21264
  this.emptyWatchers.forEach((fn) => fn());
@@ -21319,7 +21273,62 @@ var CRDTClock = class {
21319
21273
  onZoom(fn) {
21320
21274
  this.zoomers.add(fn);
21321
21275
  }
21276
+ async int_applyHead(newHead, prevHead) {
21277
+ const ogHead = sortClockHead(this.head);
21278
+ newHead = sortClockHead(newHead);
21279
+ await validateBlocks(newHead, this.blocks);
21280
+ if (compareClockHeads(ogHead, newHead)) {
21281
+ return;
21282
+ }
21283
+ const ogPrev = sortClockHead(prevHead);
21284
+ if (compareClockHeads(ogHead, ogPrev)) {
21285
+ this.setHead(newHead);
21286
+ return;
21287
+ }
21288
+ let head = this.head;
21289
+ const noLoader = false;
21290
+ if (!this.blocks)
21291
+ throw new Error("missing blocks");
21292
+ await this.blocks.transaction(
21293
+ async (tblocks) => {
21294
+ head = await advanceBlocks(newHead, tblocks, head);
21295
+ const result = await root(tblocks, head);
21296
+ for (const { cid, bytes } of [...result.additions, ...result.removals]) {
21297
+ tblocks.putSync(cid, bytes);
21298
+ }
21299
+ return { head };
21300
+ },
21301
+ void 0,
21302
+ { noLoader }
21303
+ );
21304
+ this.setHead(head);
21305
+ }
21322
21306
  };
21307
+ function sortClockHead(clockHead) {
21308
+ return clockHead.sort((a, b) => a.toString().localeCompare(b.toString()));
21309
+ }
21310
+ async function validateBlocks(newHead, blocks) {
21311
+ newHead.map(async (cid) => {
21312
+ const got = await blocks.get(cid);
21313
+ if (!got) {
21314
+ throw new Error("int_applyHead missing block: " + cid.toString());
21315
+ }
21316
+ });
21317
+ }
21318
+ function compareClockHeads(head1, head2) {
21319
+ return head1.toString() === head2.toString();
21320
+ }
21321
+ async function advanceBlocks(newHead, tblocks, head) {
21322
+ for (const cid of newHead) {
21323
+ try {
21324
+ head = await advance(tblocks, head, cid);
21325
+ } catch (e) {
21326
+ console.error("failed to advance", cid.toString(), e);
21327
+ continue;
21328
+ }
21329
+ }
21330
+ return head;
21331
+ }
21323
21332
 
21324
21333
  // src/crdt.ts
21325
21334
  var CRDT = class {
@@ -21493,6 +21502,9 @@ var Database = class {
21493
21502
  }));
21494
21503
  return { rows, clock: head };
21495
21504
  }
21505
+ async allDocuments() {
21506
+ return this.allDocs();
21507
+ }
21496
21508
  subscribe(listener, updates) {
21497
21509
  if (updates) {
21498
21510
  if (!this._listening) {