@rdlabo/ionic-angular-kit 21.5.28 → 21.5.29

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.
@@ -2372,6 +2372,24 @@ class OfflineSyncService {
2372
2372
  return this.#enqueue(prepared.request, options, generation, prepared.replicaTransaction);
2373
2373
  });
2374
2374
  }
2375
+ /**
2376
+ * Prepares and commits multiple Outbox commands as one serialized replica
2377
+ * transaction under a single captured session generation.
2378
+ *
2379
+ * All entries are prepared and validated before any write. Empty batches,
2380
+ * overlapping aggregate intents, and overlapping replica footprints are
2381
+ * rejected with no durable state change.
2382
+ */
2383
+ enqueuePreparedBatch(prepare, options = {}) {
2384
+ const generation = this.#generation;
2385
+ return this.#serializeReplicaMutation(async () => {
2386
+ await this.initialize();
2387
+ if (!this.#isCurrent(generation))
2388
+ throw new Error('Offline session changed before prepared batch enqueue.');
2389
+ const prepared = await prepare(this.#repository);
2390
+ return this.#enqueuePreparedBatch(prepared, options, generation);
2391
+ });
2392
+ }
2375
2393
  /**
2376
2394
  * Atomically replaces a resolved durable command with a newly prepared command.
2377
2395
  *
@@ -2428,6 +2446,29 @@ class OfflineSyncService {
2428
2446
  return this.#replicaMutations.run(operation);
2429
2447
  }
2430
2448
  async #enqueue(request, options, generation, replicaTransaction, replaced) {
2449
+ const session = await this.#beginEnqueueSession(generation);
2450
+ this.#assertEnqueueScope(session, request.scopeId);
2451
+ const materialization = await this.#materializeEnqueue(session.userId, request, replicaTransaction, replaced);
2452
+ await this.#assertOutboxCapacity(session.userId, [materialization.command], replaced?.commandId);
2453
+ await this.#commitMaterializedEnqueues([materialization], generation, options, replaced ? [replaced.commandId] : undefined);
2454
+ return materialization.command.commandId;
2455
+ }
2456
+ async #enqueuePreparedBatch(prepared, options, generation) {
2457
+ if (prepared.length === 0) {
2458
+ throw new Error('Prepared offline batch must contain at least one command.');
2459
+ }
2460
+ const session = await this.#beginEnqueueSession(generation);
2461
+ const materializations = [];
2462
+ for (const entry of prepared) {
2463
+ this.#assertEnqueueScope(session, entry.request.scopeId);
2464
+ materializations.push(await this.#materializeEnqueue(session.userId, entry.request, entry.replicaTransaction));
2465
+ }
2466
+ this.#assertDistinctBatchFootprints(materializations);
2467
+ await this.#assertOutboxCapacity(session.userId, materializations.map((item) => item.command));
2468
+ await this.#commitMaterializedEnqueues(materializations, generation, options);
2469
+ return materializations.map((item) => item.command.commandId);
2470
+ }
2471
+ async #beginEnqueueSession(generation) {
2431
2472
  await this.initialize();
2432
2473
  if (this.#options.mode === 'readCacheOnly') {
2433
2474
  throw new Error('This offline provider is configured as a read-only cache and cannot enqueue commands.');
@@ -2436,12 +2477,20 @@ class OfflineSyncService {
2436
2477
  if (!session)
2437
2478
  throw new Error('Cannot enqueue an offline command without an authenticated user');
2438
2479
  this.#assertSessionPrincipalBoundary(session);
2439
- const userId = session.userId;
2440
- this.#setActiveUser(userId);
2441
- const scope = { userId, scopeId: request.scopeId };
2442
- if (!session.scopes.some((candidate) => candidate.userId === userId && candidate.scopeId === request.scopeId)) {
2443
- throw new Error(`Offline sync session does not include scope "${request.scopeId}".`);
2480
+ this.#setActiveUser(session.userId);
2481
+ if (!this.#isCurrent(generation)) {
2482
+ throw new Error('Offline session changed before the command could be persisted');
2483
+ }
2484
+ return session;
2485
+ }
2486
+ #assertEnqueueScope(session, scopeId) {
2487
+ if (!session.scopes.some((candidate) => candidate.userId === session.userId && candidate.scopeId === scopeId)) {
2488
+ throw new Error(`Offline sync session does not include scope "${scopeId}".`);
2444
2489
  }
2490
+ this.noteScope({ userId: session.userId, scopeId });
2491
+ }
2492
+ async #materializeEnqueue(userId, request, replicaTransaction, replaced) {
2493
+ const scope = { userId, scopeId: request.scopeId };
2445
2494
  this.noteScope(scope);
2446
2495
  const commandIdentity = offlineCommandLookupIdentity(request.identity);
2447
2496
  const normalized = await this.#normalizeEnqueueRequest(scope, request, commandIdentity);
@@ -2543,20 +2592,44 @@ class OfflineSyncService {
2543
2592
  this.#canonicalJson(replicaTransaction);
2544
2593
  if (optimisticCompanions.length > 0)
2545
2594
  command = { ...command, optimisticCompanions };
2546
- await this.#assertOutboxCapacity(userId, command, replaced?.commandId);
2595
+ return { command, optimisticRow, optimisticCompanions };
2596
+ }
2597
+ #assertDistinctBatchFootprints(entries) {
2598
+ const aggregates = new Set();
2599
+ const replicaKeys = new Set();
2600
+ for (const entry of entries) {
2601
+ const aggregate = this.#aggregateKey(entry.command);
2602
+ if (aggregates.has(aggregate)) {
2603
+ throw new Error('Prepared offline batch contains overlapping aggregate intents.');
2604
+ }
2605
+ aggregates.add(aggregate);
2606
+ for (const key of [
2607
+ this.#replicaRowKey(entry.optimisticRow),
2608
+ ...entry.optimisticCompanions.map((companion) => this.#replicaRowKey(companion.key)),
2609
+ ]) {
2610
+ if (replicaKeys.has(key)) {
2611
+ throw new Error('Prepared offline batch contains overlapping replica footprints.');
2612
+ }
2613
+ replicaKeys.add(key);
2614
+ }
2615
+ }
2616
+ }
2617
+ async #commitMaterializedEnqueues(entries, generation, options, removeCommandIds) {
2547
2618
  if (generation !== this.#generation) {
2548
2619
  throw new Error('Offline session changed before the command could be persisted');
2549
2620
  }
2550
2621
  await this.#repository.transactReplica({
2551
- putRows: [optimisticRow, ...optimisticCompanions.flatMap((companion) => (companion.after ? [companion.after] : []))],
2552
- removeRows: optimisticCompanions.flatMap((companion) => (companion.after ? [] : [companion.key])),
2553
- putCommands: [command],
2554
- removeCommandIds: replaced ? [replaced.commandId] : undefined,
2622
+ putRows: entries.flatMap((entry) => [
2623
+ entry.optimisticRow,
2624
+ ...entry.optimisticCompanions.flatMap((companion) => (companion.after ? [companion.after] : [])),
2625
+ ]),
2626
+ removeRows: entries.flatMap((entry) => entry.optimisticCompanions.flatMap((companion) => (companion.after ? [] : [companion.key]))),
2627
+ putCommands: entries.map((entry) => entry.command),
2628
+ removeCommandIds,
2555
2629
  });
2556
- await this.#refreshState();
2630
+ await this.#refreshState().catch((error) => this.#reportError(error));
2557
2631
  if (options.flush !== false && this.#network.connected())
2558
2632
  this.#flushInBackground();
2559
- return commandId;
2560
2633
  }
2561
2634
  async #prepareOptimisticCompanions(scope, optimisticRow, transaction) {
2562
2635
  if (!transaction)
@@ -2640,7 +2713,7 @@ class OfflineSyncService {
2640
2713
  #replicaRowKey(key) {
2641
2714
  return `${canonicalOfflinePrincipalId(key.userId)}:${key.scopeId}:${key.sourceKey}:${canonicalOfflineReplicaIdentity(key.identity)}`;
2642
2715
  }
2643
- async #assertOutboxCapacity(userId, command, excludingCommandId) {
2716
+ async #assertOutboxCapacity(userId, newCommands, excludingCommandId) {
2644
2717
  const currentCommands = this.#repository.getCommandsForUser
2645
2718
  ? await this.#repository.getCommandsForUser(userId)
2646
2719
  : (await Promise.all([...this.#knownScopes.values()].filter((scope) => scope.userId === userId).map((scope) => this.#repository.getCommands(scope)))).flat();
@@ -2650,10 +2723,10 @@ class OfflineSyncService {
2650
2723
  const maxCommands = this.#options.outboxLimits?.maxCommandsPerUser ?? DEFAULT_MAX_OUTBOX_COMMANDS_PER_USER;
2651
2724
  const maxBytes = this.#options.outboxLimits?.maxBytesPerUser ?? DEFAULT_MAX_OUTBOX_BYTES_PER_USER;
2652
2725
  const currentBytes = this.#serializedOutboxBytes(commands);
2653
- if (commands.length >= maxCommands) {
2726
+ if (commands.length + newCommands.length > maxCommands) {
2654
2727
  throw new OfflineOutboxCapacityError('command_count', commands.length, currentBytes);
2655
2728
  }
2656
- const nextBytes = currentBytes + this.#serializedOutboxBytes([command]);
2729
+ const nextBytes = this.#serializedOutboxBytes([...commands, ...newCommands]);
2657
2730
  if (nextBytes > maxBytes) {
2658
2731
  throw new OfflineOutboxCapacityError('serialized_bytes', commands.length, currentBytes);
2659
2732
  }