@mmstack/resource 20.8.3 → 20.8.5
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/fesm2022/mmstack-resource.mjs +25 -10
- package/fesm2022/mmstack-resource.mjs.map +1 -1
- package/index.d.ts +22 -3
- package/package.json +1 -1
|
@@ -2504,13 +2504,20 @@ function mutationResource(request, options0 = {}) {
|
|
|
2504
2504
|
return eq(a, b);
|
|
2505
2505
|
},
|
|
2506
2506
|
}]));
|
|
2507
|
-
const
|
|
2507
|
+
const queueEnabled = !!options.queue;
|
|
2508
|
+
const queueKeyFn = typeof options.queue === 'object' ? options.queue.key : undefined;
|
|
2509
|
+
const queue = linkedSignal(...(ngDevMode ? [{ debugName: "queue", source: () => queueKeyFn?.(),
|
|
2510
|
+
computation: () => signal([]) }] : [{
|
|
2511
|
+
source: () => queueKeyFn?.(),
|
|
2512
|
+
computation: () => signal([]),
|
|
2513
|
+
}]));
|
|
2508
2514
|
let ctx = undefined;
|
|
2509
2515
|
const queueRef = effect(() => {
|
|
2510
|
-
const
|
|
2516
|
+
const q = queue(); // subscribe to swaps (key change / clearQueue)
|
|
2517
|
+
const nextInQueue = q().at(0); // subscribe to contents
|
|
2511
2518
|
if (nextInQueue === undefined || next() !== NULL_VALUE)
|
|
2512
2519
|
return;
|
|
2513
|
-
|
|
2520
|
+
q.update((arr) => arr.slice(1));
|
|
2514
2521
|
const [value, ictx] = nextInQueue;
|
|
2515
2522
|
try {
|
|
2516
2523
|
ctx = onMutate?.(value, ictx);
|
|
@@ -2522,7 +2529,7 @@ function mutationResource(request, options0 = {}) {
|
|
|
2522
2529
|
if (isDevMode())
|
|
2523
2530
|
console.error('[@mmstack/resource]: error thrown in onMutate hook, mutation was not applied', mutationErr);
|
|
2524
2531
|
}
|
|
2525
|
-
}, ...(ngDevMode ? [{ debugName: "queueRef" }] : []));
|
|
2532
|
+
}, ...(ngDevMode ? [{ debugName: "queueRef", injector: options.injector }] : [{ injector: options.injector }]));
|
|
2526
2533
|
const req = computed(() => {
|
|
2527
2534
|
const nr = next();
|
|
2528
2535
|
if (nr === NULL_VALUE)
|
|
@@ -2593,9 +2600,13 @@ function mutationResource(request, options0 = {}) {
|
|
|
2593
2600
|
const destroyRef = options.injector
|
|
2594
2601
|
? options.injector.get(DestroyRef)
|
|
2595
2602
|
: inject(DestroyRef);
|
|
2596
|
-
const error$ = toObservable(resource.error);
|
|
2597
|
-
const value$ = toObservable(resource.value
|
|
2598
|
-
|
|
2603
|
+
const error$ = toObservable(resource.error, { injector: options.injector });
|
|
2604
|
+
const value$ = toObservable(resource.value, {
|
|
2605
|
+
injector: options.injector,
|
|
2606
|
+
}).pipe(catchError(() => of(NULL_VALUE)));
|
|
2607
|
+
const statusSub = toObservable(resource.status, {
|
|
2608
|
+
injector: options.injector,
|
|
2609
|
+
})
|
|
2599
2610
|
.pipe(combineLatestWith(error$, value$), map(([status, error, value]) => {
|
|
2600
2611
|
if (status === 'error' && error) {
|
|
2601
2612
|
return {
|
|
@@ -2631,7 +2642,6 @@ function mutationResource(request, options0 = {}) {
|
|
|
2631
2642
|
ctx = undefined;
|
|
2632
2643
|
next.set(NULL_VALUE);
|
|
2633
2644
|
});
|
|
2634
|
-
const shouldQueue = options.queue ?? false;
|
|
2635
2645
|
const ref = {
|
|
2636
2646
|
...resource,
|
|
2637
2647
|
destroy: () => {
|
|
@@ -2641,8 +2651,8 @@ function mutationResource(request, options0 = {}) {
|
|
|
2641
2651
|
resource.destroy();
|
|
2642
2652
|
},
|
|
2643
2653
|
mutate: (value, ictx) => {
|
|
2644
|
-
if (
|
|
2645
|
-
return queue.update((
|
|
2654
|
+
if (queueEnabled) {
|
|
2655
|
+
return queue().update((arr) => [...arr, [value, ictx]]);
|
|
2646
2656
|
}
|
|
2647
2657
|
else {
|
|
2648
2658
|
// latest-wins: a mutation already in flight gets superseded (its request is
|
|
@@ -2676,6 +2686,11 @@ function mutationResource(request, options0 = {}) {
|
|
|
2676
2686
|
const nv = next();
|
|
2677
2687
|
return nv === NULL_VALUE ? null : nv;
|
|
2678
2688
|
}),
|
|
2689
|
+
clearQueue: () => {
|
|
2690
|
+
if (!queueEnabled)
|
|
2691
|
+
return;
|
|
2692
|
+
queue.set(signal([]));
|
|
2693
|
+
},
|
|
2679
2694
|
// redeclare disabled with last value so that it is not affected by the resource's internal disablement logic
|
|
2680
2695
|
disabled: computed(() => cb.isOpen() || lastValueRequest() === undefined),
|
|
2681
2696
|
};
|