@rdlabo/ionic-angular-kit 21.6.5 → 21.6.7

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.
@@ -2747,6 +2747,7 @@ class OfflineSyncService {
2747
2747
  #retryTimer;
2748
2748
  /** When non-null, automatic flushes pull only foreground scopes plus Outbox scopes. */
2749
2749
  #foregroundScopePolicy;
2750
+ #initialization;
2750
2751
  #initialized;
2751
2752
  #lastCommandCreatedAt;
2752
2753
  constructor() {
@@ -2776,6 +2777,7 @@ class OfflineSyncService {
2776
2777
  this.#retryTimer = null;
2777
2778
  /** When non-null, automatic flushes pull only foreground scopes plus Outbox scopes. */
2778
2779
  this.#foregroundScopePolicy = null;
2780
+ this.#initialization = null;
2779
2781
  this.#initialized = false;
2780
2782
  this.#lastCommandCreatedAt = 0;
2781
2783
  this.pendingCommands = this.#commands.asReadonly();
@@ -2807,8 +2809,21 @@ class OfflineSyncService {
2807
2809
  });
2808
2810
  }
2809
2811
  async initialize(options = {}) {
2810
- if (this.#initialized)
2812
+ const generation = this.#generation;
2813
+ if (!(await this.#restoreCurrentGeneration(generation)))
2811
2814
  return;
2815
+ if (options.flush !== false && this.#network.connected())
2816
+ this.#flushInBackground();
2817
+ }
2818
+ #ensureInitialized() {
2819
+ this.#initialization ??= this.#initializeOnce();
2820
+ return this.#initialization;
2821
+ }
2822
+ async #restoreCurrentGeneration(generation) {
2823
+ await this.#ensureInitialized();
2824
+ return this.#isCurrent(generation);
2825
+ }
2826
+ async #initializeOnce() {
2812
2827
  await this.#repository.initialize();
2813
2828
  await this.#discoverScopes();
2814
2829
  const commands = await this.#readKnownCommands();
@@ -2821,19 +2836,21 @@ class OfflineSyncService {
2821
2836
  })));
2822
2837
  this.#initialized = true;
2823
2838
  await this.#refreshState();
2824
- if (options.flush !== false && this.#network.connected())
2825
- this.#flushInBackground();
2826
2839
  }
2827
2840
  noteScope(scope) {
2828
2841
  this.#knownScopes.set(this.#scopeKey(scope), scope);
2829
2842
  }
2830
2843
  async reloadPendingCommands() {
2831
- await this.initialize();
2844
+ const generation = this.#generation;
2845
+ if (!(await this.#restoreCurrentGeneration(generation)))
2846
+ return;
2832
2847
  await this.#refreshState();
2833
2848
  }
2834
2849
  async refreshSession(foregroundScopeIds) {
2850
+ const generation = this.#generation;
2851
+ if (!(await this.#restoreCurrentGeneration(generation)))
2852
+ return;
2835
2853
  this.#setForegroundScopePolicy(foregroundScopeIds);
2836
- await this.initialize({ flush: false });
2837
2854
  await this.#discoverScopes();
2838
2855
  await this.#restoreInterruptedCommands();
2839
2856
  await this.#refreshState();
@@ -2842,7 +2859,9 @@ class OfflineSyncService {
2842
2859
  }
2843
2860
  /** Restores local outbox visibility without enabling pull or replay transport. */
2844
2861
  async refreshLocalSession() {
2845
- await this.initialize();
2862
+ const generation = this.#generation;
2863
+ if (!(await this.#restoreCurrentGeneration(generation)))
2864
+ return;
2846
2865
  await this.#discoverLocalScopes();
2847
2866
  await this.#restoreInterruptedCommands();
2848
2867
  await this.#refreshState();
@@ -2878,7 +2897,7 @@ class OfflineSyncService {
2878
2897
  return this.#mutationAdmission.run(() => {
2879
2898
  const generation = this.#generation;
2880
2899
  return this.#serializeReplicaMutation(async (repository) => {
2881
- await this.initialize();
2900
+ await this.#ensureInitialized();
2882
2901
  if (!this.#isCurrent(generation))
2883
2902
  throw new Error('Offline session changed before prepared enqueue.');
2884
2903
  const prepared = await prepare(repository);
@@ -2898,7 +2917,7 @@ class OfflineSyncService {
2898
2917
  return this.#mutationAdmission.run(() => {
2899
2918
  const generation = this.#generation;
2900
2919
  return this.#serializeReplicaMutation(async (repository) => {
2901
- await this.initialize();
2920
+ await this.#ensureInitialized();
2902
2921
  if (!this.#isCurrent(generation))
2903
2922
  throw new Error('Offline session changed before prepared batch enqueue.');
2904
2923
  const prepared = await prepare(repository);
@@ -2916,7 +2935,7 @@ class OfflineSyncService {
2916
2935
  replacePrepared(commandId, prepare, options = {}) {
2917
2936
  const generation = this.#generation;
2918
2937
  return this.#serializeReplicaMutation(async (repository) => {
2919
- await this.initialize();
2938
+ await this.#ensureInitialized();
2920
2939
  if (!this.#isCurrent(generation))
2921
2940
  throw new Error('Offline session changed before prepared replacement.');
2922
2941
  const knownCommands = await this.#readKnownCommands();
@@ -2941,7 +2960,7 @@ class OfflineSyncService {
2941
2960
  replacePreparedAggregate(commandId, prepare, options = {}) {
2942
2961
  const generation = this.#generation;
2943
2962
  return this.#serializeReplicaMutation(async (repository) => {
2944
- await this.initialize();
2963
+ await this.#ensureInitialized();
2945
2964
  if (!this.#isCurrent(generation))
2946
2965
  throw new Error('Offline session changed before prepared aggregate replacement.');
2947
2966
  const knownCommands = await this.#readKnownCommands();
@@ -2987,7 +3006,7 @@ class OfflineSyncService {
2987
3006
  runSerializedReplicaMutation(operation) {
2988
3007
  const generation = this.#generation;
2989
3008
  return this.#serializeReplicaMutation(async (repository) => {
2990
- await this.initialize();
3009
+ await this.#ensureInitialized();
2991
3010
  if (!this.#isCurrent(generation))
2992
3011
  throw new Error('Offline session changed before serialized replica mutation.');
2993
3012
  const result = await operation(repository);
@@ -3031,7 +3050,7 @@ class OfflineSyncService {
3031
3050
  return materializations.map((item) => item.command.commandId);
3032
3051
  }
3033
3052
  async #beginEnqueueSession(generation) {
3034
- await this.initialize();
3053
+ await this.#ensureInitialized();
3035
3054
  if (this.#options.mode === 'readCacheOnly') {
3036
3055
  throw new Error('This offline provider is configured as a read-only cache and cannot enqueue commands.');
3037
3056
  }
@@ -3296,7 +3315,9 @@ class OfflineSyncService {
3296
3315
  return new TextEncoder().encode(JSON.stringify(commands)).byteLength;
3297
3316
  }
3298
3317
  async discard(commandId, options = {}) {
3299
- await this.initialize();
3318
+ const generation = this.#generation;
3319
+ if (!(await this.#restoreCurrentGeneration(generation)))
3320
+ return;
3300
3321
  // A pull may have completed while transport was being cancelled. Re-read
3301
3322
  // and project the discard inside the same local mutation lane used by
3302
3323
  // enqueue, ACK, and pull application so an old before-image cannot replace
@@ -3320,7 +3341,9 @@ class OfflineSyncService {
3320
3341
  }
3321
3342
  /** Clears a retry backoff/auth block selected explicitly by the user and sends the durable command now. */
3322
3343
  async retryNow(commandId) {
3323
- await this.initialize();
3344
+ const generation = this.#generation;
3345
+ if (!(await this.#restoreCurrentGeneration(generation)))
3346
+ return;
3324
3347
  this.#invalidateFlush();
3325
3348
  await this.#waitForSendingTransitions();
3326
3349
  await this.#restoreInterruptedCommands();
@@ -3346,7 +3369,9 @@ class OfflineSyncService {
3346
3369
  await this.flush();
3347
3370
  }
3348
3371
  async discardAllPending() {
3349
- await this.initialize();
3372
+ const generation = this.#generation;
3373
+ if (!(await this.#restoreCurrentGeneration(generation)))
3374
+ return;
3350
3375
  const commands = await this.#replicaMutations.run(async (repository) => {
3351
3376
  const current = await this.#readKnownCommands();
3352
3377
  this.#assertDiscardable(current);
@@ -3367,6 +3392,12 @@ class OfflineSyncService {
3367
3392
  void this.#beginFlush(false).catch((error) => this.#reportError(error));
3368
3393
  }
3369
3394
  flush() {
3395
+ const generation = this.#generation;
3396
+ return this.#flushAfterInitialization(generation);
3397
+ }
3398
+ async #flushAfterInitialization(generation) {
3399
+ if (!(await this.#restoreCurrentGeneration(generation)))
3400
+ return;
3370
3401
  return this.#beginFlush(true);
3371
3402
  }
3372
3403
  #beginFlush(explicitFull) {
@@ -4039,8 +4070,11 @@ class OfflineSyncService {
4039
4070
  }
4040
4071
  }
4041
4072
  async #readKnownCommands() {
4042
- const commands = (await Promise.all([...this.#knownScopes.values()].map((scope) => this.#repository.getCommands(scope))))
4043
- .flat()
4073
+ const userId = this.#activeUserId;
4074
+ if (userId === null)
4075
+ return [];
4076
+ const commands = (await this.#commandsForUser(userId))
4077
+ .filter((command) => this.#knownScopes.has(this.#scopeKey({ userId: command.userId, scopeId: command.scopeId })))
4044
4078
  .sort(compareOfflineCommands);
4045
4079
  this.#rememberCreatedAt(commands);
4046
4080
  return commands;
@@ -4355,6 +4389,9 @@ class OfflineMutationPersistenceService {
4355
4389
  }
4356
4390
  async #disable(revision) {
4357
4391
  await this.#admission.close();
4392
+ if (revision !== this.#transitionRevision)
4393
+ return;
4394
+ await this.#sync.initialize({ flush: false });
4358
4395
  if (revision !== this.#transitionRevision)
4359
4396
  return;
4360
4397
  const pendingBeforeFlush = this.#sync.pendingCount();
@@ -4573,6 +4610,9 @@ class OfflineCoordinatorService {
4573
4610
  this.#session = inject(OfflineSessionService);
4574
4611
  this.#options = inject(OFFLINE_KIT_OPTIONS);
4575
4612
  this.#storageState = signal({ status: 'initializing' }, ...(ngDevMode ? [{ debugName: "#storageState" }] : /* istanbul ignore next */ []));
4613
+ this.#localInitialization = null;
4614
+ this.#syncInitialization = null;
4615
+ this.#runtimeInitialization = null;
4576
4616
  this.#transitionRevision = 0;
4577
4617
  this.#transitionTail = settledTransition();
4578
4618
  /**
@@ -4601,21 +4641,28 @@ class OfflineCoordinatorService {
4601
4641
  #session;
4602
4642
  #options;
4603
4643
  #storageState;
4644
+ #localInitialization;
4645
+ #syncInitialization;
4646
+ #runtimeInitialization;
4604
4647
  #transitionRevision;
4605
4648
  #transitionTail;
4606
- /**
4607
- * Opens local storage, then session and sync.
4608
- *
4609
- * Repository failure without {@link OfflineKitOptions.onStorageUnavailable} throws
4610
- * {@link OfflineStorageUnavailableError}. When the callback is present and settles, this method
4611
- * resolves with {@link storageState} `unavailable` and skips session/sync initialization.
4612
- */
4613
- async initialize() {
4649
+ /** Opens local storage and restores its persisted session boundary without waiting for network discovery. */
4650
+ initializeLocal() {
4651
+ this.#localInitialization ??= this.#initializeLocal();
4652
+ return this.#localInitialization;
4653
+ }
4654
+ /** Opens the local substrate, starts network discovery, then initializes synchronization. */
4655
+ initialize() {
4656
+ this.#runtimeInitialization ??= this.#initializeRuntime();
4657
+ return this.#runtimeInitialization;
4658
+ }
4659
+ async #initializeLocal() {
4614
4660
  await this.#mutationPersistence.initialize();
4615
- const networkReady = this.#network.initialize();
4616
- const initializeRepository = async () => this.#repository.initialize();
4617
- const repositoryReady = await initializeRepository().then(() => true, async (error) => {
4618
- await networkReady;
4661
+ const initializeSubstrate = async () => {
4662
+ await this.#repository.initialize();
4663
+ await this.#session.initialize();
4664
+ };
4665
+ const substrateReady = await initializeSubstrate().then(() => true, async (error) => {
4619
4666
  const typed = this.#asStorageUnavailable(error);
4620
4667
  this.#storageState.set({ status: 'unavailable', error: typed });
4621
4668
  const onUnavailable = this.#options.onStorageUnavailable;
@@ -4624,12 +4671,23 @@ class OfflineCoordinatorService {
4624
4671
  await onUnavailable(typed);
4625
4672
  return false;
4626
4673
  });
4627
- if (!repositoryReady)
4674
+ if (!substrateReady)
4628
4675
  return;
4629
- await networkReady;
4630
4676
  this.#storageState.set({ status: 'ready' });
4631
- await this.#session.initialize();
4632
- await this.#sync.initialize();
4677
+ }
4678
+ async #initializeRuntime() {
4679
+ const networkReady = this.#network.initialize();
4680
+ await Promise.all([this.#initializeSync(), networkReady]);
4681
+ }
4682
+ #initializeSync() {
4683
+ this.#syncInitialization ??= this.#initializeSyncOnce();
4684
+ return this.#syncInitialization;
4685
+ }
4686
+ async #initializeSyncOnce() {
4687
+ await this.initializeLocal();
4688
+ if (this.#storageUnavailable())
4689
+ return;
4690
+ await this.#sync.initialize({ flush: false });
4633
4691
  }
4634
4692
  async activateSession(userId, scopeIds, authSubject) {
4635
4693
  if (!(await this.prepareRemoteSession(userId, scopeIds, authSubject)))
@@ -4638,10 +4696,13 @@ class OfflineCoordinatorService {
4638
4696
  }
4639
4697
  /** Installs a remotely verified identity without starting pull or outbox replay. */
4640
4698
  async prepareRemoteSession(userId, scopeIds, authSubject, authLease) {
4641
- if (this.#storageUnavailable())
4642
- return true;
4643
4699
  const revision = ++this.#transitionRevision;
4644
4700
  const lease = this.#lease(revision, authLease);
4701
+ await this.initialize();
4702
+ if (this.#storageUnavailable())
4703
+ return true;
4704
+ if (!lease.isCurrent())
4705
+ return false;
4645
4706
  return this.#enqueueTransition(async () => {
4646
4707
  await this.#sync.resetSession();
4647
4708
  await this.#session.suspendRemoteSession();
@@ -4652,6 +4713,7 @@ class OfflineCoordinatorService {
4652
4713
  }
4653
4714
  /** Starts pull and outbox replay after the caller has published remote access. */
4654
4715
  async resumeRemoteSession(options) {
4716
+ await this.initialize();
4655
4717
  if (this.#storageUnavailable())
4656
4718
  return;
4657
4719
  await this.#sync.refreshSession(options?.foregroundScopeIds);
@@ -4660,10 +4722,13 @@ class OfflineCoordinatorService {
4660
4722
  * Activates a restored identity for local replica/outbox use without enabling transport sync.
4661
4723
  */
4662
4724
  async activateOfflineSession(authSubject, authLease) {
4663
- if (this.#storageUnavailable())
4664
- return null;
4665
4725
  const revision = ++this.#transitionRevision;
4666
4726
  const lease = this.#lease(revision, authLease);
4727
+ await this.#initializeSync();
4728
+ if (this.#storageUnavailable())
4729
+ return null;
4730
+ if (!lease.isCurrent())
4731
+ return null;
4667
4732
  return this.#enqueueTransition(async () => {
4668
4733
  await this.#sync.resetSession();
4669
4734
  if (!lease.isCurrent())
@@ -4675,17 +4740,19 @@ class OfflineCoordinatorService {
4675
4740
  });
4676
4741
  }
4677
4742
  async clearActiveSession() {
4678
- if (this.#storageUnavailable())
4679
- return;
4680
4743
  this.#sync.revokeSession();
4681
4744
  this.#session.revokeAccess();
4682
4745
  ++this.#transitionRevision;
4746
+ await this.#initializeSync();
4747
+ if (this.#storageUnavailable())
4748
+ return;
4683
4749
  return this.#enqueueTransition(async () => {
4684
4750
  await this.#sync.resetSession();
4685
4751
  await this.#session.clearActiveSession();
4686
4752
  });
4687
4753
  }
4688
4754
  async prepareLogout(action) {
4755
+ await this.initialize();
4689
4756
  if (this.#storageUnavailable())
4690
4757
  return action !== 'cancel';
4691
4758
  if (action === 'cancel')
@@ -4698,6 +4765,7 @@ class OfflineCoordinatorService {
4698
4765
  return this.#sync.pendingCount() === 0;
4699
4766
  }
4700
4767
  async flush() {
4768
+ await this.initialize();
4701
4769
  if (this.#storageUnavailable())
4702
4770
  return;
4703
4771
  return this.#sync.flush();
@@ -6203,7 +6271,21 @@ function provideOffline(options) {
6203
6271
  ...(options.providers ?? []),
6204
6272
  provideAppInitializer(() => {
6205
6273
  assertSupportedOfflineMode(Capacitor.getPlatform(), options.mode ?? 'synchronized');
6206
- return inject(OfflineCoordinatorService).initialize();
6274
+ const coordinator = inject(OfflineCoordinatorService);
6275
+ const errorHandler = inject(ErrorHandler);
6276
+ const localInitialization = coordinator.initializeLocal();
6277
+ void coordinator.initialize().catch(async (error) => {
6278
+ const localSucceeded = await localInitialization.then(() => true, () => false);
6279
+ if (!localSucceeded)
6280
+ return;
6281
+ try {
6282
+ errorHandler.handleError(error);
6283
+ }
6284
+ catch {
6285
+ // Error reporting must not create an unhandled background rejection.
6286
+ }
6287
+ });
6288
+ return localInitialization;
6207
6289
  }),
6208
6290
  ]);
6209
6291
  }