@mmstack/resource 20.4.1 → 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 +122 -47
- package/fesm2022/mmstack-resource.mjs.map +1 -1
- package/index.d.ts +54 -7
- package/package.json +2 -2
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 {
|
|
@@ -1178,15 +1178,21 @@ function hasSlowConnection() {
|
|
|
1178
1178
|
|
|
1179
1179
|
function persist(src, equal) {
|
|
1180
1180
|
// linkedSignal allows us to access previous source value
|
|
1181
|
-
const persisted = linkedSignal({
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
return
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
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
|
+
}]));
|
|
1190
1196
|
// if original value was WritableSignal then override linkedSignal methods to original...angular uses linkedSignal under the hood in ResourceImpl, this applies to that.
|
|
1191
1197
|
if ('set' in src) {
|
|
1192
1198
|
persisted.set = src.set;
|
|
@@ -1271,10 +1277,10 @@ function retryOnError(res, opt) {
|
|
|
1271
1277
|
|
|
1272
1278
|
class ResourceSensors {
|
|
1273
1279
|
networkStatus = sensor('networkStatus');
|
|
1274
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.
|
|
1275
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.
|
|
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' });
|
|
1276
1282
|
}
|
|
1277
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.
|
|
1283
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: ResourceSensors, decorators: [{
|
|
1278
1284
|
type: Injectable,
|
|
1279
1285
|
args: [{
|
|
1280
1286
|
providedIn: 'root',
|
|
@@ -1340,7 +1346,12 @@ function queryResource(request, options) {
|
|
|
1340
1346
|
const stableRequest = computed(() => {
|
|
1341
1347
|
if (!networkAvailable() || cb.isOpen())
|
|
1342
1348
|
return undefined;
|
|
1343
|
-
|
|
1349
|
+
const req = request();
|
|
1350
|
+
if (!req)
|
|
1351
|
+
return undefined;
|
|
1352
|
+
if (typeof req === 'string')
|
|
1353
|
+
return { method: 'GET', url: req };
|
|
1354
|
+
return req;
|
|
1344
1355
|
}, ...(ngDevMode ? [{ debugName: "stableRequest", equal: (a, b) => {
|
|
1345
1356
|
if (eq)
|
|
1346
1357
|
return eq(a, b);
|
|
@@ -1393,27 +1404,45 @@ function queryResource(request, options) {
|
|
|
1393
1404
|
resource = catchValueError(resource, options?.defaultValue);
|
|
1394
1405
|
// get full HttpResonse from Cache
|
|
1395
1406
|
const cachedEvent = cache.getEntryOrKey(cacheKey);
|
|
1396
|
-
const cacheEntry = linkedSignal({
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
return {
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
|
-
|
|
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
|
+
}]));
|
|
1417
1446
|
resource = refresh(resource, destroyRef, options?.refresh);
|
|
1418
1447
|
resource = retryOnError(resource, options?.retry);
|
|
1419
1448
|
resource = persistResourceValues(resource, options?.keepPrevious, options?.equal);
|
|
@@ -1482,9 +1511,10 @@ function queryResource(request, options) {
|
|
|
1482
1511
|
if (!options?.cache || hasSlowConnection())
|
|
1483
1512
|
return Promise.resolve();
|
|
1484
1513
|
const request = untracked(stableRequest);
|
|
1514
|
+
const partialReq = typeof partial === 'string' ? { method: 'GET', url: partial } : partial;
|
|
1485
1515
|
const prefetchRequest = {
|
|
1486
1516
|
...request,
|
|
1487
|
-
...
|
|
1517
|
+
...partialReq,
|
|
1488
1518
|
};
|
|
1489
1519
|
if (!prefetchRequest.url)
|
|
1490
1520
|
return Promise.resolve();
|
|
@@ -1528,6 +1558,46 @@ function queryResource(request, options) {
|
|
|
1528
1558
|
};
|
|
1529
1559
|
}
|
|
1530
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
|
+
|
|
1531
1601
|
/**
|
|
1532
1602
|
* Creates a resource for performing mutations (e.g., POST, PUT, PATCH, DELETE requests).
|
|
1533
1603
|
* Unlike `queryResource`, `mutationResource` is designed for one-off operations that change data.
|
|
@@ -1585,14 +1655,19 @@ function mutationResource(request, options = {}) {
|
|
|
1585
1655
|
}, ...(ngDevMode ? [{ debugName: "req", equal: requestEqual }] : [{
|
|
1586
1656
|
equal: requestEqual,
|
|
1587
1657
|
}]));
|
|
1588
|
-
const lastValue = linkedSignal({
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
return
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
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
|
+
}]));
|
|
1596
1671
|
const lastValueRequest = computed(() => {
|
|
1597
1672
|
const nr = lastValue();
|
|
1598
1673
|
if (!nr)
|
|
@@ -1666,5 +1741,5 @@ function mutationResource(request, options = {}) {
|
|
|
1666
1741
|
* Generated bundle index. Do not edit.
|
|
1667
1742
|
*/
|
|
1668
1743
|
|
|
1669
|
-
export { Cache, createCacheInterceptor, createCircuitBreaker, createDedupeRequestsInterceptor, injectQueryCache, mutationResource, noDedupe, provideCircuitBreakerDefaultOptions, provideQueryCache, queryResource };
|
|
1744
|
+
export { Cache, createCacheInterceptor, createCircuitBreaker, createDedupeRequestsInterceptor, injectQueryCache, manualQueryResource, mutationResource, noDedupe, provideCircuitBreakerDefaultOptions, provideQueryCache, queryResource };
|
|
1670
1745
|
//# sourceMappingURL=mmstack-resource.mjs.map
|