@rdlabo/ionic-angular-kit 21.5.26 → 21.5.27
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 { HttpErrorResponse, HttpContextToken, HttpResponse } from '@angular/common/http';
|
|
9
9
|
import { Capacitor } from '@capacitor/core';
|
|
10
|
-
import { defer, catchError, throwError, concatMap,
|
|
10
|
+
import { defer, from, map as map$1, catchError, throwError, concatMap, materialize, connect, concat, of, ReplaySubject, take, 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_]*$/;
|
|
@@ -4593,7 +4593,10 @@ const offlineInterceptor = (request, next) => {
|
|
|
4593
4593
|
const plan = registry.resolve(request);
|
|
4594
4594
|
if (!plan)
|
|
4595
4595
|
return transport();
|
|
4596
|
-
|
|
4596
|
+
if (plan.readStrategy === 'local-first') {
|
|
4597
|
+
return readLocalFirst(request, plan, transport, fallback, inject(ErrorHandler));
|
|
4598
|
+
}
|
|
4599
|
+
return readNetworkFirst(request, plan, transport, fallback);
|
|
4597
4600
|
}
|
|
4598
4601
|
if (LOCAL_FIRST_MUTATION_METHODS.has(request.method)) {
|
|
4599
4602
|
const plan = inject(OfflineMutationRequestPolicyRegistry).resolve(request);
|
|
@@ -4603,7 +4606,49 @@ const offlineInterceptor = (request, next) => {
|
|
|
4603
4606
|
}
|
|
4604
4607
|
return transport();
|
|
4605
4608
|
};
|
|
4606
|
-
function
|
|
4609
|
+
function readNetworkFirst(request, plan, transport, fallback) {
|
|
4610
|
+
return defer(transport).pipe(catchError((error) => fallback.handle(request, error, plan) ?? throwError(() => error)), concatMap((event) => projectReadResponse(event, plan)));
|
|
4611
|
+
}
|
|
4612
|
+
/**
|
|
4613
|
+
* Stale-while-revalidate local-first GET handling.
|
|
4614
|
+
*
|
|
4615
|
+
* @remarks
|
|
4616
|
+
* Starts raw transport and `readLocal()` concurrently at outer subscription,
|
|
4617
|
+
* buffers materialized remote notifications until the local attempt settles,
|
|
4618
|
+
* emits a projected local response first on hit, then drains/projects the
|
|
4619
|
+
* buffered transport. Remote projection begins only after the local decision.
|
|
4620
|
+
*
|
|
4621
|
+
* Consumers must keep the returned observable subscribed through revalidation;
|
|
4622
|
+
* `firstValueFrom` and `take(1)` cancel in-flight transport and suppress
|
|
4623
|
+
* further emissions.
|
|
4624
|
+
*/
|
|
4625
|
+
function readLocalFirst(request, plan, transport, fallback, errorHandler) {
|
|
4626
|
+
return defer(transport).pipe(materialize(), connect((bufferedTransport$) => resolveLocalAttempt(plan, errorHandler).pipe(concatMap((localResponse) => localResponse
|
|
4627
|
+
? concat(of(localResponse), drainRemoteAfterLocal(bufferedTransport$, plan))
|
|
4628
|
+
: drainRemoteNetworkFirst(bufferedTransport$, request, plan, fallback))), { connector: () => new ReplaySubject() }));
|
|
4629
|
+
}
|
|
4630
|
+
function resolveLocalAttempt(plan, errorHandler) {
|
|
4631
|
+
return defer(() => from(plan.readLocal()).pipe(catchError((localError) => {
|
|
4632
|
+
errorHandler.handleError(localError);
|
|
4633
|
+
return of(null);
|
|
4634
|
+
}))).pipe(concatMap((local) => (local ? tryProjectLocal(local, plan, errorHandler) : of(null))), take(1));
|
|
4635
|
+
}
|
|
4636
|
+
function tryProjectLocal(cached, plan, errorHandler) {
|
|
4637
|
+
return emitTaggedLocalResponse(cached, plan).pipe(catchError((error) => {
|
|
4638
|
+
errorHandler.handleError(error);
|
|
4639
|
+
return of(null);
|
|
4640
|
+
}));
|
|
4641
|
+
}
|
|
4642
|
+
function emitTaggedLocalResponse(cached, plan) {
|
|
4643
|
+
return projectReadResponse(cached.clone({ headers: cached.headers.set(OFFLINE_RESPONSE_HEADER, 'local') }), plan);
|
|
4644
|
+
}
|
|
4645
|
+
function drainRemoteAfterLocal(bufferedTransport$, plan) {
|
|
4646
|
+
return bufferedTransport$.pipe(dematerialize(), catchError((error) => (isOfflineFallbackError(error) ? EMPTY : throwError(() => error))), concatMap((event) => projectReadResponse(event, plan)));
|
|
4647
|
+
}
|
|
4648
|
+
function drainRemoteNetworkFirst(bufferedTransport$, request, plan, fallback) {
|
|
4649
|
+
return bufferedTransport$.pipe(dematerialize(), catchError((error) => fallback.handle(request, error, plan) ?? throwError(() => error)), concatMap((event) => projectReadResponse(event, plan)));
|
|
4650
|
+
}
|
|
4651
|
+
function projectReadResponse(event, plan) {
|
|
4607
4652
|
if (!(event instanceof HttpResponse) || !plan.projectResponse)
|
|
4608
4653
|
return of(event);
|
|
4609
4654
|
const source = event.headers.get(OFFLINE_RESPONSE_HEADER) === 'local' ? 'local' : 'remote';
|