@rdlabo/ionic-angular-kit 21.5.28 → 21.5.30
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,34 @@ 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 currentCommands = await this.#commandsForUser(session.userId);
|
|
2462
|
+
this.#rememberCreatedAt(currentCommands);
|
|
2463
|
+
const firstCreatedAt = Math.max(Date.now(), this.#lastCommandCreatedAt + 1);
|
|
2464
|
+
this.#lastCommandCreatedAt = firstCreatedAt + prepared.length - 1;
|
|
2465
|
+
const materializations = [];
|
|
2466
|
+
for (const [index, entry] of prepared.entries()) {
|
|
2467
|
+
this.#assertEnqueueScope(session, entry.request.scopeId);
|
|
2468
|
+
materializations.push(await this.#materializeEnqueue(session.userId, entry.request, entry.replicaTransaction, undefined, firstCreatedAt + index));
|
|
2469
|
+
}
|
|
2470
|
+
this.#assertDistinctBatchFootprints(materializations);
|
|
2471
|
+
await this.#assertOutboxCapacity(session.userId, materializations.map((item) => item.command), undefined, currentCommands);
|
|
2472
|
+
options.assertCurrent?.();
|
|
2473
|
+
await this.#commitMaterializedEnqueues(materializations, generation, options);
|
|
2474
|
+
return materializations.map((item) => item.command.commandId);
|
|
2475
|
+
}
|
|
2476
|
+
async #beginEnqueueSession(generation) {
|
|
2431
2477
|
await this.initialize();
|
|
2432
2478
|
if (this.#options.mode === 'readCacheOnly') {
|
|
2433
2479
|
throw new Error('This offline provider is configured as a read-only cache and cannot enqueue commands.');
|
|
@@ -2436,12 +2482,20 @@ class OfflineSyncService {
|
|
|
2436
2482
|
if (!session)
|
|
2437
2483
|
throw new Error('Cannot enqueue an offline command without an authenticated user');
|
|
2438
2484
|
this.#assertSessionPrincipalBoundary(session);
|
|
2439
|
-
|
|
2440
|
-
this.#
|
|
2441
|
-
|
|
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}".`);
|
|
2485
|
+
this.#setActiveUser(session.userId);
|
|
2486
|
+
if (!this.#isCurrent(generation)) {
|
|
2487
|
+
throw new Error('Offline session changed before the command could be persisted');
|
|
2444
2488
|
}
|
|
2489
|
+
return session;
|
|
2490
|
+
}
|
|
2491
|
+
#assertEnqueueScope(session, scopeId) {
|
|
2492
|
+
if (!session.scopes.some((candidate) => candidate.userId === session.userId && candidate.scopeId === scopeId)) {
|
|
2493
|
+
throw new Error(`Offline sync session does not include scope "${scopeId}".`);
|
|
2494
|
+
}
|
|
2495
|
+
this.noteScope({ userId: session.userId, scopeId });
|
|
2496
|
+
}
|
|
2497
|
+
async #materializeEnqueue(userId, request, replicaTransaction, replaced, createdAt) {
|
|
2498
|
+
const scope = { userId, scopeId: request.scopeId };
|
|
2445
2499
|
this.noteScope(scope);
|
|
2446
2500
|
const commandIdentity = offlineCommandLookupIdentity(request.identity);
|
|
2447
2501
|
const normalized = await this.#normalizeEnqueueRequest(scope, request, commandIdentity);
|
|
@@ -2463,7 +2517,7 @@ class OfflineSyncService {
|
|
|
2463
2517
|
state: 'pending',
|
|
2464
2518
|
attempts: 0,
|
|
2465
2519
|
retryAt: null,
|
|
2466
|
-
createdAt: replaced?.createdAt ?? (await this.#nextCommandCreatedAt(userId)),
|
|
2520
|
+
createdAt: replaced?.createdAt ?? createdAt ?? (await this.#nextCommandCreatedAt(userId)),
|
|
2467
2521
|
lastErrorCode: null,
|
|
2468
2522
|
};
|
|
2469
2523
|
if (replaced &&
|
|
@@ -2543,20 +2597,44 @@ class OfflineSyncService {
|
|
|
2543
2597
|
this.#canonicalJson(replicaTransaction);
|
|
2544
2598
|
if (optimisticCompanions.length > 0)
|
|
2545
2599
|
command = { ...command, optimisticCompanions };
|
|
2546
|
-
|
|
2600
|
+
return { command, optimisticRow, optimisticCompanions };
|
|
2601
|
+
}
|
|
2602
|
+
#assertDistinctBatchFootprints(entries) {
|
|
2603
|
+
const aggregates = new Set();
|
|
2604
|
+
const replicaKeys = new Set();
|
|
2605
|
+
for (const entry of entries) {
|
|
2606
|
+
const aggregate = this.#aggregateKey(entry.command);
|
|
2607
|
+
if (aggregates.has(aggregate)) {
|
|
2608
|
+
throw new Error('Prepared offline batch contains overlapping aggregate intents.');
|
|
2609
|
+
}
|
|
2610
|
+
aggregates.add(aggregate);
|
|
2611
|
+
for (const key of [
|
|
2612
|
+
this.#replicaRowKey(entry.optimisticRow),
|
|
2613
|
+
...entry.optimisticCompanions.map((companion) => this.#replicaRowKey(companion.key)),
|
|
2614
|
+
]) {
|
|
2615
|
+
if (replicaKeys.has(key)) {
|
|
2616
|
+
throw new Error('Prepared offline batch contains overlapping replica footprints.');
|
|
2617
|
+
}
|
|
2618
|
+
replicaKeys.add(key);
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
async #commitMaterializedEnqueues(entries, generation, options, removeCommandIds) {
|
|
2547
2623
|
if (generation !== this.#generation) {
|
|
2548
2624
|
throw new Error('Offline session changed before the command could be persisted');
|
|
2549
2625
|
}
|
|
2550
2626
|
await this.#repository.transactReplica({
|
|
2551
|
-
putRows:
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2627
|
+
putRows: entries.flatMap((entry) => [
|
|
2628
|
+
entry.optimisticRow,
|
|
2629
|
+
...entry.optimisticCompanions.flatMap((companion) => (companion.after ? [companion.after] : [])),
|
|
2630
|
+
]),
|
|
2631
|
+
removeRows: entries.flatMap((entry) => entry.optimisticCompanions.flatMap((companion) => (companion.after ? [] : [companion.key]))),
|
|
2632
|
+
putCommands: entries.map((entry) => entry.command),
|
|
2633
|
+
removeCommandIds,
|
|
2555
2634
|
});
|
|
2556
|
-
await this.#refreshState();
|
|
2635
|
+
await this.#refreshState().catch((error) => this.#reportError(error));
|
|
2557
2636
|
if (options.flush !== false && this.#network.connected())
|
|
2558
2637
|
this.#flushInBackground();
|
|
2559
|
-
return commandId;
|
|
2560
2638
|
}
|
|
2561
2639
|
async #prepareOptimisticCompanions(scope, optimisticRow, transaction) {
|
|
2562
2640
|
if (!transaction)
|
|
@@ -2640,24 +2718,27 @@ class OfflineSyncService {
|
|
|
2640
2718
|
#replicaRowKey(key) {
|
|
2641
2719
|
return `${canonicalOfflinePrincipalId(key.userId)}:${key.scopeId}:${key.sourceKey}:${canonicalOfflineReplicaIdentity(key.identity)}`;
|
|
2642
2720
|
}
|
|
2643
|
-
async #assertOutboxCapacity(userId,
|
|
2644
|
-
const currentCommands = this.#
|
|
2645
|
-
? await this.#repository.getCommandsForUser(userId)
|
|
2646
|
-
: (await Promise.all([...this.#knownScopes.values()].filter((scope) => scope.userId === userId).map((scope) => this.#repository.getCommands(scope)))).flat();
|
|
2721
|
+
async #assertOutboxCapacity(userId, newCommands, excludingCommandId, knownCommands) {
|
|
2722
|
+
const currentCommands = knownCommands ?? (await this.#commandsForUser(userId));
|
|
2647
2723
|
const commands = excludingCommandId
|
|
2648
2724
|
? currentCommands.filter((candidate) => candidate.commandId !== excludingCommandId)
|
|
2649
2725
|
: currentCommands;
|
|
2650
2726
|
const maxCommands = this.#options.outboxLimits?.maxCommandsPerUser ?? DEFAULT_MAX_OUTBOX_COMMANDS_PER_USER;
|
|
2651
2727
|
const maxBytes = this.#options.outboxLimits?.maxBytesPerUser ?? DEFAULT_MAX_OUTBOX_BYTES_PER_USER;
|
|
2652
2728
|
const currentBytes = this.#serializedOutboxBytes(commands);
|
|
2653
|
-
if (commands.length
|
|
2729
|
+
if (commands.length + newCommands.length > maxCommands) {
|
|
2654
2730
|
throw new OfflineOutboxCapacityError('command_count', commands.length, currentBytes);
|
|
2655
2731
|
}
|
|
2656
|
-
const nextBytes =
|
|
2732
|
+
const nextBytes = this.#serializedOutboxBytes([...commands, ...newCommands]);
|
|
2657
2733
|
if (nextBytes > maxBytes) {
|
|
2658
2734
|
throw new OfflineOutboxCapacityError('serialized_bytes', commands.length, currentBytes);
|
|
2659
2735
|
}
|
|
2660
2736
|
}
|
|
2737
|
+
async #commandsForUser(userId) {
|
|
2738
|
+
return this.#repository.getCommandsForUser
|
|
2739
|
+
? this.#repository.getCommandsForUser(userId)
|
|
2740
|
+
: (await Promise.all([...this.#knownScopes.values()].filter((scope) => scope.userId === userId).map((scope) => this.#repository.getCommands(scope)))).flat();
|
|
2741
|
+
}
|
|
2661
2742
|
#serializedOutboxBytes(commands) {
|
|
2662
2743
|
return new TextEncoder().encode(JSON.stringify(commands)).byteLength;
|
|
2663
2744
|
}
|
|
@@ -3342,9 +3423,7 @@ class OfflineSyncService {
|
|
|
3342
3423
|
return commands;
|
|
3343
3424
|
}
|
|
3344
3425
|
async #nextCommandCreatedAt(userId) {
|
|
3345
|
-
const commands = this.#
|
|
3346
|
-
? await this.#repository.getCommandsForUser(userId)
|
|
3347
|
-
: await this.#readKnownCommands();
|
|
3426
|
+
const commands = await this.#commandsForUser(userId);
|
|
3348
3427
|
this.#rememberCreatedAt(commands);
|
|
3349
3428
|
const createdAt = Math.max(Date.now(), this.#lastCommandCreatedAt + 1);
|
|
3350
3429
|
this.#lastCommandCreatedAt = createdAt;
|