@fireproof/core 0.19.100 → 0.19.102

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -356,6 +356,8 @@ var BaseBlockstore = class {
356
356
  }
357
357
  async destroy() {
358
358
  }
359
+ async compact() {
360
+ }
359
361
  async get(cid) {
360
362
  if (!cid) throw this.logger.Error().Msg("required cid").AsError();
361
363
  for (const f of this.transactions) {
@@ -374,6 +376,21 @@ var BaseBlockstore = class {
374
376
  this.lastTxMeta = done;
375
377
  return { t, meta: done };
376
378
  }
379
+ openTransaction(opts = { add: true, noLoader: false }) {
380
+ return new CarTransaction(this, opts);
381
+ }
382
+ async commitTransaction(t, done, opts) {
383
+ if (!this.loader) throw this.logger.Error().Msg("loader required to commit").AsError();
384
+ const cars = await this.loader?.commit(t, done, opts);
385
+ if (this.ebOpts.autoCompact && this.loader.carLog.length > this.ebOpts.autoCompact) {
386
+ setTimeout(() => void this.compact(), 10);
387
+ }
388
+ if (cars) {
389
+ this.transactions.delete(t);
390
+ return { meta: done, cars, t };
391
+ }
392
+ throw this.logger.Error().Msg("failed to commit car files").AsError();
393
+ }
377
394
  async *entries() {
378
395
  const seen = /* @__PURE__ */ new Set();
379
396
  for (const t of this.transactions) {
@@ -3050,21 +3067,23 @@ var CRDTClock = class {
3050
3067
  throw this.logger.Error().Msg("missing blockstore").AsError();
3051
3068
  }
3052
3069
  await validateBlocks(this.logger, newHead, this.blockstore);
3053
- const { meta } = await this.blockstore.transaction(
3054
- async (tblocks) => {
3055
- const advancedHead = await advanceBlocks(this.logger, newHead, tblocks, this.head);
3056
- const result = await root2(tblocks, advancedHead);
3057
- for (const { cid, bytes } of [
3058
- ...result.additions
3059
- // ...result.removals
3060
- ]) {
3061
- tblocks.putSync(cid, bytes);
3062
- }
3063
- return { head: advancedHead };
3064
- },
3065
- { noLoader, add: false }
3066
- );
3067
- this.setHead(meta.head);
3070
+ if (!this.transaction) {
3071
+ this.transaction = this.blockstore.openTransaction({ noLoader, add: false });
3072
+ }
3073
+ const tblocks = this.transaction;
3074
+ const advancedHead = await advanceBlocks(this.logger, newHead, tblocks, this.head);
3075
+ const result = await root2(tblocks, advancedHead);
3076
+ for (const { cid, bytes } of [
3077
+ ...result.additions
3078
+ // ...result.removals
3079
+ ]) {
3080
+ tblocks.putSync(cid, bytes);
3081
+ }
3082
+ if (!noLoader) {
3083
+ await this.blockstore.commitTransaction(tblocks, { head: advancedHead }, { add: false, noLoader });
3084
+ this.transaction = void 0;
3085
+ }
3086
+ this.setHead(advancedHead);
3068
3087
  }
3069
3088
  };
3070
3089
  function sortClockHead(clockHead) {
@@ -3427,7 +3446,7 @@ import { runtimeFn as runtimeFn2 } from "@adviser/cement";
3427
3446
 
3428
3447
  // src/version.ts
3429
3448
  var PACKAGE_VERSION = Object.keys({
3430
- "0.19.100": "xxxx"
3449
+ "0.19.102": "xxxx"
3431
3450
  })[0];
3432
3451
  export {
3433
3452
  CRDT,