@mmstack/resource 21.4.3 → 21.4.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.
|
@@ -2448,13 +2448,17 @@ function mutationResource(request, options0 = {}) {
|
|
|
2448
2448
|
return false;
|
|
2449
2449
|
return eq(a, b);
|
|
2450
2450
|
} });
|
|
2451
|
-
const
|
|
2451
|
+
const queueEnabled = !!options.queue;
|
|
2452
|
+
const queueKeyFn = typeof options.queue === 'object' ? options.queue.key : undefined;
|
|
2453
|
+
const queue = linkedSignal({ ...(ngDevMode ? { debugName: "queue" } : /* istanbul ignore next */ {}), source: () => queueKeyFn?.(),
|
|
2454
|
+
computation: () => signal([]) });
|
|
2452
2455
|
let ctx = undefined;
|
|
2453
2456
|
const queueRef = effect(() => {
|
|
2454
|
-
const
|
|
2457
|
+
const q = queue(); // subscribe to swaps (key change / clearQueue)
|
|
2458
|
+
const nextInQueue = q().at(0); // subscribe to contents
|
|
2455
2459
|
if (nextInQueue === undefined || next() !== NULL_VALUE)
|
|
2456
2460
|
return;
|
|
2457
|
-
|
|
2461
|
+
q.update((arr) => arr.slice(1));
|
|
2458
2462
|
const [value, ictx] = nextInQueue;
|
|
2459
2463
|
try {
|
|
2460
2464
|
ctx = onMutate?.(value, ictx);
|
|
@@ -2466,7 +2470,7 @@ function mutationResource(request, options0 = {}) {
|
|
|
2466
2470
|
if (isDevMode())
|
|
2467
2471
|
console.error('[@mmstack/resource]: error thrown in onMutate hook, mutation was not applied', mutationErr);
|
|
2468
2472
|
}
|
|
2469
|
-
}, ...(ngDevMode ?
|
|
2473
|
+
}, { ...(ngDevMode ? { debugName: "queueRef" } : /* istanbul ignore next */ {}), injector: options.injector });
|
|
2470
2474
|
const req = computed(() => {
|
|
2471
2475
|
const nr = next();
|
|
2472
2476
|
if (nr === NULL_VALUE)
|
|
@@ -2512,9 +2516,13 @@ function mutationResource(request, options0 = {}) {
|
|
|
2512
2516
|
const destroyRef = options.injector
|
|
2513
2517
|
? options.injector.get(DestroyRef)
|
|
2514
2518
|
: inject(DestroyRef);
|
|
2515
|
-
const error$ = toObservable(resource.error);
|
|
2516
|
-
const value$ = toObservable(resource.value
|
|
2517
|
-
|
|
2519
|
+
const error$ = toObservable(resource.error, { injector: options.injector });
|
|
2520
|
+
const value$ = toObservable(resource.value, {
|
|
2521
|
+
injector: options.injector,
|
|
2522
|
+
}).pipe(catchError(() => of(NULL_VALUE)));
|
|
2523
|
+
const statusSub = toObservable(resource.status, {
|
|
2524
|
+
injector: options.injector,
|
|
2525
|
+
})
|
|
2518
2526
|
.pipe(combineLatestWith(error$, value$), map(([status, error, value]) => {
|
|
2519
2527
|
if (status === 'error' && error) {
|
|
2520
2528
|
return {
|
|
@@ -2550,7 +2558,6 @@ function mutationResource(request, options0 = {}) {
|
|
|
2550
2558
|
ctx = undefined;
|
|
2551
2559
|
next.set(NULL_VALUE);
|
|
2552
2560
|
});
|
|
2553
|
-
const shouldQueue = options.queue ?? false;
|
|
2554
2561
|
const ref = {
|
|
2555
2562
|
...resource,
|
|
2556
2563
|
destroy: () => {
|
|
@@ -2560,8 +2567,8 @@ function mutationResource(request, options0 = {}) {
|
|
|
2560
2567
|
resource.destroy();
|
|
2561
2568
|
},
|
|
2562
2569
|
mutate: (value, ictx) => {
|
|
2563
|
-
if (
|
|
2564
|
-
return queue.update((
|
|
2570
|
+
if (queueEnabled) {
|
|
2571
|
+
return queue().update((arr) => [...arr, [value, ictx]]);
|
|
2565
2572
|
}
|
|
2566
2573
|
else {
|
|
2567
2574
|
// latest-wins: a mutation already in flight gets superseded (its request is
|
|
@@ -2595,6 +2602,11 @@ function mutationResource(request, options0 = {}) {
|
|
|
2595
2602
|
const nv = next();
|
|
2596
2603
|
return nv === NULL_VALUE ? null : nv;
|
|
2597
2604
|
}),
|
|
2605
|
+
clearQueue: () => {
|
|
2606
|
+
if (!queueEnabled)
|
|
2607
|
+
return;
|
|
2608
|
+
queue.set(signal([]));
|
|
2609
|
+
},
|
|
2598
2610
|
// redeclare disabled with last value so that it is not affected by the resource's internal disablement logic
|
|
2599
2611
|
disabled: computed(() => cb.isOpen() || lastValueRequest() === undefined),
|
|
2600
2612
|
};
|