@rdlabo/ionic-angular-kit 21.6.5 → 21.6.6

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) {
@@ -4355,6 +4386,9 @@ class OfflineMutationPersistenceService {
4355
4386
  }
4356
4387
  async #disable(revision) {
4357
4388
  await this.#admission.close();
4389
+ if (revision !== this.#transitionRevision)
4390
+ return;
4391
+ await this.#sync.initialize({ flush: false });
4358
4392
  if (revision !== this.#transitionRevision)
4359
4393
  return;
4360
4394
  const pendingBeforeFlush = this.#sync.pendingCount();
@@ -4573,6 +4607,9 @@ class OfflineCoordinatorService {
4573
4607
  this.#session = inject(OfflineSessionService);
4574
4608
  this.#options = inject(OFFLINE_KIT_OPTIONS);
4575
4609
  this.#storageState = signal({ status: 'initializing' }, ...(ngDevMode ? [{ debugName: "#storageState" }] : /* istanbul ignore next */ []));
4610
+ this.#localInitialization = null;
4611
+ this.#syncInitialization = null;
4612
+ this.#runtimeInitialization = null;
4576
4613
  this.#transitionRevision = 0;
4577
4614
  this.#transitionTail = settledTransition();
4578
4615
  /**
@@ -4601,21 +4638,28 @@ class OfflineCoordinatorService {
4601
4638
  #session;
4602
4639
  #options;
4603
4640
  #storageState;
4641
+ #localInitialization;
4642
+ #syncInitialization;
4643
+ #runtimeInitialization;
4604
4644
  #transitionRevision;
4605
4645
  #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() {
4646
+ /** Opens local storage and restores its persisted session boundary without waiting for network discovery. */
4647
+ initializeLocal() {
4648
+ this.#localInitialization ??= this.#initializeLocal();
4649
+ return this.#localInitialization;
4650
+ }
4651
+ /** Opens the local substrate, starts network discovery, then initializes synchronization. */
4652
+ initialize() {
4653
+ this.#runtimeInitialization ??= this.#initializeRuntime();
4654
+ return this.#runtimeInitialization;
4655
+ }
4656
+ async #initializeLocal() {
4614
4657
  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;
4658
+ const initializeSubstrate = async () => {
4659
+ await this.#repository.initialize();
4660
+ await this.#session.initialize();
4661
+ };
4662
+ const substrateReady = await initializeSubstrate().then(() => true, async (error) => {
4619
4663
  const typed = this.#asStorageUnavailable(error);
4620
4664
  this.#storageState.set({ status: 'unavailable', error: typed });
4621
4665
  const onUnavailable = this.#options.onStorageUnavailable;
@@ -4624,12 +4668,23 @@ class OfflineCoordinatorService {
4624
4668
  await onUnavailable(typed);
4625
4669
  return false;
4626
4670
  });
4627
- if (!repositoryReady)
4671
+ if (!substrateReady)
4628
4672
  return;
4629
- await networkReady;
4630
4673
  this.#storageState.set({ status: 'ready' });
4631
- await this.#session.initialize();
4632
- await this.#sync.initialize();
4674
+ }
4675
+ async #initializeRuntime() {
4676
+ const networkReady = this.#network.initialize();
4677
+ await Promise.all([this.#initializeSync(), networkReady]);
4678
+ }
4679
+ #initializeSync() {
4680
+ this.#syncInitialization ??= this.#initializeSyncOnce();
4681
+ return this.#syncInitialization;
4682
+ }
4683
+ async #initializeSyncOnce() {
4684
+ await this.initializeLocal();
4685
+ if (this.#storageUnavailable())
4686
+ return;
4687
+ await this.#sync.initialize({ flush: false });
4633
4688
  }
4634
4689
  async activateSession(userId, scopeIds, authSubject) {
4635
4690
  if (!(await this.prepareRemoteSession(userId, scopeIds, authSubject)))
@@ -4638,10 +4693,13 @@ class OfflineCoordinatorService {
4638
4693
  }
4639
4694
  /** Installs a remotely verified identity without starting pull or outbox replay. */
4640
4695
  async prepareRemoteSession(userId, scopeIds, authSubject, authLease) {
4641
- if (this.#storageUnavailable())
4642
- return true;
4643
4696
  const revision = ++this.#transitionRevision;
4644
4697
  const lease = this.#lease(revision, authLease);
4698
+ await this.initialize();
4699
+ if (this.#storageUnavailable())
4700
+ return true;
4701
+ if (!lease.isCurrent())
4702
+ return false;
4645
4703
  return this.#enqueueTransition(async () => {
4646
4704
  await this.#sync.resetSession();
4647
4705
  await this.#session.suspendRemoteSession();
@@ -4652,6 +4710,7 @@ class OfflineCoordinatorService {
4652
4710
  }
4653
4711
  /** Starts pull and outbox replay after the caller has published remote access. */
4654
4712
  async resumeRemoteSession(options) {
4713
+ await this.initialize();
4655
4714
  if (this.#storageUnavailable())
4656
4715
  return;
4657
4716
  await this.#sync.refreshSession(options?.foregroundScopeIds);
@@ -4660,10 +4719,13 @@ class OfflineCoordinatorService {
4660
4719
  * Activates a restored identity for local replica/outbox use without enabling transport sync.
4661
4720
  */
4662
4721
  async activateOfflineSession(authSubject, authLease) {
4663
- if (this.#storageUnavailable())
4664
- return null;
4665
4722
  const revision = ++this.#transitionRevision;
4666
4723
  const lease = this.#lease(revision, authLease);
4724
+ await this.#initializeSync();
4725
+ if (this.#storageUnavailable())
4726
+ return null;
4727
+ if (!lease.isCurrent())
4728
+ return null;
4667
4729
  return this.#enqueueTransition(async () => {
4668
4730
  await this.#sync.resetSession();
4669
4731
  if (!lease.isCurrent())
@@ -4675,17 +4737,19 @@ class OfflineCoordinatorService {
4675
4737
  });
4676
4738
  }
4677
4739
  async clearActiveSession() {
4678
- if (this.#storageUnavailable())
4679
- return;
4680
4740
  this.#sync.revokeSession();
4681
4741
  this.#session.revokeAccess();
4682
4742
  ++this.#transitionRevision;
4743
+ await this.#initializeSync();
4744
+ if (this.#storageUnavailable())
4745
+ return;
4683
4746
  return this.#enqueueTransition(async () => {
4684
4747
  await this.#sync.resetSession();
4685
4748
  await this.#session.clearActiveSession();
4686
4749
  });
4687
4750
  }
4688
4751
  async prepareLogout(action) {
4752
+ await this.initialize();
4689
4753
  if (this.#storageUnavailable())
4690
4754
  return action !== 'cancel';
4691
4755
  if (action === 'cancel')
@@ -4698,6 +4762,7 @@ class OfflineCoordinatorService {
4698
4762
  return this.#sync.pendingCount() === 0;
4699
4763
  }
4700
4764
  async flush() {
4765
+ await this.initialize();
4701
4766
  if (this.#storageUnavailable())
4702
4767
  return;
4703
4768
  return this.#sync.flush();
@@ -6203,7 +6268,21 @@ function provideOffline(options) {
6203
6268
  ...(options.providers ?? []),
6204
6269
  provideAppInitializer(() => {
6205
6270
  assertSupportedOfflineMode(Capacitor.getPlatform(), options.mode ?? 'synchronized');
6206
- return inject(OfflineCoordinatorService).initialize();
6271
+ const coordinator = inject(OfflineCoordinatorService);
6272
+ const errorHandler = inject(ErrorHandler);
6273
+ const localInitialization = coordinator.initializeLocal();
6274
+ void coordinator.initialize().catch(async (error) => {
6275
+ const localSucceeded = await localInitialization.then(() => true, () => false);
6276
+ if (!localSucceeded)
6277
+ return;
6278
+ try {
6279
+ errorHandler.handleError(error);
6280
+ }
6281
+ catch {
6282
+ // Error reporting must not create an unhandled background rejection.
6283
+ }
6284
+ });
6285
+ return localInitialization;
6207
6286
  }),
6208
6287
  ]);
6209
6288
  }