@mmstack/resource 20.4.0 → 20.5.0
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.
- package/LICENSE +21 -21
- package/README.md +120 -120
- package/fesm2022/mmstack-resource.mjs +128 -48
- package/fesm2022/mmstack-resource.mjs.map +1 -1
- package/index.d.ts +54 -7
- package/package.json +4 -4
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025-present Miha J. Mulec
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Miha J. Mulec
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
# @mmstack/resource
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/@mmstack/resource)
|
|
4
|
-
[](https://github.com/mihajm/mmstack/blob/master/packages/resource/LICENSE)
|
|
5
|
-
|
|
6
|
-
`@mmstack/resource` is an Angular library that provides powerful, signal-based primitives for managing asynchronous data fetching and mutations. It builds upon Angular's `httpResource` and offers features like caching, retries, refresh intervals, circuit breakers, and request deduplication, all while maintaining a fine-grained reactive graph. It's inspired by libraries like TanStack Query, but aims for a more Angular-idiomatic and signal-centric approach.
|
|
7
|
-
|
|
8
|
-
## Features
|
|
9
|
-
|
|
10
|
-
- **Signal-Based:** Fully integrates with Angular's signal system for efficient change detection and reactivity.
|
|
11
|
-
- **Caching:** Built-in caching with configurable TTL (Time To Live) and stale-while-revalidate behavior. Supports custom cache key generation and respects HTTP caching headers.
|
|
12
|
-
- **Retries:** Automatic retries on failure with configurable backoff strategies.
|
|
13
|
-
- **Refresh Intervals:** Automatically refetch data at specified intervals.
|
|
14
|
-
- **Circuit Breaker:** Protects your application from cascading failures by temporarily disabling requests to failing endpoints.
|
|
15
|
-
- **Request Deduplication:** Avoids making multiple identical requests concurrently.
|
|
16
|
-
- **Mutations:** Provides a dedicated `mutationResource` for handling data modifications, with callbacks for `onMutate`, `onError`, `onSuccess`, and `onSettled`.
|
|
17
|
-
- **Prefetching:** Allows you to prefetch data into the cache, improving perceived performance.
|
|
18
|
-
- **Extensible:** Designed to be modular and extensible. You can easily add your own custom features or integrate with other libraries.
|
|
19
|
-
- **TypeScript Support:** A strong focus on typesafety
|
|
20
|
-
|
|
21
|
-
## Quick Start
|
|
22
|
-
|
|
23
|
-
1. Install mmstack-resource
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
npm install @mmstack/resource
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
2. Initialize the QueryCache & interceptors (optional)
|
|
30
|
-
|
|
31
|
-
```typescript
|
|
32
|
-
import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
33
|
-
import { ApplicationConfig } from '@angular/core';
|
|
34
|
-
import { createCacheInterceptor, createDedupeRequestsInterceptor, provideQueryCache } from '@mmstack/resource';
|
|
35
|
-
|
|
36
|
-
export const appConfig: ApplicationConfig = {
|
|
37
|
-
providers: [
|
|
38
|
-
// ..other providers
|
|
39
|
-
provideQueryCache(),
|
|
40
|
-
|
|
41
|
-
// --- Example of a more advanced setup ---
|
|
42
|
-
// provideQueryCache({
|
|
43
|
-
// persist: true, // Enable IndexedDB persistence
|
|
44
|
-
// version: 1, // Version for the cache schema
|
|
45
|
-
// syncTabs: true // enable BroadcastChannel
|
|
46
|
-
// }),
|
|
47
|
-
|
|
48
|
-
provideHttpClient(withInterceptors([createCacheInterceptor(), createDedupeRequestsInterceptor()])),
|
|
49
|
-
],
|
|
50
|
-
};
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
3. Use it :)
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
import { Injectable, isDevMode, untracked } from '@angular/core';
|
|
57
|
-
import { createCircuitBreaker, mutationResource, queryResource } from '@mmstack/resource';
|
|
58
|
-
|
|
59
|
-
type Post = {
|
|
60
|
-
userId: number;
|
|
61
|
-
id: number;
|
|
62
|
-
title: string;
|
|
63
|
-
body: string;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
@Injectable({
|
|
67
|
-
providedIn: 'root',
|
|
68
|
-
})
|
|
69
|
-
export class PostsService {
|
|
70
|
-
private readonly endpoint = 'https://jsonplaceholder.typicode.com/posts';
|
|
71
|
-
private readonly cb = createCircuitBreaker();
|
|
72
|
-
readonly posts = queryResource<Post[]>(
|
|
73
|
-
() => ({
|
|
74
|
-
url: this.endpoint,
|
|
75
|
-
}),
|
|
76
|
-
{
|
|
77
|
-
keepPrevious: true, // keep data between requests
|
|
78
|
-
refresh: 5 * 60 * 1000, // refresh every 5 minutes
|
|
79
|
-
circuitBreaker: this.cb, // use shared circuit breaker use true if not sharing
|
|
80
|
-
retry: 3, // retry 3 times on error using default backoff
|
|
81
|
-
onError: (err) => {
|
|
82
|
-
if (!isDevMode()) return;
|
|
83
|
-
console.error(err);
|
|
84
|
-
}, // log errors in dev mode
|
|
85
|
-
defaultValue: [],
|
|
86
|
-
},
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
private readonly createPostResource = mutationResource(
|
|
90
|
-
(post: Post) => ({
|
|
91
|
-
url: this.endpoint,
|
|
92
|
-
method: 'POST',
|
|
93
|
-
body: post,
|
|
94
|
-
}),
|
|
95
|
-
{
|
|
96
|
-
circuitBreaker: this.cb, // use shared circuit breaker use true if not sharing
|
|
97
|
-
onMutate: (post: Post) => {
|
|
98
|
-
const prev = untracked(this.posts.value);
|
|
99
|
-
this.posts.set([...prev, post]); // optimistically update
|
|
100
|
-
return prev;
|
|
101
|
-
},
|
|
102
|
-
onError: (err, prev) => {
|
|
103
|
-
if (isDevMode()) console.error(err);
|
|
104
|
-
this.posts.set(prev); // rollback on error
|
|
105
|
-
},
|
|
106
|
-
onSuccess: (next) => {
|
|
107
|
-
this.posts.update((posts) => posts.map((p) => (p.id === next.id ? next : p))); // replace with value from server
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
createPost(post: Post) {
|
|
113
|
-
this.createPostResource.mutate(post); // send the request
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
## In-depth
|
|
119
|
-
|
|
120
|
-
For an in-depth explanation of the primitives & how they work check out this article: [Fun-grained Reactivity in Angular: Part 3 - Resources](https://dev.to/mihamulec/fun-grained-reactivity-in-angular-part-3-client-side-http-57g4)
|
|
1
|
+
# @mmstack/resource
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@mmstack/resource)
|
|
4
|
+
[](https://github.com/mihajm/mmstack/blob/master/packages/resource/LICENSE)
|
|
5
|
+
|
|
6
|
+
`@mmstack/resource` is an Angular library that provides powerful, signal-based primitives for managing asynchronous data fetching and mutations. It builds upon Angular's `httpResource` and offers features like caching, retries, refresh intervals, circuit breakers, and request deduplication, all while maintaining a fine-grained reactive graph. It's inspired by libraries like TanStack Query, but aims for a more Angular-idiomatic and signal-centric approach.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Signal-Based:** Fully integrates with Angular's signal system for efficient change detection and reactivity.
|
|
11
|
+
- **Caching:** Built-in caching with configurable TTL (Time To Live) and stale-while-revalidate behavior. Supports custom cache key generation and respects HTTP caching headers.
|
|
12
|
+
- **Retries:** Automatic retries on failure with configurable backoff strategies.
|
|
13
|
+
- **Refresh Intervals:** Automatically refetch data at specified intervals.
|
|
14
|
+
- **Circuit Breaker:** Protects your application from cascading failures by temporarily disabling requests to failing endpoints.
|
|
15
|
+
- **Request Deduplication:** Avoids making multiple identical requests concurrently.
|
|
16
|
+
- **Mutations:** Provides a dedicated `mutationResource` for handling data modifications, with callbacks for `onMutate`, `onError`, `onSuccess`, and `onSettled`.
|
|
17
|
+
- **Prefetching:** Allows you to prefetch data into the cache, improving perceived performance.
|
|
18
|
+
- **Extensible:** Designed to be modular and extensible. You can easily add your own custom features or integrate with other libraries.
|
|
19
|
+
- **TypeScript Support:** A strong focus on typesafety
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
1. Install mmstack-resource
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @mmstack/resource
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2. Initialize the QueryCache & interceptors (optional)
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
33
|
+
import { ApplicationConfig } from '@angular/core';
|
|
34
|
+
import { createCacheInterceptor, createDedupeRequestsInterceptor, provideQueryCache } from '@mmstack/resource';
|
|
35
|
+
|
|
36
|
+
export const appConfig: ApplicationConfig = {
|
|
37
|
+
providers: [
|
|
38
|
+
// ..other providers
|
|
39
|
+
provideQueryCache(),
|
|
40
|
+
|
|
41
|
+
// --- Example of a more advanced setup ---
|
|
42
|
+
// provideQueryCache({
|
|
43
|
+
// persist: true, // Enable IndexedDB persistence
|
|
44
|
+
// version: 1, // Version for the cache schema
|
|
45
|
+
// syncTabs: true // enable BroadcastChannel
|
|
46
|
+
// }),
|
|
47
|
+
|
|
48
|
+
provideHttpClient(withInterceptors([createCacheInterceptor(), createDedupeRequestsInterceptor()])),
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
3. Use it :)
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { Injectable, isDevMode, untracked } from '@angular/core';
|
|
57
|
+
import { createCircuitBreaker, mutationResource, queryResource } from '@mmstack/resource';
|
|
58
|
+
|
|
59
|
+
type Post = {
|
|
60
|
+
userId: number;
|
|
61
|
+
id: number;
|
|
62
|
+
title: string;
|
|
63
|
+
body: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
@Injectable({
|
|
67
|
+
providedIn: 'root',
|
|
68
|
+
})
|
|
69
|
+
export class PostsService {
|
|
70
|
+
private readonly endpoint = 'https://jsonplaceholder.typicode.com/posts';
|
|
71
|
+
private readonly cb = createCircuitBreaker();
|
|
72
|
+
readonly posts = queryResource<Post[]>(
|
|
73
|
+
() => ({
|
|
74
|
+
url: this.endpoint,
|
|
75
|
+
}),
|
|
76
|
+
{
|
|
77
|
+
keepPrevious: true, // keep data between requests
|
|
78
|
+
refresh: 5 * 60 * 1000, // refresh every 5 minutes
|
|
79
|
+
circuitBreaker: this.cb, // use shared circuit breaker use true if not sharing
|
|
80
|
+
retry: 3, // retry 3 times on error using default backoff
|
|
81
|
+
onError: (err) => {
|
|
82
|
+
if (!isDevMode()) return;
|
|
83
|
+
console.error(err);
|
|
84
|
+
}, // log errors in dev mode
|
|
85
|
+
defaultValue: [],
|
|
86
|
+
},
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
private readonly createPostResource = mutationResource(
|
|
90
|
+
(post: Post) => ({
|
|
91
|
+
url: this.endpoint,
|
|
92
|
+
method: 'POST',
|
|
93
|
+
body: post,
|
|
94
|
+
}),
|
|
95
|
+
{
|
|
96
|
+
circuitBreaker: this.cb, // use shared circuit breaker use true if not sharing
|
|
97
|
+
onMutate: (post: Post) => {
|
|
98
|
+
const prev = untracked(this.posts.value);
|
|
99
|
+
this.posts.set([...prev, post]); // optimistically update
|
|
100
|
+
return prev;
|
|
101
|
+
},
|
|
102
|
+
onError: (err, prev) => {
|
|
103
|
+
if (isDevMode()) console.error(err);
|
|
104
|
+
this.posts.set(prev); // rollback on error
|
|
105
|
+
},
|
|
106
|
+
onSuccess: (next) => {
|
|
107
|
+
this.posts.update((posts) => posts.map((p) => (p.id === next.id ? next : p))); // replace with value from server
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
createPost(post: Post) {
|
|
113
|
+
this.createPostResource.mutate(post); // send the request
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## In-depth
|
|
119
|
+
|
|
120
|
+
For an in-depth explanation of the primitives & how they work check out this article: [Fun-grained Reactivity in Angular: Part 3 - Resources](https://dev.to/mihamulec/fun-grained-reactivity-in-angular-part-3-client-side-http-57g4)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
2
|
import { isDevMode, untracked, computed, InjectionToken, inject, signal, effect, Injector, linkedSignal, Injectable, DestroyRef } from '@angular/core';
|
|
3
|
-
import {
|
|
4
|
-
import { of, tap, map, finalize, shareReplay, interval, firstValueFrom, catchError, combineLatestWith, filter } from 'rxjs';
|
|
3
|
+
import { mutable, toWritable, sensor, nestedEffect } from '@mmstack/primitives';
|
|
5
4
|
import { HttpHeaders, HttpResponse, HttpContextToken, HttpContext, HttpParams, httpResource, HttpClient } from '@angular/common/http';
|
|
6
|
-
import {
|
|
5
|
+
import { of, tap, map, finalize, shareReplay, interval, firstValueFrom, catchError, combineLatestWith, filter } from 'rxjs';
|
|
6
|
+
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
|
|
7
7
|
|
|
8
8
|
function createNoopDB() {
|
|
9
9
|
return {
|
|
@@ -945,7 +945,12 @@ function createDedupeRequestsInterceptor(allowed = ['GET', 'DELETE', 'HEAD', 'OP
|
|
|
945
945
|
* isPlainObject(new Date()) // => false
|
|
946
946
|
*/
|
|
947
947
|
function isPlainObject(value) {
|
|
948
|
-
|
|
948
|
+
if (value === null || typeof value !== 'object')
|
|
949
|
+
return false;
|
|
950
|
+
const proto = Object.getPrototypeOf(value);
|
|
951
|
+
if (proto === null)
|
|
952
|
+
return false; // remove Object.create(null);
|
|
953
|
+
return proto === Object.prototype;
|
|
949
954
|
}
|
|
950
955
|
/**
|
|
951
956
|
* Internal helper to generate a stable JSON string from an array.
|
|
@@ -1173,15 +1178,21 @@ function hasSlowConnection() {
|
|
|
1173
1178
|
|
|
1174
1179
|
function persist(src, equal) {
|
|
1175
1180
|
// linkedSignal allows us to access previous source value
|
|
1176
|
-
const persisted = linkedSignal({
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
return
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1181
|
+
const persisted = linkedSignal(...(ngDevMode ? [{ debugName: "persisted", source: () => src(),
|
|
1182
|
+
computation: (next, prev) => {
|
|
1183
|
+
if (next === undefined && prev !== undefined)
|
|
1184
|
+
return prev.value;
|
|
1185
|
+
return next;
|
|
1186
|
+
},
|
|
1187
|
+
equal }] : [{
|
|
1188
|
+
source: () => src(),
|
|
1189
|
+
computation: (next, prev) => {
|
|
1190
|
+
if (next === undefined && prev !== undefined)
|
|
1191
|
+
return prev.value;
|
|
1192
|
+
return next;
|
|
1193
|
+
},
|
|
1194
|
+
equal,
|
|
1195
|
+
}]));
|
|
1185
1196
|
// if original value was WritableSignal then override linkedSignal methods to original...angular uses linkedSignal under the hood in ResourceImpl, this applies to that.
|
|
1186
1197
|
if ('set' in src) {
|
|
1187
1198
|
persisted.set = src.set;
|
|
@@ -1266,10 +1277,10 @@ function retryOnError(res, opt) {
|
|
|
1266
1277
|
|
|
1267
1278
|
class ResourceSensors {
|
|
1268
1279
|
networkStatus = sensor('networkStatus');
|
|
1269
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.
|
|
1270
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.
|
|
1280
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: ResourceSensors, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1281
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: ResourceSensors, providedIn: 'root' });
|
|
1271
1282
|
}
|
|
1272
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.
|
|
1283
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: ResourceSensors, decorators: [{
|
|
1273
1284
|
type: Injectable,
|
|
1274
1285
|
args: [{
|
|
1275
1286
|
providedIn: 'root',
|
|
@@ -1335,7 +1346,12 @@ function queryResource(request, options) {
|
|
|
1335
1346
|
const stableRequest = computed(() => {
|
|
1336
1347
|
if (!networkAvailable() || cb.isOpen())
|
|
1337
1348
|
return undefined;
|
|
1338
|
-
|
|
1349
|
+
const req = request();
|
|
1350
|
+
if (!req)
|
|
1351
|
+
return undefined;
|
|
1352
|
+
if (typeof req === 'string')
|
|
1353
|
+
return { method: 'GET', url: req };
|
|
1354
|
+
return req;
|
|
1339
1355
|
}, ...(ngDevMode ? [{ debugName: "stableRequest", equal: (a, b) => {
|
|
1340
1356
|
if (eq)
|
|
1341
1357
|
return eq(a, b);
|
|
@@ -1388,27 +1404,45 @@ function queryResource(request, options) {
|
|
|
1388
1404
|
resource = catchValueError(resource, options?.defaultValue);
|
|
1389
1405
|
// get full HttpResonse from Cache
|
|
1390
1406
|
const cachedEvent = cache.getEntryOrKey(cacheKey);
|
|
1391
|
-
const cacheEntry = linkedSignal({
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
return {
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
}
|
|
1410
|
-
|
|
1411
|
-
|
|
1407
|
+
const cacheEntry = linkedSignal(...(ngDevMode ? [{ debugName: "cacheEntry", source: () => cachedEvent(),
|
|
1408
|
+
computation: (entry, prev) => {
|
|
1409
|
+
if (!entry)
|
|
1410
|
+
return null;
|
|
1411
|
+
if (typeof entry === 'string' &&
|
|
1412
|
+
prev &&
|
|
1413
|
+
prev.value !== null &&
|
|
1414
|
+
prev.value.key === entry) {
|
|
1415
|
+
return prev.value;
|
|
1416
|
+
}
|
|
1417
|
+
if (typeof entry === 'string')
|
|
1418
|
+
return { key: entry, value: null };
|
|
1419
|
+
if (!(entry.value instanceof HttpResponse))
|
|
1420
|
+
return { key: entry.key, value: null };
|
|
1421
|
+
return {
|
|
1422
|
+
value: entry.value.body,
|
|
1423
|
+
key: entry.key,
|
|
1424
|
+
};
|
|
1425
|
+
} }] : [{
|
|
1426
|
+
source: () => cachedEvent(),
|
|
1427
|
+
computation: (entry, prev) => {
|
|
1428
|
+
if (!entry)
|
|
1429
|
+
return null;
|
|
1430
|
+
if (typeof entry === 'string' &&
|
|
1431
|
+
prev &&
|
|
1432
|
+
prev.value !== null &&
|
|
1433
|
+
prev.value.key === entry) {
|
|
1434
|
+
return prev.value;
|
|
1435
|
+
}
|
|
1436
|
+
if (typeof entry === 'string')
|
|
1437
|
+
return { key: entry, value: null };
|
|
1438
|
+
if (!(entry.value instanceof HttpResponse))
|
|
1439
|
+
return { key: entry.key, value: null };
|
|
1440
|
+
return {
|
|
1441
|
+
value: entry.value.body,
|
|
1442
|
+
key: entry.key,
|
|
1443
|
+
};
|
|
1444
|
+
},
|
|
1445
|
+
}]));
|
|
1412
1446
|
resource = refresh(resource, destroyRef, options?.refresh);
|
|
1413
1447
|
resource = retryOnError(resource, options?.retry);
|
|
1414
1448
|
resource = persistResourceValues(resource, options?.keepPrevious, options?.equal);
|
|
@@ -1477,9 +1511,10 @@ function queryResource(request, options) {
|
|
|
1477
1511
|
if (!options?.cache || hasSlowConnection())
|
|
1478
1512
|
return Promise.resolve();
|
|
1479
1513
|
const request = untracked(stableRequest);
|
|
1514
|
+
const partialReq = typeof partial === 'string' ? { method: 'GET', url: partial } : partial;
|
|
1480
1515
|
const prefetchRequest = {
|
|
1481
1516
|
...request,
|
|
1482
|
-
...
|
|
1517
|
+
...partialReq,
|
|
1483
1518
|
};
|
|
1484
1519
|
if (!prefetchRequest.url)
|
|
1485
1520
|
return Promise.resolve();
|
|
@@ -1523,6 +1558,46 @@ function queryResource(request, options) {
|
|
|
1523
1558
|
};
|
|
1524
1559
|
}
|
|
1525
1560
|
|
|
1561
|
+
function manualQueryResource(request, options) {
|
|
1562
|
+
const trigger = signal({ epoch: 0 }, ...(ngDevMode ? [{ debugName: "trigger", equal: (a, b) => a.epoch === b.epoch }] : [{
|
|
1563
|
+
equal: (a, b) => a.epoch === b.epoch,
|
|
1564
|
+
}]));
|
|
1565
|
+
const injector = options?.injector ?? inject(Injector);
|
|
1566
|
+
const req = computed(() => {
|
|
1567
|
+
const state = trigger();
|
|
1568
|
+
if (state.epoch === 0)
|
|
1569
|
+
return;
|
|
1570
|
+
if (state.override)
|
|
1571
|
+
return state.override;
|
|
1572
|
+
return untracked(request);
|
|
1573
|
+
}, ...(ngDevMode ? [{ debugName: "req", equal: () => false }] : [{
|
|
1574
|
+
equal: () => false,
|
|
1575
|
+
}]));
|
|
1576
|
+
const resource = queryResource(req, options);
|
|
1577
|
+
return {
|
|
1578
|
+
...resource,
|
|
1579
|
+
trigger: (override, injectorOverride) => {
|
|
1580
|
+
trigger.update((s) => ({
|
|
1581
|
+
epoch: s.epoch + 1,
|
|
1582
|
+
override,
|
|
1583
|
+
}));
|
|
1584
|
+
return new Promise((res, rej) => {
|
|
1585
|
+
const watcher = nestedEffect(() => {
|
|
1586
|
+
const status = resource.status();
|
|
1587
|
+
if (status === 'resolved') {
|
|
1588
|
+
watcher.destroy();
|
|
1589
|
+
res(untracked(resource.value));
|
|
1590
|
+
}
|
|
1591
|
+
else if (status === 'error') {
|
|
1592
|
+
watcher.destroy();
|
|
1593
|
+
rej(untracked(resource.error));
|
|
1594
|
+
}
|
|
1595
|
+
}, { injector: injectorOverride ?? injector });
|
|
1596
|
+
});
|
|
1597
|
+
},
|
|
1598
|
+
};
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1526
1601
|
/**
|
|
1527
1602
|
* Creates a resource for performing mutations (e.g., POST, PUT, PATCH, DELETE requests).
|
|
1528
1603
|
* Unlike `queryResource`, `mutationResource` is designed for one-off operations that change data.
|
|
@@ -1580,14 +1655,19 @@ function mutationResource(request, options = {}) {
|
|
|
1580
1655
|
}, ...(ngDevMode ? [{ debugName: "req", equal: requestEqual }] : [{
|
|
1581
1656
|
equal: requestEqual,
|
|
1582
1657
|
}]));
|
|
1583
|
-
const lastValue = linkedSignal({
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
return
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1658
|
+
const lastValue = linkedSignal(...(ngDevMode ? [{ debugName: "lastValue", source: next,
|
|
1659
|
+
computation: (next, prev) => {
|
|
1660
|
+
if (next === null && !!prev)
|
|
1661
|
+
return prev.value;
|
|
1662
|
+
return next;
|
|
1663
|
+
} }] : [{
|
|
1664
|
+
source: next,
|
|
1665
|
+
computation: (next, prev) => {
|
|
1666
|
+
if (next === null && !!prev)
|
|
1667
|
+
return prev.value;
|
|
1668
|
+
return next;
|
|
1669
|
+
},
|
|
1670
|
+
}]));
|
|
1591
1671
|
const lastValueRequest = computed(() => {
|
|
1592
1672
|
const nr = lastValue();
|
|
1593
1673
|
if (!nr)
|
|
@@ -1661,5 +1741,5 @@ function mutationResource(request, options = {}) {
|
|
|
1661
1741
|
* Generated bundle index. Do not edit.
|
|
1662
1742
|
*/
|
|
1663
1743
|
|
|
1664
|
-
export { Cache, createCacheInterceptor, createCircuitBreaker, createDedupeRequestsInterceptor, injectQueryCache, mutationResource, noDedupe, provideCircuitBreakerDefaultOptions, provideQueryCache, queryResource };
|
|
1744
|
+
export { Cache, createCacheInterceptor, createCircuitBreaker, createDedupeRequestsInterceptor, injectQueryCache, manualQueryResource, mutationResource, noDedupe, provideCircuitBreakerDefaultOptions, provideQueryCache, queryResource };
|
|
1665
1745
|
//# sourceMappingURL=mmstack-resource.mjs.map
|