@unicitylabs/sphere-sdk 0.14.11-dev.8 → 0.14.11

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.
package/dist/index.cjs CHANGED
@@ -13122,22 +13122,24 @@ var Receive = class {
13122
13122
  * the tokens just accepted, and the balance would still only move at the end.
13123
13123
  * Ordering is preserved: store() still precedes the ack (§5.7).
13124
13124
  */
13125
- async maybeRefresh(deps, pending, epochOf) {
13125
+ async maybeRefresh(ctx) {
13126
+ const { deps, pending, pageEpoch } = ctx;
13126
13127
  if (deps.refreshView === void 0) return;
13127
13128
  const now = deps.now?.() ?? Date.now();
13128
13129
  if (now - this.lastRefreshAt < REFRESH_INTERVAL_MS) return;
13129
13130
  this.lastRefreshAt = now;
13130
- await flushAcks(deps, pending, epochOf);
13131
- deps.refreshView();
13131
+ if ((await flushAcks(deps, pending, pageEpoch)).progressed) deps.refreshView();
13132
13132
  }
13133
13133
  /** One listing pass: process each entry, flushing and refreshing as it goes. */
13134
13134
  async drainPages(ctx, basis) {
13135
- const { deps, engine, pending, stored, pageEpoch } = ctx;
13135
+ const { deps, pending, pageEpoch, claimable } = ctx;
13136
13136
  for await (const entry of deps.delivery.incoming(basis === null ? void 0 : String(basis.cursor))) {
13137
- const storedBefore = stored.length;
13138
- await this.processEntry(deps, engine, entry, pending, stored);
13139
- if (pending.length >= ACK_BATCH_SIZE) await flushAcks(deps, pending, pageEpoch);
13140
- if (stored.length > storedBefore) await this.maybeRefresh(deps, pending, pageEpoch);
13137
+ const claimableBefore = claimable.n;
13138
+ await this.processEntry(ctx, entry);
13139
+ if (pending.length >= ACK_BATCH_SIZE) {
13140
+ if (!(await flushAcks(deps, pending, pageEpoch)).progressed) return;
13141
+ }
13142
+ if (claimable.n > claimableBefore) await this.maybeRefresh(ctx);
13141
13143
  }
13142
13144
  await flushAcks(deps, pending, pageEpoch);
13143
13145
  }
@@ -13148,11 +13150,16 @@ var Receive = class {
13148
13150
  const stored = [];
13149
13151
  const pending = [];
13150
13152
  const pageEpoch = () => deps.delivery.incomingEpoch() ?? deps.syncEpoch();
13153
+ const claimable = { n: 0 };
13154
+ const finish = () => {
13155
+ if (claimable.n > 0) deps.refreshView?.();
13156
+ return stored;
13157
+ };
13151
13158
  try {
13152
13159
  const record = await deps.kv.get(STORE_KEYS.streamCursor("mailbox"));
13153
13160
  let basis = record !== null && record.syncEpoch === deps.syncEpoch() ? record : null;
13154
13161
  for (let pass = 0; pass < 2; pass++) {
13155
- await this.drainPages({ deps, engine, pending, stored, pageEpoch }, basis);
13162
+ await this.drainPages({ deps, engine, pending, stored, pageEpoch, claimable }, basis);
13156
13163
  const served = deps.delivery.incomingEpoch();
13157
13164
  if (basis === null || served === null || served === basis.syncEpoch) break;
13158
13165
  await deps.kv.remove(STORE_KEYS.streamCursor("mailbox"));
@@ -13160,22 +13167,25 @@ var Receive = class {
13160
13167
  }
13161
13168
  } catch (err) {
13162
13169
  logger.warn("PaymentsV2", "receive drain interrupted \u2014 unacked entries retry next drain:", err);
13163
- if (isRetryableAckError(err)) return stored;
13170
+ if (isRetryableAckError(err)) return finish();
13164
13171
  await flushAcks(deps, pending, pageEpoch).catch((flushErr) => {
13165
13172
  logger.warn("PaymentsV2", "receive ack flush failed \u2014 cursor holds at the acked prefix:", flushErr);
13173
+ return { progressed: false };
13166
13174
  });
13167
13175
  }
13168
- if (stored.length > 0) deps.refreshView?.();
13169
- return stored;
13176
+ return finish();
13170
13177
  }
13171
- async processEntry(deps, engine, entry, pending, stored) {
13178
+ async processEntry(ctx, entry) {
13179
+ const { deps, engine, pending, stored, claimable } = ctx;
13172
13180
  const screened = await screen(deps, engine, entry);
13173
13181
  if (screened.kind === "ack") {
13174
13182
  pending.push(screened.ack);
13183
+ if (screened.ack.disposition === "claimed") claimable.n += 1;
13175
13184
  return;
13176
13185
  }
13177
13186
  await deps.view.store(screened.record);
13178
13187
  pending.push(claimAck(entry));
13188
+ claimable.n += 1;
13179
13189
  const transfer = await announce(deps, entry, screened.record);
13180
13190
  stored.push(transfer);
13181
13191
  deps.emit("transfer:incoming", transfer);
@@ -13236,7 +13246,8 @@ async function announce(deps, entry, record) {
13236
13246
  };
13237
13247
  }
13238
13248
  async function flushAcks(deps, pending, epochOf) {
13239
- if (pending.length === 0) return;
13249
+ if (pending.length === 0) return { progressed: true };
13250
+ const before = pending.length;
13240
13251
  let lastAcked = null;
13241
13252
  try {
13242
13253
  const settled = await settleAcks(deps, pending);
@@ -13254,6 +13265,7 @@ async function flushAcks(deps, pending, epochOf) {
13254
13265
  await deps.kv.set(STORE_KEYS.streamCursor("mailbox"), record);
13255
13266
  }
13256
13267
  }
13268
+ return { progressed: pending.length < before };
13257
13269
  }
13258
13270
  async function settleAcks(deps, pending) {
13259
13271
  const batch = deps.delivery.ackBatch?.bind(deps.delivery);
@@ -15774,7 +15786,9 @@ var WalletApiDeliveryPort = class {
15774
15786
  await this.settleClaims(acks, settled, outcomes);
15775
15787
  await this.settleRejects(acks, settled);
15776
15788
  } catch (err) {
15777
- await this.addSeenAll(settled).catch(() => void 0);
15789
+ await this.addSeenAll(settled).catch((seenErr) => {
15790
+ logger.warn("WalletApiDelivery", `seen-set write failed for ${String(settled.length)} committed acks:`, seenErr);
15791
+ });
15778
15792
  throw asRetryable(err);
15779
15793
  }
15780
15794
  await this.addSeenAll(settled);