@rdlabo/ionic-angular-kit 21.6.7 → 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
  }
@@ -6310,7 +6323,7 @@ const offlineInterceptor = (request, next) => {
6310
6323
  if (!plan)
6311
6324
  return transport();
6312
6325
  if (plan.readStrategy === 'local-first') {
6313
- return readLocalFirst(request, plan, transport, fallback, inject(ErrorHandler));
6326
+ return readLocalFirst(request, plan, transport, fallback, inject(ErrorHandler), inject(OfflineReplicaMutationCoordinator));
6314
6327
  }
6315
6328
  return readNetworkFirst(request, plan, transport, fallback);
6316
6329
  }
@@ -6340,16 +6353,36 @@ function readNetworkFirst(request, plan, transport, fallback) {
6340
6353
  * `firstValueFrom` and `take(1)` cancel in-flight transport and suppress
6341
6354
  * further emissions.
6342
6355
  */
6343
- function readLocalFirst(request, plan, transport, fallback, errorHandler) {
6344
- 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
6345
6358
  ? concat(of(localResponse), drainRemoteAfterLocal(bufferedTransport$, plan))
6346
6359
  : drainRemoteNetworkFirst(bufferedTransport$, request, plan, fallback))), { connector: () => new ReplaySubject() }));
6347
6360
  }
6348
- function resolveLocalAttempt(plan, errorHandler) {
6349
- return defer(() => from(plan.readLocal()).pipe(catchError((localError) => {
6361
+ function resolveLocalAttempt(plan, errorHandler, replicaMutations) {
6362
+ return serializedLocalRead(plan, replicaMutations).pipe(catchError((localError) => {
6350
6363
  errorHandler.handleError(localError);
6351
6364
  return of(null);
6352
- }))).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
+ });
6353
6386
  }
6354
6387
  function tryProjectLocal(cached, plan, errorHandler) {
6355
6388
  return emitTaggedLocalResponse(cached, plan).pipe(catchError((error) => {
@@ -6393,13 +6426,14 @@ function observeTransport(source, network) {
6393
6426
  class OfflineRequestFallbackService {
6394
6427
  #registry = inject(OfflineRequestPolicyRegistry);
6395
6428
  #errorHandler = inject(ErrorHandler);
6429
+ #replicaMutations = inject(OfflineReplicaMutationCoordinator);
6396
6430
  handle(request, error, resolvedPlan) {
6397
6431
  if (request.context.get(OFFLINE_BYPASS) || request.method !== 'GET' || !isOfflineFallbackError(error))
6398
6432
  return null;
6399
6433
  const plan = resolvedPlan ?? this.#registry.resolve(request);
6400
6434
  if (!plan || plan.kind !== 'read')
6401
6435
  return null;
6402
- return defer(() => from(plan.readLocal())).pipe(catchError((localError) => {
6436
+ return serializedLocalRead(plan, this.#replicaMutations).pipe(catchError((localError) => {
6403
6437
  this.#errorHandler.handleError(localError);
6404
6438
  return throwError(() => error);
6405
6439
  }), concatMap((cached) => cached ? of(cached.clone({ headers: cached.headers.set(OFFLINE_RESPONSE_HEADER, 'local') })) : throwError(() => error)));