@mmstack/resource 19.6.3 → 19.6.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.
@@ -2455,13 +2455,19 @@ function mutationResource(request, options0 = {}) {
2455
2455
  return eq(a, b);
2456
2456
  },
2457
2457
  });
2458
- const queue = signal([]);
2458
+ const queueEnabled = !!options.queue;
2459
+ const queueKeyFn = typeof options.queue === 'object' ? options.queue.key : undefined;
2460
+ const queue = linkedSignal({
2461
+ source: () => queueKeyFn?.(),
2462
+ computation: () => signal([]),
2463
+ });
2459
2464
  let ctx = undefined;
2460
2465
  const queueRef = effect(() => {
2461
- const nextInQueue = queue().at(0);
2466
+ const q = queue(); // subscribe to swaps (key change / clearQueue)
2467
+ const nextInQueue = q().at(0); // subscribe to contents
2462
2468
  if (nextInQueue === undefined || next() !== NULL_VALUE)
2463
2469
  return;
2464
- queue.update((q) => q.slice(1));
2470
+ q.update((arr) => arr.slice(1));
2465
2471
  const [value, ictx] = nextInQueue;
2466
2472
  try {
2467
2473
  ctx = onMutate?.(value, ictx);
@@ -2473,7 +2479,7 @@ function mutationResource(request, options0 = {}) {
2473
2479
  if (isDevMode())
2474
2480
  console.error('[@mmstack/resource]: error thrown in onMutate hook, mutation was not applied', mutationErr);
2475
2481
  }
2476
- });
2482
+ }, { injector: options.injector });
2477
2483
  const req = computed(() => {
2478
2484
  const nr = next();
2479
2485
  if (nr === NULL_VALUE)
@@ -2525,9 +2531,13 @@ function mutationResource(request, options0 = {}) {
2525
2531
  const destroyRef = options.injector
2526
2532
  ? options.injector.get(DestroyRef)
2527
2533
  : inject(DestroyRef);
2528
- const error$ = toObservable(resource.error);
2529
- const value$ = toObservable(resource.value).pipe(catchError(() => of(NULL_VALUE)));
2530
- const statusSub = toObservable(resource.status)
2534
+ const error$ = toObservable(resource.error, { injector: options.injector });
2535
+ const value$ = toObservable(resource.value, {
2536
+ injector: options.injector,
2537
+ }).pipe(catchError(() => of(NULL_VALUE)));
2538
+ const statusSub = toObservable(resource.status, {
2539
+ injector: options.injector,
2540
+ })
2531
2541
  .pipe(combineLatestWith(error$, value$), map(([status, error, value]) => {
2532
2542
  if (status === ResourceStatus.Error && error) {
2533
2543
  return {
@@ -2563,7 +2573,6 @@ function mutationResource(request, options0 = {}) {
2563
2573
  ctx = undefined;
2564
2574
  next.set(NULL_VALUE);
2565
2575
  });
2566
- const shouldQueue = options.queue ?? false;
2567
2576
  const ref = {
2568
2577
  ...resource,
2569
2578
  destroy: () => {
@@ -2573,8 +2582,8 @@ function mutationResource(request, options0 = {}) {
2573
2582
  resource.destroy();
2574
2583
  },
2575
2584
  mutate: (value, ictx) => {
2576
- if (shouldQueue) {
2577
- return queue.update((q) => [...q, [value, ictx]]);
2585
+ if (queueEnabled) {
2586
+ return queue().update((arr) => [...arr, [value, ictx]]);
2578
2587
  }
2579
2588
  else {
2580
2589
  // latest-wins: a mutation already in flight gets superseded (its request is
@@ -2608,6 +2617,11 @@ function mutationResource(request, options0 = {}) {
2608
2617
  const nv = next();
2609
2618
  return nv === NULL_VALUE ? null : nv;
2610
2619
  }),
2620
+ clearQueue: () => {
2621
+ if (!queueEnabled)
2622
+ return;
2623
+ queue.set(signal([]));
2624
+ },
2611
2625
  // redeclare disabled with last value so that it is not affected by the resource's internal disablement logic
2612
2626
  disabled: computed(() => cb.isOpen() || lastValueRequest() === undefined),
2613
2627
  };