@rdlabo/ionic-angular-kit 21.6.6 → 21.6.8

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.
@@ -7,7 +7,7 @@ import { App } from '@capacitor/app';
7
7
  import { Network } from '@capacitor/network';
8
8
  import { Capacitor } from '@capacitor/core';
9
9
  import { HttpErrorResponse, HttpContextToken, HttpResponse } from '@angular/common/http';
10
- import { defer, from, map as map$1, catchError, throwError, concatMap, materialize, connect, concat, of, ReplaySubject, take, dematerialize, EMPTY, tap } from 'rxjs';
10
+ import { defer, from, map as map$1, catchError, throwError, concatMap, materialize, connect, concat, of, ReplaySubject, take, Observable, dematerialize, EMPTY, tap } from 'rxjs';
11
11
 
12
12
  const MAX_OFFLINE_NATURAL_KEY_TEXT_BYTES = 1024;
13
13
  const IDENTIFIER_PATTERN = /^[a-z][a-z0-9_]*$/;
@@ -1751,7 +1751,7 @@ function compareUtf8Binary(left, right) {
1751
1751
 
1752
1752
  const settledMutation = async () => undefined;
1753
1753
  /**
1754
- * Serializes only local replica read/derive/write critical sections. Network
1754
+ * Serializes local replica reads and read/derive/write critical sections. Network
1755
1755
  * transport must stay outside this coordinator so synchronization never holds
1756
1756
  * the local mutation lane while waiting on I/O.
1757
1757
  *
@@ -1766,16 +1766,29 @@ class OfflineReplicaMutationCoordinator {
1766
1766
  #tail = settledMutation();
1767
1767
  /** Enqueues one local replica critical section behind any in-flight mutation. */
1768
1768
  run(operation) {
1769
- const mutation = this.#tail.then(() => {
1769
+ return this.#enqueue(() => {
1770
1770
  if (!this.#repository)
1771
1771
  throw new Error('Offline repository is not configured.');
1772
1772
  const atomicMutation = this.#repository?.[OFFLINE_REPOSITORY_ATOMIC_MUTATION];
1773
1773
  return atomicMutation ? atomicMutation.call(this.#repository, operation) : operation(this.#repository);
1774
1774
  });
1775
- this.#tail = mutation.then(() => undefined, () => undefined);
1776
- return mutation;
1777
1775
  }
1778
- /** Resolves after every currently queued replica mutation has settled. */
1776
+ /**
1777
+ * Enqueues an external local-replica read behind mutations and keeps later
1778
+ * mutations behind that read until it settles.
1779
+ *
1780
+ * Do not invoke this from inside {@link run}; use the repository passed to
1781
+ * that callback for reads owned by an atomic mutation.
1782
+ */
1783
+ runSerializedRead(read) {
1784
+ return this.#enqueue(read);
1785
+ }
1786
+ #enqueue(operation) {
1787
+ const queued = this.#tail.then(operation);
1788
+ this.#tail = queued.then(() => undefined, () => undefined);
1789
+ return queued;
1790
+ }
1791
+ /** Resolves after every currently queued replica read or mutation has settled. */
1779
1792
  async drain() {
1780
1793
  await this.#tail;
1781
1794
  }
@@ -4070,8 +4083,11 @@ class OfflineSyncService {
4070
4083
  }
4071
4084
  }
4072
4085
  async #readKnownCommands() {
4073
- const commands = (await Promise.all([...this.#knownScopes.values()].map((scope) => this.#repository.getCommands(scope))))
4074
- .flat()
4086
+ const userId = this.#activeUserId;
4087
+ if (userId === null)
4088
+ return [];
4089
+ const commands = (await this.#commandsForUser(userId))
4090
+ .filter((command) => this.#knownScopes.has(this.#scopeKey({ userId: command.userId, scopeId: command.scopeId })))
4075
4091
  .sort(compareOfflineCommands);
4076
4092
  this.#rememberCreatedAt(commands);
4077
4093
  return commands;
@@ -6307,7 +6323,7 @@ const offlineInterceptor = (request, next) => {
6307
6323
  if (!plan)
6308
6324
  return transport();
6309
6325
  if (plan.readStrategy === 'local-first') {
6310
- return readLocalFirst(request, plan, transport, fallback, inject(ErrorHandler));
6326
+ return readLocalFirst(request, plan, transport, fallback, inject(ErrorHandler), inject(OfflineReplicaMutationCoordinator));
6311
6327
  }
6312
6328
  return readNetworkFirst(request, plan, transport, fallback);
6313
6329
  }
@@ -6337,16 +6353,36 @@ function readNetworkFirst(request, plan, transport, fallback) {
6337
6353
  * `firstValueFrom` and `take(1)` cancel in-flight transport and suppress
6338
6354
  * further emissions.
6339
6355
  */
6340
- function readLocalFirst(request, plan, transport, fallback, errorHandler) {
6341
- return defer(transport).pipe(materialize(), connect((bufferedTransport$) => resolveLocalAttempt(plan, errorHandler).pipe(concatMap((localResponse) => localResponse
6356
+ function readLocalFirst(request, plan, transport, fallback, errorHandler, replicaMutations) {
6357
+ return defer(transport).pipe(materialize(), connect((bufferedTransport$) => resolveLocalAttempt(plan, errorHandler, replicaMutations).pipe(concatMap((localResponse) => localResponse
6342
6358
  ? concat(of(localResponse), drainRemoteAfterLocal(bufferedTransport$, plan))
6343
6359
  : drainRemoteNetworkFirst(bufferedTransport$, request, plan, fallback))), { connector: () => new ReplaySubject() }));
6344
6360
  }
6345
- function resolveLocalAttempt(plan, errorHandler) {
6346
- return defer(() => from(plan.readLocal()).pipe(catchError((localError) => {
6361
+ function resolveLocalAttempt(plan, errorHandler, replicaMutations) {
6362
+ return serializedLocalRead(plan, replicaMutations).pipe(catchError((localError) => {
6347
6363
  errorHandler.handleError(localError);
6348
6364
  return of(null);
6349
- }))).pipe(concatMap((local) => (local ? tryProjectLocal(local, plan, errorHandler) : of(null))), take(1));
6365
+ }), concatMap((local) => (local ? tryProjectLocal(local, plan, errorHandler) : of(null))), take(1));
6366
+ }
6367
+ /** Skips a queued local read when its HTTP subscriber leaves before the lane opens. */
6368
+ function serializedLocalRead(plan, replicaMutations) {
6369
+ return new Observable((subscriber) => {
6370
+ let cancelled = false;
6371
+ void replicaMutations
6372
+ .runSerializedRead(async () => (cancelled ? null : plan.readLocal()))
6373
+ .then((response) => {
6374
+ if (cancelled)
6375
+ return;
6376
+ subscriber.next(response);
6377
+ subscriber.complete();
6378
+ }, (error) => {
6379
+ if (!cancelled)
6380
+ subscriber.error(error);
6381
+ });
6382
+ return () => {
6383
+ cancelled = true;
6384
+ };
6385
+ });
6350
6386
  }
6351
6387
  function tryProjectLocal(cached, plan, errorHandler) {
6352
6388
  return emitTaggedLocalResponse(cached, plan).pipe(catchError((error) => {
@@ -6390,13 +6426,14 @@ function observeTransport(source, network) {
6390
6426
  class OfflineRequestFallbackService {
6391
6427
  #registry = inject(OfflineRequestPolicyRegistry);
6392
6428
  #errorHandler = inject(ErrorHandler);
6429
+ #replicaMutations = inject(OfflineReplicaMutationCoordinator);
6393
6430
  handle(request, error, resolvedPlan) {
6394
6431
  if (request.context.get(OFFLINE_BYPASS) || request.method !== 'GET' || !isOfflineFallbackError(error))
6395
6432
  return null;
6396
6433
  const plan = resolvedPlan ?? this.#registry.resolve(request);
6397
6434
  if (!plan || plan.kind !== 'read')
6398
6435
  return null;
6399
- return defer(() => from(plan.readLocal())).pipe(catchError((localError) => {
6436
+ return serializedLocalRead(plan, this.#replicaMutations).pipe(catchError((localError) => {
6400
6437
  this.#errorHandler.handleError(localError);
6401
6438
  return throwError(() => error);
6402
6439
  }), concatMap((cached) => cached ? of(cached.clone({ headers: cached.headers.set(OFFLINE_RESPONSE_HEADER, 'local') })) : throwError(() => error)));