reactjrx 1.58.0 → 1.60.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/dist/index.cjs CHANGED
@@ -1520,7 +1520,9 @@ const createMutationRunner = ({
1520
1520
  mutationsRunning$.next(mutationsRunning$.getValue() + 1);
1521
1521
  }),
1522
1522
  switchOperator(({ args, options }) => {
1523
- const queryRunner$ = rxjs.defer(() => rxjs.from(options.mutationFn(args))).pipe(
1523
+ const mutationFn = options.mutationFn;
1524
+ const mutationFnObservable = typeof mutationFn === "function" ? rxjs.defer(() => rxjs.from(mutationFn(args))) : mutationFn;
1525
+ const queryRunner$ = mutationFnObservable.pipe(
1524
1526
  retryOnError(options),
1525
1527
  rxjs.take(1),
1526
1528
  rxjs.map((data) => ({ data, isError: false })),
@@ -1619,6 +1621,10 @@ class MutationClient {
1619
1621
  __publicField(this, "mutationResults$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
1620
1622
  __publicField(this, "mutate$", new rxjs.Subject());
1621
1623
  __publicField(this, "reset$", new rxjs.Subject());
1624
+ /**
1625
+ * Observable to track how many running mutations per runner
1626
+ */
1627
+ __publicField(this, "isMutatingSubject", new rxjs.BehaviorSubject([]));
1622
1628
  this.mutate$.pipe(
1623
1629
  rxjs.tap(({ options, args }) => {
1624
1630
  const { mutationKey } = options;
@@ -1664,6 +1670,21 @@ class MutationClient {
1664
1670
  (_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
1665
1671
  })
1666
1672
  ).subscribe();
1673
+ this.mutationRunnersByKey$.pipe(
1674
+ rxjs.switchMap((mapItem) => {
1675
+ const mutationRunners = Array.from(mapItem.entries()).map(
1676
+ ([, value]) => value.mutationsRunning$.pipe(
1677
+ rxjs.map((number) => [value.mutationKey, number])
1678
+ )
1679
+ );
1680
+ const mutationRunnersMutationsRunning$ = rxjs.combineLatest([
1681
+ // when map is empty we still need to push 0
1682
+ rxjs.of([[], 0]),
1683
+ ...mutationRunners
1684
+ ]);
1685
+ return mutationRunnersMutationsRunning$;
1686
+ })
1687
+ ).subscribe(this.isMutatingSubject);
1667
1688
  }
1668
1689
  /**
1669
1690
  * @helper
@@ -1687,36 +1708,18 @@ class MutationClient {
1687
1708
  getMutationRunnersByKey(key) {
1688
1709
  return this.mutationRunnersByKey$.getValue().get(key);
1689
1710
  }
1690
- /**
1691
- * @returns number of mutation runnings
1692
- */
1693
- runningMutations({ mutationKey, predicate } = {}) {
1711
+ useIsMutating({ mutationKey, predicate } = {}) {
1694
1712
  const defaultPredicate = ({ options }) => mutationKey ? (
1695
1713
  // @todo optimize
1696
1714
  serializeKey(options.mutationKey) === serializeKey(mutationKey)
1697
1715
  ) : true;
1698
1716
  const finalPredicate = predicate ?? defaultPredicate;
1699
- const mutationRunnersByKeyEntries = Array.from(
1700
- this.mutationRunnersByKey$.getValue().entries()
1701
- );
1702
- const lastValue = mutationRunnersByKeyEntries.filter(
1703
- ([, value]) => finalPredicate({ options: { mutationKey: value.mutationKey } })
1704
- ).reduce((acc, [, value]) => acc + value.mutationsRunning$.getValue(), 0);
1705
- const value$ = this.mutationRunnersByKey$.pipe(
1706
- rxjs.switchMap((map2) => {
1707
- const mutationRunners = Array.from(map2.entries()).filter(
1708
- ([, value]) => finalPredicate({ options: { mutationKey: value.mutationKey } })
1709
- ).map(([, value]) => value);
1710
- const mutationRunnersMutationsRunning$ = rxjs.combineLatest([
1711
- // when map is empty we still need to push 0
1712
- rxjs.of(0),
1713
- ...mutationRunners.map(
1714
- (mutationRunner) => mutationRunner.mutationsRunning$
1715
- )
1716
- ]);
1717
- return mutationRunnersMutationsRunning$;
1718
- }),
1719
- rxjs.map((values) => values.reduce((acc, value) => value + acc, 0)),
1717
+ const reduceByNumber = (entries) => entries.reduce((acc, [mutationKey2, value]) => {
1718
+ return finalPredicate({ options: { mutationKey: mutationKey2 } }) ? value + acc : acc;
1719
+ }, 0);
1720
+ const lastValue = reduceByNumber(this.isMutatingSubject.getValue());
1721
+ const value$ = this.isMutatingSubject.pipe(
1722
+ rxjs.map((mutationRunningByKeys) => reduceByNumber(mutationRunningByKeys)),
1720
1723
  rxjs.distinctUntilChanged()
1721
1724
  );
1722
1725
  return { value$, lastValue };
@@ -1770,6 +1773,7 @@ class MutationClient {
1770
1773
  this.mutate$.complete();
1771
1774
  this.mutationResults$.complete();
1772
1775
  this.mutationRunnersByKey$.complete();
1776
+ this.isMutatingSubject.complete();
1773
1777
  }
1774
1778
  }
1775
1779
  const createClient = () => {
package/dist/index.js CHANGED
@@ -1518,7 +1518,9 @@ const createMutationRunner = ({
1518
1518
  mutationsRunning$.next(mutationsRunning$.getValue() + 1);
1519
1519
  }),
1520
1520
  switchOperator(({ args, options }) => {
1521
- const queryRunner$ = defer(() => from(options.mutationFn(args))).pipe(
1521
+ const mutationFn = options.mutationFn;
1522
+ const mutationFnObservable = typeof mutationFn === "function" ? defer(() => from(mutationFn(args))) : mutationFn;
1523
+ const queryRunner$ = mutationFnObservable.pipe(
1522
1524
  retryOnError(options),
1523
1525
  take(1),
1524
1526
  map((data) => ({ data, isError: false })),
@@ -1617,6 +1619,10 @@ class MutationClient {
1617
1619
  __publicField(this, "mutationResults$", new BehaviorSubject(/* @__PURE__ */ new Map()));
1618
1620
  __publicField(this, "mutate$", new Subject());
1619
1621
  __publicField(this, "reset$", new Subject());
1622
+ /**
1623
+ * Observable to track how many running mutations per runner
1624
+ */
1625
+ __publicField(this, "isMutatingSubject", new BehaviorSubject([]));
1620
1626
  this.mutate$.pipe(
1621
1627
  tap(({ options, args }) => {
1622
1628
  const { mutationKey } = options;
@@ -1662,6 +1668,21 @@ class MutationClient {
1662
1668
  (_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
1663
1669
  })
1664
1670
  ).subscribe();
1671
+ this.mutationRunnersByKey$.pipe(
1672
+ switchMap((mapItem) => {
1673
+ const mutationRunners = Array.from(mapItem.entries()).map(
1674
+ ([, value]) => value.mutationsRunning$.pipe(
1675
+ map((number) => [value.mutationKey, number])
1676
+ )
1677
+ );
1678
+ const mutationRunnersMutationsRunning$ = combineLatest([
1679
+ // when map is empty we still need to push 0
1680
+ of([[], 0]),
1681
+ ...mutationRunners
1682
+ ]);
1683
+ return mutationRunnersMutationsRunning$;
1684
+ })
1685
+ ).subscribe(this.isMutatingSubject);
1665
1686
  }
1666
1687
  /**
1667
1688
  * @helper
@@ -1685,36 +1706,18 @@ class MutationClient {
1685
1706
  getMutationRunnersByKey(key) {
1686
1707
  return this.mutationRunnersByKey$.getValue().get(key);
1687
1708
  }
1688
- /**
1689
- * @returns number of mutation runnings
1690
- */
1691
- runningMutations({ mutationKey, predicate } = {}) {
1709
+ useIsMutating({ mutationKey, predicate } = {}) {
1692
1710
  const defaultPredicate = ({ options }) => mutationKey ? (
1693
1711
  // @todo optimize
1694
1712
  serializeKey(options.mutationKey) === serializeKey(mutationKey)
1695
1713
  ) : true;
1696
1714
  const finalPredicate = predicate ?? defaultPredicate;
1697
- const mutationRunnersByKeyEntries = Array.from(
1698
- this.mutationRunnersByKey$.getValue().entries()
1699
- );
1700
- const lastValue = mutationRunnersByKeyEntries.filter(
1701
- ([, value]) => finalPredicate({ options: { mutationKey: value.mutationKey } })
1702
- ).reduce((acc, [, value]) => acc + value.mutationsRunning$.getValue(), 0);
1703
- const value$ = this.mutationRunnersByKey$.pipe(
1704
- switchMap((map2) => {
1705
- const mutationRunners = Array.from(map2.entries()).filter(
1706
- ([, value]) => finalPredicate({ options: { mutationKey: value.mutationKey } })
1707
- ).map(([, value]) => value);
1708
- const mutationRunnersMutationsRunning$ = combineLatest([
1709
- // when map is empty we still need to push 0
1710
- of(0),
1711
- ...mutationRunners.map(
1712
- (mutationRunner) => mutationRunner.mutationsRunning$
1713
- )
1714
- ]);
1715
- return mutationRunnersMutationsRunning$;
1716
- }),
1717
- map((values) => values.reduce((acc, value) => value + acc, 0)),
1715
+ const reduceByNumber = (entries) => entries.reduce((acc, [mutationKey2, value]) => {
1716
+ return finalPredicate({ options: { mutationKey: mutationKey2 } }) ? value + acc : acc;
1717
+ }, 0);
1718
+ const lastValue = reduceByNumber(this.isMutatingSubject.getValue());
1719
+ const value$ = this.isMutatingSubject.pipe(
1720
+ map((mutationRunningByKeys) => reduceByNumber(mutationRunningByKeys)),
1718
1721
  distinctUntilChanged()
1719
1722
  );
1720
1723
  return { value$, lastValue };
@@ -1768,6 +1771,7 @@ class MutationClient {
1768
1771
  this.mutate$.complete();
1769
1772
  this.mutationResults$.complete();
1770
1773
  this.mutationRunnersByKey$.complete();
1774
+ this.isMutatingSubject.complete();
1771
1775
  }
1772
1776
  }
1773
1777
  const createClient = () => {
@@ -37,6 +37,10 @@ export declare class MutationClient {
37
37
  reset$: Subject<{
38
38
  key: QueryKey;
39
39
  }>;
40
+ /**
41
+ * Observable to track how many running mutations per runner
42
+ */
43
+ isMutatingSubject: BehaviorSubject<(readonly [MutationKey, number])[]>;
40
44
  constructor();
41
45
  /**
42
46
  * @helper
@@ -62,10 +66,7 @@ export declare class MutationClient {
62
66
  } & {
63
67
  mutationKey: MutationKey;
64
68
  }) | undefined;
65
- /**
66
- * @returns number of mutation runnings
67
- */
68
- runningMutations({ mutationKey, predicate }?: MutationFilters): {
69
+ useIsMutating({ mutationKey, predicate }?: MutationFilters): {
69
70
  value$: Observable<number>;
70
71
  lastValue: number;
71
72
  };
@@ -22,6 +22,9 @@ import { type Query, type QueryResult } from "../types";
22
22
  export type MapOperator = "switch" | "concat" | "merge";
23
23
  export type MutationStatus = "idle" | "pending" | "success" | "error";
24
24
  export type MutationKey = unknown[];
25
+ /**
26
+ * @todo this should be used in a lot of place so we can probably make a helper for that
27
+ */
25
28
  export interface MutationFilters {
26
29
  /**
27
30
  * Match mutation key exactly
@@ -56,7 +59,7 @@ export interface MutationObservedResult<R> extends MutationResult<R> {
56
59
  isPending: boolean;
57
60
  isPaused: boolean;
58
61
  }
59
- export type MutationFn<T, MutationArg> = ((arg: MutationArg) => Promise<T>) | ((arg: MutationArg) => Observable<T>);
62
+ export type MutationFn<T, MutationArg> = Observable<T> | ((arg: MutationArg) => Promise<T>) | ((arg: MutationArg) => Observable<T>);
60
63
  export interface MutationOptions<Result, MutationArg> {
61
64
  enabled?: boolean;
62
65
  retry?: false | number | ((attempt: number, error: unknown) => boolean);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "reactjrx",
3
3
  "private": false,
4
- "version": "1.58.0",
4
+ "version": "1.60.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"