@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.
@@ -39,10 +39,16 @@ __export(payments_v2_exports, {
39
39
  PaymentsFacade: () => PaymentsFacade,
40
40
  STORE_KEYS: () => STORE_KEYS,
41
41
  createScopedKV: () => createScopedKV,
42
+ isRetryableAckError: () => isRetryableAckError,
42
43
  supportsDeterministicMint: () => supportsDeterministicMint
43
44
  });
44
45
  module.exports = __toCommonJS(payments_v2_exports);
45
46
 
47
+ // modules/payments-v2/ports.ts
48
+ function isRetryableAckError(err) {
49
+ return err !== null && typeof err === "object" && err.retryable === true;
50
+ }
51
+
46
52
  // modules/payments-v2/stores.ts
47
53
  function createScopedKV(storage, network, chainPubkey) {
48
54
  const prefix = `pv2:${network}:${chainPubkey}:`;
@@ -3239,11 +3245,6 @@ var logger = {
3239
3245
  }
3240
3246
  };
3241
3247
 
3242
- // modules/payments-v2/ports.ts
3243
- function isRetryableAckError(err) {
3244
- return err !== null && typeof err === "object" && err.retryable === true;
3245
- }
3246
-
3247
3248
  // modules/payments-v2/receive/Receive.ts
3248
3249
  var ACK_BATCH_SIZE = 200;
3249
3250
  var REFRESH_INTERVAL_MS = 2500;
@@ -3292,22 +3293,24 @@ var Receive = class {
3292
3293
  * the tokens just accepted, and the balance would still only move at the end.
3293
3294
  * Ordering is preserved: store() still precedes the ack (§5.7).
3294
3295
  */
3295
- async maybeRefresh(deps, pending, epochOf) {
3296
+ async maybeRefresh(ctx) {
3297
+ const { deps, pending, pageEpoch } = ctx;
3296
3298
  if (deps.refreshView === void 0) return;
3297
3299
  const now = deps.now?.() ?? Date.now();
3298
3300
  if (now - this.lastRefreshAt < REFRESH_INTERVAL_MS) return;
3299
3301
  this.lastRefreshAt = now;
3300
- await flushAcks(deps, pending, epochOf);
3301
- deps.refreshView();
3302
+ if ((await flushAcks(deps, pending, pageEpoch)).progressed) deps.refreshView();
3302
3303
  }
3303
3304
  /** One listing pass: process each entry, flushing and refreshing as it goes. */
3304
3305
  async drainPages(ctx, basis) {
3305
- const { deps, engine, pending, stored, pageEpoch } = ctx;
3306
+ const { deps, pending, pageEpoch, claimable } = ctx;
3306
3307
  for await (const entry of deps.delivery.incoming(basis === null ? void 0 : String(basis.cursor))) {
3307
- const storedBefore = stored.length;
3308
- await this.processEntry(deps, engine, entry, pending, stored);
3309
- if (pending.length >= ACK_BATCH_SIZE) await flushAcks(deps, pending, pageEpoch);
3310
- if (stored.length > storedBefore) await this.maybeRefresh(deps, pending, pageEpoch);
3308
+ const claimableBefore = claimable.n;
3309
+ await this.processEntry(ctx, entry);
3310
+ if (pending.length >= ACK_BATCH_SIZE) {
3311
+ if (!(await flushAcks(deps, pending, pageEpoch)).progressed) return;
3312
+ }
3313
+ if (claimable.n > claimableBefore) await this.maybeRefresh(ctx);
3311
3314
  }
3312
3315
  await flushAcks(deps, pending, pageEpoch);
3313
3316
  }
@@ -3318,11 +3321,16 @@ var Receive = class {
3318
3321
  const stored = [];
3319
3322
  const pending = [];
3320
3323
  const pageEpoch = () => deps.delivery.incomingEpoch() ?? deps.syncEpoch();
3324
+ const claimable = { n: 0 };
3325
+ const finish = () => {
3326
+ if (claimable.n > 0) deps.refreshView?.();
3327
+ return stored;
3328
+ };
3321
3329
  try {
3322
3330
  const record = await deps.kv.get(STORE_KEYS.streamCursor("mailbox"));
3323
3331
  let basis = record !== null && record.syncEpoch === deps.syncEpoch() ? record : null;
3324
3332
  for (let pass = 0; pass < 2; pass++) {
3325
- await this.drainPages({ deps, engine, pending, stored, pageEpoch }, basis);
3333
+ await this.drainPages({ deps, engine, pending, stored, pageEpoch, claimable }, basis);
3326
3334
  const served = deps.delivery.incomingEpoch();
3327
3335
  if (basis === null || served === null || served === basis.syncEpoch) break;
3328
3336
  await deps.kv.remove(STORE_KEYS.streamCursor("mailbox"));
@@ -3330,22 +3338,25 @@ var Receive = class {
3330
3338
  }
3331
3339
  } catch (err) {
3332
3340
  logger.warn("PaymentsV2", "receive drain interrupted \u2014 unacked entries retry next drain:", err);
3333
- if (isRetryableAckError(err)) return stored;
3341
+ if (isRetryableAckError(err)) return finish();
3334
3342
  await flushAcks(deps, pending, pageEpoch).catch((flushErr) => {
3335
3343
  logger.warn("PaymentsV2", "receive ack flush failed \u2014 cursor holds at the acked prefix:", flushErr);
3344
+ return { progressed: false };
3336
3345
  });
3337
3346
  }
3338
- if (stored.length > 0) deps.refreshView?.();
3339
- return stored;
3347
+ return finish();
3340
3348
  }
3341
- async processEntry(deps, engine, entry, pending, stored) {
3349
+ async processEntry(ctx, entry) {
3350
+ const { deps, engine, pending, stored, claimable } = ctx;
3342
3351
  const screened = await screen(deps, engine, entry);
3343
3352
  if (screened.kind === "ack") {
3344
3353
  pending.push(screened.ack);
3354
+ if (screened.ack.disposition === "claimed") claimable.n += 1;
3345
3355
  return;
3346
3356
  }
3347
3357
  await deps.view.store(screened.record);
3348
3358
  pending.push(claimAck(entry));
3359
+ claimable.n += 1;
3349
3360
  const transfer = await announce(deps, entry, screened.record);
3350
3361
  stored.push(transfer);
3351
3362
  deps.emit("transfer:incoming", transfer);
@@ -3406,7 +3417,8 @@ async function announce(deps, entry, record) {
3406
3417
  };
3407
3418
  }
3408
3419
  async function flushAcks(deps, pending, epochOf) {
3409
- if (pending.length === 0) return;
3420
+ if (pending.length === 0) return { progressed: true };
3421
+ const before = pending.length;
3410
3422
  let lastAcked = null;
3411
3423
  try {
3412
3424
  const settled = await settleAcks(deps, pending);
@@ -3424,6 +3436,7 @@ async function flushAcks(deps, pending, epochOf) {
3424
3436
  await deps.kv.set(STORE_KEYS.streamCursor("mailbox"), record);
3425
3437
  }
3426
3438
  }
3439
+ return { progressed: pending.length < before };
3427
3440
  }
3428
3441
  async function settleAcks(deps, pending) {
3429
3442
  const batch = deps.delivery.ackBatch?.bind(deps.delivery);
@@ -5027,6 +5040,7 @@ var PaymentsFacade = class {
5027
5040
  PaymentsFacade,
5028
5041
  STORE_KEYS,
5029
5042
  createScopedKV,
5043
+ isRetryableAckError,
5030
5044
  supportsDeterministicMint
5031
5045
  });
5032
5046
  /*! Bundled license information: