kaafil-js 0.2.0 → 0.3.0

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.
@@ -2585,6 +2585,11 @@ function createDrainer(options) {
2585
2585
  row: outcome.entity,
2586
2586
  ...outcome.serverTime !== void 0 ? { syncedAt: outcome.serverTime } : {}
2587
2587
  });
2588
+ const temporaryId = entity.temporaryId;
2589
+ if (temporaryId !== void 0 && temporaryId !== outcome.entity.id) {
2590
+ await snapshot.purgeEntity(op.tripRef, entity.listName, temporaryId);
2591
+ changed = true;
2592
+ }
2588
2593
  }
2589
2594
  if (changed && outcome.serverTime !== void 0) {
2590
2595
  events.emit("snapshot.updated", {
@@ -2684,6 +2689,16 @@ function createDrainer(options) {
2684
2689
  switch (result.action) {
2685
2690
  case "drop":
2686
2691
  await outbox.drop(op.id);
2692
+ if (op.entity?.temporaryId !== void 0) {
2693
+ await snapshot.purgeEntity(op.tripRef, op.entity.listName, op.entity.temporaryId);
2694
+ if (outcome.serverTime !== void 0) {
2695
+ events.emit("snapshot.updated", {
2696
+ tripRef: op.tripRef,
2697
+ lists: [op.entity.listName],
2698
+ serverTime: outcome.serverTime
2699
+ });
2700
+ }
2701
+ }
2687
2702
  return "terminal";
2688
2703
  case "reissue":
2689
2704
  await outbox.reissue(op.id, {
@@ -3517,6 +3532,7 @@ function createOfflineEngine(options) {
3517
3532
  });
3518
3533
  const snapshot = createSnapshotStore({ storage: options.storage, keyPrefix });
3519
3534
  const reads = createReadCacheStore({ storage: options.storage, keyPrefix });
3535
+ const mintEntityId = () => `tmp_${options.newId?.() ?? newIdempotencyKey()}`;
3520
3536
  const outbox = createOutbox({
3521
3537
  storage: options.storage,
3522
3538
  keyPrefix,
@@ -3565,6 +3581,13 @@ function createOfflineEngine(options) {
3565
3581
  let unbind;
3566
3582
  async function enqueue(input) {
3567
3583
  share?.assertWritable();
3584
+ let entity = input.entity;
3585
+ let temporaryId;
3586
+ if (input.creates === true && input.entity !== void 0) {
3587
+ const { temporaryId: _ignored, ...ref } = input.entity;
3588
+ temporaryId = mintEntityId();
3589
+ entity = { ...ref, id: temporaryId, temporaryId };
3590
+ }
3568
3591
  const op = await outbox.enqueue({
3569
3592
  tripRef: input.tripRef,
3570
3593
  method: input.method,
@@ -3572,16 +3595,25 @@ function createOfflineEngine(options) {
3572
3595
  operationId: input.operationId,
3573
3596
  ...input.body !== void 0 ? { body: input.body } : {},
3574
3597
  ...input.ifMatch !== void 0 ? { ifMatch: input.ifMatch } : {},
3575
- ...input.entity !== void 0 ? { entity: input.entity } : {},
3598
+ ...entity !== void 0 ? { entity } : {},
3576
3599
  ...input.idempotencyKey !== void 0 ? { idempotencyKey: input.idempotencyKey } : {},
3577
3600
  ...input.enqueuedAt !== void 0 ? { enqueuedAt: input.enqueuedAt } : {}
3578
3601
  });
3579
- if (input.optimistic !== void 0 && input.entity !== void 0) {
3602
+ if (input.optimistic !== void 0 && entity !== void 0) {
3580
3603
  await snapshot.upsert({
3581
3604
  tripRef: input.tripRef,
3582
- listName: input.entity.listName,
3583
- row: input.optimistic
3605
+ listName: entity.listName,
3606
+ // A create's row is keyed on the minted id rather than on whatever
3607
+ // `id` the caller happened to put in the optimistic row — those two
3608
+ // disagreeing is what would leave an orphan the drainer cannot find.
3609
+ row: temporaryId === void 0 ? input.optimistic : { ...input.optimistic, id: temporaryId }
3584
3610
  });
3611
+ if (temporaryId !== void 0) {
3612
+ events.emit("snapshot.updated", {
3613
+ tripRef: input.tripRef,
3614
+ lists: [entity.listName]
3615
+ });
3616
+ }
3585
3617
  }
3586
3618
  drainer.kick();
3587
3619
  return op;
@@ -3606,6 +3638,22 @@ function createOfflineEngine(options) {
3606
3638
  drainer.kick();
3607
3639
  },
3608
3640
  enqueue,
3641
+ async discard(opId) {
3642
+ const op = outbox.get(opId);
3643
+ if (op === void 0) {
3644
+ return;
3645
+ }
3646
+ await outbox.drop(opId);
3647
+ const temporaryId = op.entity?.temporaryId;
3648
+ if (op.entity === void 0 || temporaryId === void 0) {
3649
+ return;
3650
+ }
3651
+ await snapshot.purgeEntity(op.tripRef, op.entity.listName, temporaryId);
3652
+ events.emit("snapshot.updated", {
3653
+ tripRef: op.tripRef,
3654
+ lists: [op.entity.listName]
3655
+ });
3656
+ },
3609
3657
  async enqueueWithBlob(input) {
3610
3658
  const { blob, ...rest } = input;
3611
3659
  share?.assertWritable();