@rdlabo/ionic-angular-kit 21.5.33 → 21.5.34

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/README.md CHANGED
@@ -398,7 +398,7 @@ declare dummy adapters.
398
398
  Mutations may still call `OfflineSyncService.enqueue` explicitly. A product that must keep ordinary HTTP services
399
399
  offline-unaware can instead register `mutationPolicies`; a matched `POST` / `PUT` / `PATCH` / `DELETE` is prepared
400
400
  locally before transport and returns an optimistic response while the Outbox owns remote replay.
401
- Web storage uses Ionic Storage; iOS and Android use encrypted `@capacitor-community/sqlite`. Importing either the
401
+ Web storage uses Ionic Storage and supports `readCacheOnly` only. `synchronized` fails fast on Web because the current repository has no cross-tab lock; iOS and Android use encrypted `@capacitor-community/sqlite`. Importing either the
402
402
  primary entry point or `/offline` does not pull the optional native SQLite plugin into web-only applications.
403
403
 
404
404
  For cold-start offline route access, `OfflineCoordinatorService.activateOfflineSession()` restores only a manifest
@@ -909,9 +909,13 @@ function canonicalOfflineReplicaRowKey(schema, row) {
909
909
  }
910
910
  /** DI token for the selected platform repository. */
911
911
  const OFFLINE_REPOSITORY = new InjectionToken('OFFLINE_REPOSITORY');
912
- /** Selects encrypted SQLite on native platforms and Ionic Storage on web. */
912
+ /** Returns whether the platform uses the cross-process-safe native repository. */
913
+ function supportsSynchronizedOfflineRepository(platform) {
914
+ return platform === 'ios' || platform === 'android';
915
+ }
916
+ /** Selects encrypted SQLite on native platforms and Ionic Storage elsewhere. */
913
917
  function selectOfflineRepository(platform, webRepository, nativeRepository) {
914
- return platform === 'ios' || platform === 'android' ? nativeRepository : webRepository;
918
+ return supportsSynchronizedOfflineRepository(platform) ? nativeRepository : webRepository;
915
919
  }
916
920
  const METADATA_KEY = 'offline:metadata';
917
921
  const SESSION_MANIFESTS_KEY = 'offline:session:manifests';
@@ -2564,6 +2568,44 @@ class OfflineSyncService {
2564
2568
  return this.#enqueue(prepared.request, options, generation, prepared.replicaTransaction, replaced);
2565
2569
  });
2566
2570
  }
2571
+ /**
2572
+ * Atomically rematerializes every unresolved intent for one aggregate.
2573
+ *
2574
+ * This is the conflict-recovery boundary for ordered intent chains: the old
2575
+ * chain remains durable until every replacement has been prepared and the
2576
+ * complete chain can be committed in one replica transaction.
2577
+ */
2578
+ replacePreparedAggregate(commandId, prepare, options = {}) {
2579
+ const generation = this.#generation;
2580
+ return this.#serializeReplicaMutation(async () => {
2581
+ await this.initialize();
2582
+ if (!this.#isCurrent(generation))
2583
+ throw new Error('Offline session changed before prepared aggregate replacement.');
2584
+ const knownCommands = await this.#readKnownCommands();
2585
+ const selected = knownCommands.find((command) => command.commandId === commandId);
2586
+ if (!selected)
2587
+ throw new Error(`Offline command ${commandId} no longer exists.`);
2588
+ const aggregateKey = this.#aggregateKey(selected);
2589
+ const replaced = knownCommands.filter((command) => this.#aggregateKey(command) === aggregateKey);
2590
+ this.#assertDiscardable(replaced);
2591
+ const prepared = await prepare(this.#repository, replaced);
2592
+ if (prepared.length !== replaced.length) {
2593
+ throw new Error('Offline aggregate replacement must preserve the ordered intent count.');
2594
+ }
2595
+ const session = await this.#beginEnqueueSession(generation);
2596
+ const materializations = [];
2597
+ for (const [index, entry] of prepared.entries()) {
2598
+ this.#assertEnqueueScope(session, entry.request.scopeId);
2599
+ materializations.push(await this.#materializeEnqueue(session.userId, entry.request, entry.replicaTransaction, replaced[index]));
2600
+ }
2601
+ const retained = knownCommands.filter((command) => !replaced.some((item) => item.commandId === command.commandId));
2602
+ this.#assertDistinctBatchFootprints(materializations, retained, true);
2603
+ await this.#assertOutboxCapacity(session.userId, materializations.map((item) => item.command), replaced.map((command) => command.commandId), knownCommands);
2604
+ options.assertCurrent?.();
2605
+ await this.#commitMaterializedEnqueues(materializations, generation, options, replaced.map((command) => command.commandId));
2606
+ return materializations.map((item) => item.command.commandId);
2607
+ });
2608
+ }
2567
2609
  /**
2568
2610
  * Serializes a product-owned replica projection with enqueue and command ACK
2569
2611
  * reconciliation. For read/derive/write cache updates, prefer
@@ -2601,7 +2643,7 @@ class OfflineSyncService {
2601
2643
  const currentCommands = await this.#commandsForUser(session.userId);
2602
2644
  const retainedCommands = replaced ? currentCommands.filter((command) => command.commandId !== replaced.commandId) : currentCommands;
2603
2645
  this.#assertDistinctBatchFootprints([materialization], retainedCommands);
2604
- await this.#assertOutboxCapacity(session.userId, [materialization.command], replaced?.commandId, currentCommands);
2646
+ await this.#assertOutboxCapacity(session.userId, [materialization.command], replaced ? [replaced.commandId] : undefined, currentCommands);
2605
2647
  await this.#commitMaterializedEnqueues([materialization], generation, options, replaced ? [replaced.commandId] : undefined);
2606
2648
  return materialization.command.commandId;
2607
2649
  }
@@ -2751,7 +2793,7 @@ class OfflineSyncService {
2751
2793
  command = { ...command, optimisticCompanions };
2752
2794
  return { command, optimisticRow, optimisticCompanions };
2753
2795
  }
2754
- #assertDistinctBatchFootprints(entries, existingCommands) {
2796
+ #assertDistinctBatchFootprints(entries, existingCommands, allowOneAggregate = false) {
2755
2797
  const aggregates = new Set();
2756
2798
  const replicaKeys = new Set();
2757
2799
  const existingFootprints = new Map();
@@ -2762,7 +2804,7 @@ class OfflineSyncService {
2762
2804
  }
2763
2805
  for (const entry of entries) {
2764
2806
  const aggregate = this.#aggregateKey(entry.command);
2765
- if (aggregates.has(aggregate)) {
2807
+ if (aggregates.has(aggregate) && !allowOneAggregate) {
2766
2808
  throw new Error('Prepared offline batch contains overlapping aggregate intents.');
2767
2809
  }
2768
2810
  aggregates.add(aggregate);
@@ -2770,7 +2812,7 @@ class OfflineSyncService {
2770
2812
  this.#replicaRowKey(entry.optimisticRow),
2771
2813
  ...entry.optimisticCompanions.map((companion) => this.#replicaRowKey(companion.key)),
2772
2814
  ]) {
2773
- if (replicaKeys.has(key)) {
2815
+ if (replicaKeys.has(key) && !allowOneAggregate) {
2774
2816
  throw new Error('Prepared offline batch contains overlapping replica footprints.');
2775
2817
  }
2776
2818
  const existingAggregate = existingFootprints.get(key);
@@ -2889,10 +2931,10 @@ class OfflineSyncService {
2889
2931
  #replicaRowKey(key) {
2890
2932
  return canonicalOfflineReplicaRowKey(this.#entitySchema(key.sourceKey), key);
2891
2933
  }
2892
- async #assertOutboxCapacity(userId, newCommands, excludingCommandId, knownCommands) {
2934
+ async #assertOutboxCapacity(userId, newCommands, excludingCommandIds, knownCommands) {
2893
2935
  const currentCommands = knownCommands ?? (await this.#commandsForUser(userId));
2894
- const commands = excludingCommandId
2895
- ? currentCommands.filter((candidate) => candidate.commandId !== excludingCommandId)
2936
+ const commands = excludingCommandIds
2937
+ ? currentCommands.filter((candidate) => !excludingCommandIds.includes(candidate.commandId))
2896
2938
  : currentCommands;
2897
2939
  const maxCommands = this.#options.outboxLimits?.maxCommandsPerUser ?? DEFAULT_MAX_OUTBOX_COMMANDS_PER_USER;
2898
2940
  const maxBytes = this.#options.outboxLimits?.maxBytesPerUser ?? DEFAULT_MAX_OUTBOX_BYTES_PER_USER;
@@ -4971,9 +5013,18 @@ function provideOffline(options) {
4971
5013
  ...options.requestPolicies.flatMap((policy) => provideOfflineRequestPolicy(policy)),
4972
5014
  ...(options.mutationPolicies ?? []).flatMap((policy) => provideOfflineMutationRequestPolicy(policy)),
4973
5015
  ...(options.providers ?? []),
4974
- provideAppInitializer(() => inject(OfflineCoordinatorService).initialize()),
5016
+ provideAppInitializer(() => {
5017
+ assertSupportedOfflineMode(Capacitor.getPlatform(), options.mode ?? 'synchronized');
5018
+ return inject(OfflineCoordinatorService).initialize();
5019
+ }),
4975
5020
  ]);
4976
5021
  }
5022
+ /** Prevents unsupported multi-tab Web writes from silently losing Outbox state. */
5023
+ function assertSupportedOfflineMode(platform, mode) {
5024
+ if (!supportsSynchronizedOfflineRepository(platform) && mode === 'synchronized') {
5025
+ throw new Error('Offline synchronized mode is supported only by native repositories. Use readCacheOnly until the selected repository provides cross-context locking.');
5026
+ }
5027
+ }
4977
5028
 
4978
5029
  const LOCAL_FIRST_MUTATION_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
4979
5030
  /** Applies product read and local-first mutation policies while observing real API reachability. */
@@ -5095,5 +5146,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImpo
5095
5146
  * Generated bundle index. Do not edit.
5096
5147
  */
5097
5148
 
5098
- export { COMMUNITY_SQLITE, DEFAULT_OFFLINE_COMMAND_HOOKS, IonicOfflineRepository, OFFLINE_BYPASS, OFFLINE_COMMAND_EXECUTOR, OFFLINE_COMMAND_HOOKS, OFFLINE_KIT_OPTIONS, OFFLINE_MUTATION_REQUEST_POLICIES, OFFLINE_REPLICA_PROJECTOR, OFFLINE_REPLICA_PULLER, OFFLINE_REPOSITORY, OFFLINE_REQUEST_POLICIES, OFFLINE_RESPONSE_HEADER, OFFLINE_SCHEMA_VERSION, OFFLINE_SYNC_CONTEXT, OfflineCommandInFlightError, OfflineCoordinatorService, OfflineMutationPolicyConflictError, OfflineMutationProtocolError, OfflineMutationRequestPolicyRegistry, OfflineNetworkService, OfflineOutboxCapacityError, OfflinePayloadValidationError, OfflineReplicaMutationCoordinator, OfflineReplicaPullService, OfflineRequestFallbackService, OfflineRequestPolicyRegistry, OfflineSessionService, OfflineSyncService, SqliteOfflineRepository, assertOfflineLocalId, assertOfflineReplicaGeneratedRemoteId, assertOfflineReplicaNaturalKeyBaseline, booleanColumn, canonicalOfflineCommandIdentity, canonicalOfflinePrincipalId, canonicalOfflineRemoteIdentity, canonicalOfflineReplicaIdentity, canonicalOfflineReplicaRowKey, commandIdentityFromReplicaIdentity, commandIdentityMatchesReplicaRow, createCommunitySqliteDriver, createOfflineAuthBridge, createRandomOfflineEncryptionKey, datetime, decodeOfflineReplicaValues, defineOfflineReplicaSchema, defineReplicaEntity, encodeOfflineReplicaValues, generatedId, ignored, integer, isOfflineFallbackError, json, localOnly, naturalKey, normalizeOfflineNaturalKey, normalizeOfflineReplicaPullPage, nullable, offlineCommandLookupIdentity, offlineCommandTargetFromReplicaRow, offlineGeneratedReplicaIdentity, offlineInterceptor, offlineNaturalKeyFromValues, offlineNaturalReplicaIdentity, offlineReplicaRemoteIdentity, offlineSessionManifestAllows, parseOfflineCommandIdentity, parseOfflineMutationEnvelope, parseOfflinePrincipalId, projectOfflineReplicaValues, provideOffline, provideOfflineMutationRequestPolicy, provideOfflineRequestPolicy, real, replicaAddressFromIdentity, requirePositiveOfflineInteger, selectOfflineRepository, serializeOfflineCommandIdentity, sha256OfflineReplicaSchema, text };
5149
+ export { COMMUNITY_SQLITE, DEFAULT_OFFLINE_COMMAND_HOOKS, IonicOfflineRepository, OFFLINE_BYPASS, OFFLINE_COMMAND_EXECUTOR, OFFLINE_COMMAND_HOOKS, OFFLINE_KIT_OPTIONS, OFFLINE_MUTATION_REQUEST_POLICIES, OFFLINE_REPLICA_PROJECTOR, OFFLINE_REPLICA_PULLER, OFFLINE_REPOSITORY, OFFLINE_REQUEST_POLICIES, OFFLINE_RESPONSE_HEADER, OFFLINE_SCHEMA_VERSION, OFFLINE_SYNC_CONTEXT, OfflineCommandInFlightError, OfflineCoordinatorService, OfflineMutationPolicyConflictError, OfflineMutationProtocolError, OfflineMutationRequestPolicyRegistry, OfflineNetworkService, OfflineOutboxCapacityError, OfflinePayloadValidationError, OfflineReplicaMutationCoordinator, OfflineReplicaPullService, OfflineRequestFallbackService, OfflineRequestPolicyRegistry, OfflineSessionService, OfflineSyncService, SqliteOfflineRepository, assertOfflineLocalId, assertOfflineReplicaGeneratedRemoteId, assertOfflineReplicaNaturalKeyBaseline, assertSupportedOfflineMode, booleanColumn, canonicalOfflineCommandIdentity, canonicalOfflinePrincipalId, canonicalOfflineRemoteIdentity, canonicalOfflineReplicaIdentity, canonicalOfflineReplicaRowKey, commandIdentityFromReplicaIdentity, commandIdentityMatchesReplicaRow, createCommunitySqliteDriver, createOfflineAuthBridge, createRandomOfflineEncryptionKey, datetime, decodeOfflineReplicaValues, defineOfflineReplicaSchema, defineReplicaEntity, encodeOfflineReplicaValues, generatedId, ignored, integer, isOfflineFallbackError, json, localOnly, naturalKey, normalizeOfflineNaturalKey, normalizeOfflineReplicaPullPage, nullable, offlineCommandLookupIdentity, offlineCommandTargetFromReplicaRow, offlineGeneratedReplicaIdentity, offlineInterceptor, offlineNaturalKeyFromValues, offlineNaturalReplicaIdentity, offlineReplicaRemoteIdentity, offlineSessionManifestAllows, parseOfflineCommandIdentity, parseOfflineMutationEnvelope, parseOfflinePrincipalId, projectOfflineReplicaValues, provideOffline, provideOfflineMutationRequestPolicy, provideOfflineRequestPolicy, real, replicaAddressFromIdentity, requirePositiveOfflineInteger, selectOfflineRepository, serializeOfflineCommandIdentity, sha256OfflineReplicaSchema, supportsSynchronizedOfflineRepository, text };
5099
5150
  //# sourceMappingURL=rdlabo-ionic-angular-kit-offline.mjs.map