@rdlabo/ionic-angular-kit 21.5.24 → 21.5.25

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.
@@ -2134,10 +2134,14 @@ class OfflineSyncService {
2134
2134
  #knownScopes;
2135
2135
  #activeUserId;
2136
2136
  #flushPromise;
2137
+ #partialFlushInFlight;
2138
+ #chainedFullFlush;
2137
2139
  #flushTransitions;
2138
2140
  #generation;
2139
2141
  #sendingTransitions;
2140
2142
  #retryTimer;
2143
+ /** When non-null, automatic flushes pull only foreground scopes plus Outbox scopes. */
2144
+ #foregroundScopePolicy;
2141
2145
  #initialized;
2142
2146
  #lastCommandCreatedAt;
2143
2147
  constructor() {
@@ -2154,10 +2158,14 @@ class OfflineSyncService {
2154
2158
  this.#knownScopes = new Map();
2155
2159
  this.#activeUserId = null;
2156
2160
  this.#flushPromise = null;
2161
+ this.#partialFlushInFlight = false;
2162
+ this.#chainedFullFlush = null;
2157
2163
  this.#flushTransitions = new Set();
2158
2164
  this.#generation = 0;
2159
2165
  this.#sendingTransitions = new Set();
2160
2166
  this.#retryTimer = null;
2167
+ /** When non-null, automatic flushes pull only foreground scopes plus Outbox scopes. */
2168
+ this.#foregroundScopePolicy = null;
2161
2169
  this.#initialized = false;
2162
2170
  this.#lastCommandCreatedAt = 0;
2163
2171
  this.pendingCommands = this.#commands.asReadonly();
@@ -2177,7 +2185,7 @@ class OfflineSyncService {
2177
2185
  this.#flushInBackground();
2178
2186
  });
2179
2187
  }
2180
- async initialize() {
2188
+ async initialize(options = {}) {
2181
2189
  if (this.#initialized)
2182
2190
  return;
2183
2191
  await this.#repository.initialize();
@@ -2188,7 +2196,7 @@ class OfflineSyncService {
2188
2196
  .map((command) => this.#repository.putCommand({ ...command, state: 'pending' })));
2189
2197
  this.#initialized = true;
2190
2198
  await this.#refreshState();
2191
- if (this.#network.connected())
2199
+ if (options.flush !== false && this.#network.connected())
2192
2200
  this.#flushInBackground();
2193
2201
  }
2194
2202
  noteScope(scope) {
@@ -2198,8 +2206,9 @@ class OfflineSyncService {
2198
2206
  await this.initialize();
2199
2207
  await this.#refreshState();
2200
2208
  }
2201
- async refreshSession() {
2202
- await this.initialize();
2209
+ async refreshSession(foregroundScopeIds) {
2210
+ this.#setForegroundScopePolicy(foregroundScopeIds);
2211
+ await this.initialize({ flush: false });
2203
2212
  await this.#discoverScopes();
2204
2213
  await this.#restoreInterruptedCommands();
2205
2214
  await this.#refreshState();
@@ -2226,6 +2235,7 @@ class OfflineSyncService {
2226
2235
  /** Synchronously invalidate in-flight enqueue and transport work owned by the current session. */
2227
2236
  revokeSession() {
2228
2237
  this.#invalidateFlush();
2238
+ this.#foregroundScopePolicy = null;
2229
2239
  }
2230
2240
  enqueue(request, options = {}) {
2231
2241
  const generation = this.#generation;
@@ -2524,22 +2534,49 @@ class OfflineSyncService {
2524
2534
  await this.#refreshState();
2525
2535
  }
2526
2536
  #flushInBackground() {
2527
- void this.flush().catch((error) => this.#errorHandler.handleError(error));
2537
+ void this.#beginFlush(false).catch((error) => this.#errorHandler.handleError(error));
2528
2538
  }
2529
2539
  flush() {
2530
- if (this.#flushPromise)
2540
+ return this.#beginFlush(true);
2541
+ }
2542
+ #beginFlush(explicitFull) {
2543
+ const isPartial = !explicitFull && this.#foregroundScopePolicy !== null;
2544
+ if (this.#flushPromise) {
2545
+ if (explicitFull && this.#partialFlushInFlight) {
2546
+ if (!this.#chainedFullFlush) {
2547
+ const generation = this.#generation;
2548
+ const partial = this.#flushPromise;
2549
+ const chain = partial
2550
+ .then(() => undefined, () => undefined)
2551
+ .then(() => {
2552
+ if (!this.#isCurrent(generation))
2553
+ return;
2554
+ return this.#beginFlush(true);
2555
+ })
2556
+ .finally(() => {
2557
+ if (this.#chainedFullFlush === chain)
2558
+ this.#chainedFullFlush = null;
2559
+ });
2560
+ this.#chainedFullFlush = chain;
2561
+ }
2562
+ return this.#chainedFullFlush;
2563
+ }
2531
2564
  return this.#flushPromise;
2565
+ }
2532
2566
  const generation = this.#generation;
2533
- const promise = this.#runFlush(generation).finally(() => {
2567
+ const promise = this.#runFlush(generation, explicitFull).finally(() => {
2534
2568
  this.#flushTransitions.delete(promise);
2535
- if (this.#flushPromise === promise)
2569
+ if (this.#flushPromise === promise) {
2536
2570
  this.#flushPromise = null;
2571
+ this.#partialFlushInFlight = false;
2572
+ }
2537
2573
  });
2538
2574
  this.#flushPromise = promise;
2575
+ this.#partialFlushInFlight = isPartial;
2539
2576
  this.#flushTransitions.add(promise);
2540
2577
  return promise;
2541
2578
  }
2542
- async #runFlush(generation) {
2579
+ async #runFlush(generation, explicitFull) {
2543
2580
  if (!this.#isCurrent(generation))
2544
2581
  return;
2545
2582
  if (!this.#network.connected()) {
@@ -2548,7 +2585,11 @@ class OfflineSyncService {
2548
2585
  }
2549
2586
  if (!(await this.#discoverScopes(generation)))
2550
2587
  return;
2551
- for (const scope of this.#knownScopes.values()) {
2588
+ const isPartial = !explicitFull && this.#foregroundScopePolicy !== null;
2589
+ const pullScopes = isPartial
2590
+ ? this.#scopesForPartialPull(this.#foregroundScopePolicy, await this.#readKnownCommands())
2591
+ : [...this.#knownScopes.values()];
2592
+ for (const scope of pullScopes) {
2552
2593
  if (!this.#isCurrent(generation) || !this.#network.connected())
2553
2594
  return;
2554
2595
  await this.#pull.pull(scope);
@@ -2593,6 +2634,14 @@ class OfflineSyncService {
2593
2634
  throw postPullFailures[0];
2594
2635
  }
2595
2636
  }
2637
+ #setForegroundScopePolicy(foregroundScopeIds) {
2638
+ this.#foregroundScopePolicy = foregroundScopeIds !== undefined ? foregroundScopeIds : null;
2639
+ }
2640
+ #scopesForPartialPull(foregroundScopeIds, commands) {
2641
+ const foregroundScopeSet = new Set(foregroundScopeIds);
2642
+ const outboxScopeKeys = new Set(commands.map((command) => this.#scopeKey({ userId: command.userId, scopeId: command.scopeId })));
2643
+ return [...this.#knownScopes.values()].filter((scope) => foregroundScopeSet.has(scope.scopeId) || outboxScopeKeys.has(this.#scopeKey(scope)));
2644
+ }
2596
2645
  #eligibleAggregateGroups(commands) {
2597
2646
  const now = Date.now();
2598
2647
  const groups = new Map();
@@ -3056,6 +3105,8 @@ class OfflineSyncService {
3056
3105
  #invalidateFlush() {
3057
3106
  this.#generation += 1;
3058
3107
  this.#flushPromise = null;
3108
+ this.#partialFlushInFlight = false;
3109
+ this.#chainedFullFlush = null;
3059
3110
  this.#scheduleRetry(null);
3060
3111
  }
3061
3112
  #isCurrent(generation) {
@@ -3158,8 +3209,8 @@ class OfflineCoordinatorService {
3158
3209
  });
3159
3210
  }
3160
3211
  /** Starts pull and outbox replay after the caller has published remote access. */
3161
- async resumeRemoteSession() {
3162
- await this.#sync.refreshSession();
3212
+ async resumeRemoteSession(options) {
3213
+ await this.#sync.refreshSession(options?.foregroundScopeIds);
3163
3214
  }
3164
3215
  /**
3165
3216
  * Activates a restored identity for local replica/outbox use without enabling transport sync.
@@ -3273,7 +3324,7 @@ function createOfflineAuthBridge(options) {
3273
3324
  const resumeStillCurrent = () => (resumeLease?.isCurrent() ?? true) && currentAuthSubject() === identity.authSubject && (isIdentityCurrent?.(identity) ?? true);
3274
3325
  if (!resumeStillCurrent())
3275
3326
  return;
3276
- await offline.resumeRemoteSession();
3327
+ await offline.resumeRemoteSession(identity.foregroundScopeIds !== undefined ? { foregroundScopeIds: identity.foregroundScopeIds } : undefined);
3277
3328
  if (!resumeStillCurrent())
3278
3329
  return;
3279
3330
  if (onRemoteResumed) {
@@ -3315,6 +3366,18 @@ function assertOfflineRemoteIdentity(identity) {
3315
3366
  if (typeof identity.authSubject !== 'string' || identity.authSubject.length === 0) {
3316
3367
  throw new Error('Offline remote identity authSubject must be a non-empty string.');
3317
3368
  }
3369
+ if (identity.foregroundScopeIds !== undefined) {
3370
+ if (!Array.isArray(identity.foregroundScopeIds) ||
3371
+ identity.foregroundScopeIds.some((scopeId) => typeof scopeId !== 'string' || scopeId.length === 0)) {
3372
+ throw new Error('Offline remote identity foregroundScopeIds must contain only non-empty strings.');
3373
+ }
3374
+ const scopeIds = new Set(identity.scopeIds);
3375
+ for (const scopeId of identity.foregroundScopeIds) {
3376
+ if (!scopeIds.has(scopeId)) {
3377
+ throw new Error(`Offline remote identity foregroundScopeIds must be a subset of scopeIds: "${scopeId}".`);
3378
+ }
3379
+ }
3380
+ }
3318
3381
  }
3319
3382
 
3320
3383
  /**