@mmstack/resource 22.1.3 → 22.1.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.
@@ -2471,14 +2471,17 @@ function mutationResource(request, options0 = {}) {
2471
2471
  return false;
2472
2472
  return eq(a, b);
2473
2473
  } });
2474
- const queue = signal([], /* @ts-ignore */
2475
- ...(ngDevMode ? [{ debugName: "queue" }] : /* istanbul ignore next */ []));
2474
+ const queueEnabled = !!options.queue;
2475
+ const queueKeyFn = typeof options.queue === 'object' ? options.queue.key : undefined;
2476
+ const queue = linkedSignal({ ...(ngDevMode ? { debugName: "queue" } : /* istanbul ignore next */ {}), source: () => queueKeyFn?.(),
2477
+ computation: () => signal([]) });
2476
2478
  let ctx = undefined;
2477
2479
  const queueRef = effect(() => {
2478
- const nextInQueue = queue().at(0);
2480
+ const q = queue(); // subscribe to swaps (key change / clearQueue)
2481
+ const nextInQueue = q().at(0); // subscribe to contents
2479
2482
  if (nextInQueue === undefined || next() !== NULL_VALUE)
2480
2483
  return;
2481
- queue.update((q) => q.slice(1));
2484
+ q.update((arr) => arr.slice(1));
2482
2485
  const [value, ictx] = nextInQueue;
2483
2486
  try {
2484
2487
  ctx = onMutate?.(value, ictx);
@@ -2490,8 +2493,7 @@ function mutationResource(request, options0 = {}) {
2490
2493
  if (isDevMode())
2491
2494
  console.error('[@mmstack/resource]: error thrown in onMutate hook, mutation was not applied', mutationErr);
2492
2495
  }
2493
- }, /* @ts-ignore */
2494
- ...(ngDevMode ? [{ debugName: "queueRef" }] : /* istanbul ignore next */ []));
2496
+ }, { ...(ngDevMode ? { debugName: "queueRef" } : /* istanbul ignore next */ {}), injector: options.injector });
2495
2497
  const req = computed(() => {
2496
2498
  const nr = next();
2497
2499
  if (nr === NULL_VALUE)
@@ -2537,9 +2539,13 @@ function mutationResource(request, options0 = {}) {
2537
2539
  const destroyRef = options.injector
2538
2540
  ? options.injector.get(DestroyRef)
2539
2541
  : inject(DestroyRef);
2540
- const error$ = toObservable(resource.error);
2541
- const value$ = toObservable(resource.value).pipe(catchError(() => of(NULL_VALUE)));
2542
- const statusSub = toObservable(resource.status)
2542
+ const error$ = toObservable(resource.error, { injector: options.injector });
2543
+ const value$ = toObservable(resource.value, {
2544
+ injector: options.injector,
2545
+ }).pipe(catchError(() => of(NULL_VALUE)));
2546
+ const statusSub = toObservable(resource.status, {
2547
+ injector: options.injector,
2548
+ })
2543
2549
  .pipe(combineLatestWith(error$, value$), map(([status, error, value]) => {
2544
2550
  if (status === 'error' && error) {
2545
2551
  return {
@@ -2575,7 +2581,6 @@ function mutationResource(request, options0 = {}) {
2575
2581
  ctx = undefined;
2576
2582
  next.set(NULL_VALUE);
2577
2583
  });
2578
- const shouldQueue = options.queue ?? false;
2579
2584
  const ref = {
2580
2585
  ...resource,
2581
2586
  destroy: () => {
@@ -2585,8 +2590,8 @@ function mutationResource(request, options0 = {}) {
2585
2590
  resource.destroy();
2586
2591
  },
2587
2592
  mutate: (value, ictx) => {
2588
- if (shouldQueue) {
2589
- return queue.update((q) => [...q, [value, ictx]]);
2593
+ if (queueEnabled) {
2594
+ return queue().update((arr) => [...arr, [value, ictx]]);
2590
2595
  }
2591
2596
  else {
2592
2597
  // latest-wins: a mutation already in flight gets superseded (its request is
@@ -2620,6 +2625,11 @@ function mutationResource(request, options0 = {}) {
2620
2625
  const nv = next();
2621
2626
  return nv === NULL_VALUE ? null : nv;
2622
2627
  }),
2628
+ clearQueue: () => {
2629
+ if (!queueEnabled)
2630
+ return;
2631
+ queue.set(signal([]));
2632
+ },
2623
2633
  // redeclare disabled with last value so that it is not affected by the resource's internal disablement logic
2624
2634
  disabled: computed(() => cb.isOpen() || lastValueRequest() === undefined),
2625
2635
  };