reactjrx 1.57.0 → 1.59.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 +90 -24
- package/dist/index.js +90 -24
- package/dist/lib/queries/client/keys/serializeKey.d.ts +4 -0
- package/dist/lib/queries/client/mutations/MutationClient.d.ts +35 -2
- package/dist/lib/queries/client/mutations/types.d.ts +30 -3
- package/dist/lib/queries/react/Provider.d.ts +3 -1
- package/dist/lib/queries/react/mutations/useMutation.d.ts +5 -5
- package/dist/lib/queries/react/mutations/useMutationState.d.ts +3 -0
- package/dist/lib/queries/react/mutations/useMutationState.test.d.ts +1 -0
- package/dist/tests/utils.d.ts +20 -0
- package/package.json +1 -1
- package/dist/lib/queries/react/keys/serializeKey.d.ts +0 -3
package/dist/index.cjs
CHANGED
|
@@ -421,9 +421,9 @@ const QueryClientProvider = react.memo(
|
|
|
421
421
|
] });
|
|
422
422
|
}
|
|
423
423
|
);
|
|
424
|
-
const useQueryClient = () => {
|
|
424
|
+
const useQueryClient = ({ unsafe = false } = {}) => {
|
|
425
425
|
const context = react.useContext(Context);
|
|
426
|
-
if (context === null) {
|
|
426
|
+
if (!unsafe && context.client === null) {
|
|
427
427
|
throw new Error("You forgot to register the provider");
|
|
428
428
|
}
|
|
429
429
|
return context.client;
|
|
@@ -458,34 +458,38 @@ const nanoid = (size = 21) => crypto.getRandomValues(new Uint8Array(size)).reduc
|
|
|
458
458
|
}
|
|
459
459
|
return id;
|
|
460
460
|
}, "");
|
|
461
|
-
function useMutation(options) {
|
|
462
|
-
const
|
|
461
|
+
function useMutation(options, queryClient) {
|
|
462
|
+
const defaultQueryClient = useQueryClient({ unsafe: !!queryClient });
|
|
463
|
+
const finalQueryClient = (queryClient == null ? void 0 : queryClient.client) ?? defaultQueryClient;
|
|
463
464
|
const optionsRef = useLiveRef(options);
|
|
464
465
|
const defaultKey = useConstant(() => [nanoid()]);
|
|
465
466
|
const key = serializeKey(options.mutationKey ?? defaultKey.current);
|
|
466
467
|
const observedMutation = react.useMemo(
|
|
467
|
-
() =>
|
|
468
|
+
() => finalQueryClient.mutationClient.observe({ key }),
|
|
468
469
|
[key]
|
|
469
470
|
);
|
|
470
471
|
const result = useObserve(observedMutation.result$) ?? observedMutation.lastValue;
|
|
471
472
|
const mutate = react.useCallback(
|
|
472
473
|
(mutationArgs) => {
|
|
473
|
-
|
|
474
|
-
options: {
|
|
474
|
+
finalQueryClient.mutationClient.mutate({
|
|
475
|
+
options: {
|
|
476
|
+
...optionsRef.current,
|
|
477
|
+
mutationKey: optionsRef.current.mutationKey ?? defaultKey.current
|
|
478
|
+
},
|
|
475
479
|
args: mutationArgs
|
|
476
480
|
});
|
|
477
481
|
},
|
|
478
|
-
[
|
|
482
|
+
[finalQueryClient, key]
|
|
479
483
|
);
|
|
480
484
|
const reset = react.useCallback(() => {
|
|
481
|
-
|
|
485
|
+
finalQueryClient.mutationClient.reset({
|
|
482
486
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
483
487
|
});
|
|
484
|
-
}, [
|
|
488
|
+
}, [finalQueryClient]);
|
|
485
489
|
react.useEffect(() => {
|
|
486
490
|
return () => {
|
|
487
491
|
if (optionsRef.current.cancelOnUnMount) {
|
|
488
|
-
|
|
492
|
+
finalQueryClient.mutationClient.reset({
|
|
489
493
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
490
494
|
});
|
|
491
495
|
}
|
|
@@ -1516,7 +1520,9 @@ const createMutationRunner = ({
|
|
|
1516
1520
|
mutationsRunning$.next(mutationsRunning$.getValue() + 1);
|
|
1517
1521
|
}),
|
|
1518
1522
|
switchOperator(({ args, options }) => {
|
|
1519
|
-
const
|
|
1523
|
+
const mutationFn = options.mutationFn;
|
|
1524
|
+
const mutationFnObservable = typeof mutationFn === "function" ? rxjs.defer(() => rxjs.from(mutationFn(args))) : mutationFn;
|
|
1525
|
+
const queryRunner$ = mutationFnObservable.pipe(
|
|
1520
1526
|
retryOnError(options),
|
|
1521
1527
|
rxjs.take(1),
|
|
1522
1528
|
rxjs.map((data) => ({ data, isError: false })),
|
|
@@ -1604,7 +1610,7 @@ class MutationClient {
|
|
|
1604
1610
|
* @important
|
|
1605
1611
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1606
1612
|
*/
|
|
1607
|
-
__publicField(this, "
|
|
1613
|
+
__publicField(this, "mutationRunnersByKey$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1608
1614
|
/**
|
|
1609
1615
|
* Mutation result subject. It can be used whether there is a mutation
|
|
1610
1616
|
* running or not and can be directly observed.
|
|
@@ -1618,16 +1624,22 @@ class MutationClient {
|
|
|
1618
1624
|
this.mutate$.pipe(
|
|
1619
1625
|
rxjs.tap(({ options, args }) => {
|
|
1620
1626
|
const { mutationKey } = options;
|
|
1621
|
-
|
|
1627
|
+
const serializedMutationKey = serializeKey(mutationKey);
|
|
1628
|
+
let mutationForKey = this.getMutationRunnersByKey(
|
|
1629
|
+
serializedMutationKey
|
|
1630
|
+
);
|
|
1622
1631
|
if (!mutationForKey) {
|
|
1623
|
-
mutationForKey =
|
|
1624
|
-
|
|
1632
|
+
mutationForKey = {
|
|
1633
|
+
...createMutationRunner(options),
|
|
1634
|
+
mutationKey
|
|
1635
|
+
};
|
|
1636
|
+
this.setMutationRunnersByKey(serializedMutationKey, mutationForKey);
|
|
1625
1637
|
mutationForKey.mutation$.subscribe((result) => {
|
|
1626
|
-
let resultForKeySubject = this.mutationResults$.getValue().get(
|
|
1638
|
+
let resultForKeySubject = this.mutationResults$.getValue().get(serializedMutationKey);
|
|
1627
1639
|
if (!resultForKeySubject) {
|
|
1628
1640
|
resultForKeySubject = new rxjs.BehaviorSubject(result);
|
|
1629
1641
|
const resultMap = this.mutationResults$.getValue();
|
|
1630
|
-
resultMap.set(
|
|
1642
|
+
resultMap.set(serializedMutationKey, resultForKeySubject);
|
|
1631
1643
|
this.mutationResults$.next(resultMap);
|
|
1632
1644
|
} else {
|
|
1633
1645
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
@@ -1639,11 +1651,9 @@ class MutationClient {
|
|
|
1639
1651
|
).subscribe(() => {
|
|
1640
1652
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1641
1653
|
const resultMap = this.mutationResults$.getValue();
|
|
1642
|
-
resultMap.delete(
|
|
1654
|
+
resultMap.delete(serializedMutationKey);
|
|
1643
1655
|
this.mutationResults$.next(resultMap);
|
|
1644
|
-
|
|
1645
|
-
mutationsMap.delete(mutationKey);
|
|
1646
|
-
this.mutationRunners$.next(mutationsMap);
|
|
1656
|
+
this.deleteMutationRunnersByKey(serializedMutationKey);
|
|
1647
1657
|
});
|
|
1648
1658
|
}
|
|
1649
1659
|
mutationForKey.trigger({ args, options });
|
|
@@ -1653,10 +1663,66 @@ class MutationClient {
|
|
|
1653
1663
|
rxjs.tap(({ key }) => {
|
|
1654
1664
|
var _a;
|
|
1655
1665
|
const serializedKey = serializeKey(key);
|
|
1656
|
-
(_a = this.
|
|
1666
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
|
|
1657
1667
|
})
|
|
1658
1668
|
).subscribe();
|
|
1659
1669
|
}
|
|
1670
|
+
/**
|
|
1671
|
+
* @helper
|
|
1672
|
+
*/
|
|
1673
|
+
setMutationRunnersByKey(key, value) {
|
|
1674
|
+
const map2 = this.mutationRunnersByKey$.getValue();
|
|
1675
|
+
map2.set(key, value);
|
|
1676
|
+
this.mutationRunnersByKey$.next(map2);
|
|
1677
|
+
}
|
|
1678
|
+
/**
|
|
1679
|
+
* @helper
|
|
1680
|
+
*/
|
|
1681
|
+
deleteMutationRunnersByKey(key) {
|
|
1682
|
+
const map2 = this.mutationRunnersByKey$.getValue();
|
|
1683
|
+
map2.delete(key);
|
|
1684
|
+
this.mutationRunnersByKey$.next(map2);
|
|
1685
|
+
}
|
|
1686
|
+
/**
|
|
1687
|
+
* @helper
|
|
1688
|
+
*/
|
|
1689
|
+
getMutationRunnersByKey(key) {
|
|
1690
|
+
return this.mutationRunnersByKey$.getValue().get(key);
|
|
1691
|
+
}
|
|
1692
|
+
/**
|
|
1693
|
+
* @returns number of mutation runnings
|
|
1694
|
+
*/
|
|
1695
|
+
runningMutations({ mutationKey, predicate } = {}) {
|
|
1696
|
+
const defaultPredicate = ({ options }) => mutationKey ? (
|
|
1697
|
+
// @todo optimize
|
|
1698
|
+
serializeKey(options.mutationKey) === serializeKey(mutationKey)
|
|
1699
|
+
) : true;
|
|
1700
|
+
const finalPredicate = predicate ?? defaultPredicate;
|
|
1701
|
+
const mutationRunnersByKeyEntries = Array.from(
|
|
1702
|
+
this.mutationRunnersByKey$.getValue().entries()
|
|
1703
|
+
);
|
|
1704
|
+
const lastValue = mutationRunnersByKeyEntries.filter(
|
|
1705
|
+
([, value]) => finalPredicate({ options: { mutationKey: value.mutationKey } })
|
|
1706
|
+
).reduce((acc, [, value]) => acc + value.mutationsRunning$.getValue(), 0);
|
|
1707
|
+
const value$ = this.mutationRunnersByKey$.pipe(
|
|
1708
|
+
rxjs.switchMap((map2) => {
|
|
1709
|
+
const mutationRunners = Array.from(map2.entries()).filter(
|
|
1710
|
+
([, value]) => finalPredicate({ options: { mutationKey: value.mutationKey } })
|
|
1711
|
+
).map(([, value]) => value);
|
|
1712
|
+
const mutationRunnersMutationsRunning$ = rxjs.combineLatest([
|
|
1713
|
+
// when map is empty we still need to push 0
|
|
1714
|
+
rxjs.of(0),
|
|
1715
|
+
...mutationRunners.map(
|
|
1716
|
+
(mutationRunner) => mutationRunner.mutationsRunning$
|
|
1717
|
+
)
|
|
1718
|
+
]);
|
|
1719
|
+
return mutationRunnersMutationsRunning$;
|
|
1720
|
+
}),
|
|
1721
|
+
rxjs.map((values) => values.reduce((acc, value) => value + acc, 0)),
|
|
1722
|
+
rxjs.distinctUntilChanged()
|
|
1723
|
+
);
|
|
1724
|
+
return { value$, lastValue };
|
|
1725
|
+
}
|
|
1660
1726
|
observe({ key }) {
|
|
1661
1727
|
var _a;
|
|
1662
1728
|
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
@@ -1705,7 +1771,7 @@ class MutationClient {
|
|
|
1705
1771
|
this.reset$.complete();
|
|
1706
1772
|
this.mutate$.complete();
|
|
1707
1773
|
this.mutationResults$.complete();
|
|
1708
|
-
this.
|
|
1774
|
+
this.mutationRunnersByKey$.complete();
|
|
1709
1775
|
}
|
|
1710
1776
|
}
|
|
1711
1777
|
const createClient = () => {
|
package/dist/index.js
CHANGED
|
@@ -419,9 +419,9 @@ const QueryClientProvider = memo(
|
|
|
419
419
|
] });
|
|
420
420
|
}
|
|
421
421
|
);
|
|
422
|
-
const useQueryClient = () => {
|
|
422
|
+
const useQueryClient = ({ unsafe = false } = {}) => {
|
|
423
423
|
const context = useContext(Context);
|
|
424
|
-
if (context === null) {
|
|
424
|
+
if (!unsafe && context.client === null) {
|
|
425
425
|
throw new Error("You forgot to register the provider");
|
|
426
426
|
}
|
|
427
427
|
return context.client;
|
|
@@ -456,34 +456,38 @@ const nanoid = (size = 21) => crypto.getRandomValues(new Uint8Array(size)).reduc
|
|
|
456
456
|
}
|
|
457
457
|
return id;
|
|
458
458
|
}, "");
|
|
459
|
-
function useMutation(options) {
|
|
460
|
-
const
|
|
459
|
+
function useMutation(options, queryClient) {
|
|
460
|
+
const defaultQueryClient = useQueryClient({ unsafe: !!queryClient });
|
|
461
|
+
const finalQueryClient = (queryClient == null ? void 0 : queryClient.client) ?? defaultQueryClient;
|
|
461
462
|
const optionsRef = useLiveRef(options);
|
|
462
463
|
const defaultKey = useConstant(() => [nanoid()]);
|
|
463
464
|
const key = serializeKey(options.mutationKey ?? defaultKey.current);
|
|
464
465
|
const observedMutation = useMemo(
|
|
465
|
-
() =>
|
|
466
|
+
() => finalQueryClient.mutationClient.observe({ key }),
|
|
466
467
|
[key]
|
|
467
468
|
);
|
|
468
469
|
const result = useObserve(observedMutation.result$) ?? observedMutation.lastValue;
|
|
469
470
|
const mutate = useCallback(
|
|
470
471
|
(mutationArgs) => {
|
|
471
|
-
|
|
472
|
-
options: {
|
|
472
|
+
finalQueryClient.mutationClient.mutate({
|
|
473
|
+
options: {
|
|
474
|
+
...optionsRef.current,
|
|
475
|
+
mutationKey: optionsRef.current.mutationKey ?? defaultKey.current
|
|
476
|
+
},
|
|
473
477
|
args: mutationArgs
|
|
474
478
|
});
|
|
475
479
|
},
|
|
476
|
-
[
|
|
480
|
+
[finalQueryClient, key]
|
|
477
481
|
);
|
|
478
482
|
const reset = useCallback(() => {
|
|
479
|
-
|
|
483
|
+
finalQueryClient.mutationClient.reset({
|
|
480
484
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
481
485
|
});
|
|
482
|
-
}, [
|
|
486
|
+
}, [finalQueryClient]);
|
|
483
487
|
useEffect(() => {
|
|
484
488
|
return () => {
|
|
485
489
|
if (optionsRef.current.cancelOnUnMount) {
|
|
486
|
-
|
|
490
|
+
finalQueryClient.mutationClient.reset({
|
|
487
491
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
488
492
|
});
|
|
489
493
|
}
|
|
@@ -1514,7 +1518,9 @@ const createMutationRunner = ({
|
|
|
1514
1518
|
mutationsRunning$.next(mutationsRunning$.getValue() + 1);
|
|
1515
1519
|
}),
|
|
1516
1520
|
switchOperator(({ args, options }) => {
|
|
1517
|
-
const
|
|
1521
|
+
const mutationFn = options.mutationFn;
|
|
1522
|
+
const mutationFnObservable = typeof mutationFn === "function" ? defer(() => from(mutationFn(args))) : mutationFn;
|
|
1523
|
+
const queryRunner$ = mutationFnObservable.pipe(
|
|
1518
1524
|
retryOnError(options),
|
|
1519
1525
|
take(1),
|
|
1520
1526
|
map((data) => ({ data, isError: false })),
|
|
@@ -1602,7 +1608,7 @@ class MutationClient {
|
|
|
1602
1608
|
* @important
|
|
1603
1609
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1604
1610
|
*/
|
|
1605
|
-
__publicField(this, "
|
|
1611
|
+
__publicField(this, "mutationRunnersByKey$", new BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1606
1612
|
/**
|
|
1607
1613
|
* Mutation result subject. It can be used whether there is a mutation
|
|
1608
1614
|
* running or not and can be directly observed.
|
|
@@ -1616,16 +1622,22 @@ class MutationClient {
|
|
|
1616
1622
|
this.mutate$.pipe(
|
|
1617
1623
|
tap(({ options, args }) => {
|
|
1618
1624
|
const { mutationKey } = options;
|
|
1619
|
-
|
|
1625
|
+
const serializedMutationKey = serializeKey(mutationKey);
|
|
1626
|
+
let mutationForKey = this.getMutationRunnersByKey(
|
|
1627
|
+
serializedMutationKey
|
|
1628
|
+
);
|
|
1620
1629
|
if (!mutationForKey) {
|
|
1621
|
-
mutationForKey =
|
|
1622
|
-
|
|
1630
|
+
mutationForKey = {
|
|
1631
|
+
...createMutationRunner(options),
|
|
1632
|
+
mutationKey
|
|
1633
|
+
};
|
|
1634
|
+
this.setMutationRunnersByKey(serializedMutationKey, mutationForKey);
|
|
1623
1635
|
mutationForKey.mutation$.subscribe((result) => {
|
|
1624
|
-
let resultForKeySubject = this.mutationResults$.getValue().get(
|
|
1636
|
+
let resultForKeySubject = this.mutationResults$.getValue().get(serializedMutationKey);
|
|
1625
1637
|
if (!resultForKeySubject) {
|
|
1626
1638
|
resultForKeySubject = new BehaviorSubject(result);
|
|
1627
1639
|
const resultMap = this.mutationResults$.getValue();
|
|
1628
|
-
resultMap.set(
|
|
1640
|
+
resultMap.set(serializedMutationKey, resultForKeySubject);
|
|
1629
1641
|
this.mutationResults$.next(resultMap);
|
|
1630
1642
|
} else {
|
|
1631
1643
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
@@ -1637,11 +1649,9 @@ class MutationClient {
|
|
|
1637
1649
|
).subscribe(() => {
|
|
1638
1650
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1639
1651
|
const resultMap = this.mutationResults$.getValue();
|
|
1640
|
-
resultMap.delete(
|
|
1652
|
+
resultMap.delete(serializedMutationKey);
|
|
1641
1653
|
this.mutationResults$.next(resultMap);
|
|
1642
|
-
|
|
1643
|
-
mutationsMap.delete(mutationKey);
|
|
1644
|
-
this.mutationRunners$.next(mutationsMap);
|
|
1654
|
+
this.deleteMutationRunnersByKey(serializedMutationKey);
|
|
1645
1655
|
});
|
|
1646
1656
|
}
|
|
1647
1657
|
mutationForKey.trigger({ args, options });
|
|
@@ -1651,10 +1661,66 @@ class MutationClient {
|
|
|
1651
1661
|
tap(({ key }) => {
|
|
1652
1662
|
var _a;
|
|
1653
1663
|
const serializedKey = serializeKey(key);
|
|
1654
|
-
(_a = this.
|
|
1664
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
|
|
1655
1665
|
})
|
|
1656
1666
|
).subscribe();
|
|
1657
1667
|
}
|
|
1668
|
+
/**
|
|
1669
|
+
* @helper
|
|
1670
|
+
*/
|
|
1671
|
+
setMutationRunnersByKey(key, value) {
|
|
1672
|
+
const map2 = this.mutationRunnersByKey$.getValue();
|
|
1673
|
+
map2.set(key, value);
|
|
1674
|
+
this.mutationRunnersByKey$.next(map2);
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* @helper
|
|
1678
|
+
*/
|
|
1679
|
+
deleteMutationRunnersByKey(key) {
|
|
1680
|
+
const map2 = this.mutationRunnersByKey$.getValue();
|
|
1681
|
+
map2.delete(key);
|
|
1682
|
+
this.mutationRunnersByKey$.next(map2);
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* @helper
|
|
1686
|
+
*/
|
|
1687
|
+
getMutationRunnersByKey(key) {
|
|
1688
|
+
return this.mutationRunnersByKey$.getValue().get(key);
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* @returns number of mutation runnings
|
|
1692
|
+
*/
|
|
1693
|
+
runningMutations({ mutationKey, predicate } = {}) {
|
|
1694
|
+
const defaultPredicate = ({ options }) => mutationKey ? (
|
|
1695
|
+
// @todo optimize
|
|
1696
|
+
serializeKey(options.mutationKey) === serializeKey(mutationKey)
|
|
1697
|
+
) : true;
|
|
1698
|
+
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
|
+
switchMap((map2) => {
|
|
1707
|
+
const mutationRunners = Array.from(map2.entries()).filter(
|
|
1708
|
+
([, value]) => finalPredicate({ options: { mutationKey: value.mutationKey } })
|
|
1709
|
+
).map(([, value]) => value);
|
|
1710
|
+
const mutationRunnersMutationsRunning$ = combineLatest([
|
|
1711
|
+
// when map is empty we still need to push 0
|
|
1712
|
+
of(0),
|
|
1713
|
+
...mutationRunners.map(
|
|
1714
|
+
(mutationRunner) => mutationRunner.mutationsRunning$
|
|
1715
|
+
)
|
|
1716
|
+
]);
|
|
1717
|
+
return mutationRunnersMutationsRunning$;
|
|
1718
|
+
}),
|
|
1719
|
+
map((values) => values.reduce((acc, value) => value + acc, 0)),
|
|
1720
|
+
distinctUntilChanged()
|
|
1721
|
+
);
|
|
1722
|
+
return { value$, lastValue };
|
|
1723
|
+
}
|
|
1658
1724
|
observe({ key }) {
|
|
1659
1725
|
var _a;
|
|
1660
1726
|
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
@@ -1703,7 +1769,7 @@ class MutationClient {
|
|
|
1703
1769
|
this.reset$.complete();
|
|
1704
1770
|
this.mutate$.complete();
|
|
1705
1771
|
this.mutationResults$.complete();
|
|
1706
|
-
this.
|
|
1772
|
+
this.mutationRunnersByKey$.complete();
|
|
1707
1773
|
}
|
|
1708
1774
|
}
|
|
1709
1775
|
const createClient = () => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Subject, BehaviorSubject, type Observable } from "rxjs";
|
|
2
|
-
import { type MutationResult, type MutationOptions, type MutationObservedResult } from "./types";
|
|
2
|
+
import { type MutationResult, type MutationOptions, type MutationObservedResult, type MutationFilters, type MutationKey } from "./types";
|
|
3
3
|
import { type QueryKey } from "../keys/types";
|
|
4
4
|
export declare class MutationClient {
|
|
5
5
|
/**
|
|
@@ -9,7 +9,7 @@ export declare class MutationClient {
|
|
|
9
9
|
* @important
|
|
10
10
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
mutationRunnersByKey$: BehaviorSubject<Map<string, {
|
|
13
13
|
mutation$: Observable<MutationResult<unknown>>;
|
|
14
14
|
trigger: ({ args, options }: {
|
|
15
15
|
args: unknown;
|
|
@@ -19,6 +19,8 @@ export declare class MutationClient {
|
|
|
19
19
|
destroy: () => void;
|
|
20
20
|
mutationsRunning$: BehaviorSubject<number>;
|
|
21
21
|
getClosed: () => boolean;
|
|
22
|
+
} & {
|
|
23
|
+
mutationKey: MutationKey;
|
|
22
24
|
}>>;
|
|
23
25
|
/**
|
|
24
26
|
* Mutation result subject. It can be used whether there is a mutation
|
|
@@ -36,6 +38,37 @@ export declare class MutationClient {
|
|
|
36
38
|
key: QueryKey;
|
|
37
39
|
}>;
|
|
38
40
|
constructor();
|
|
41
|
+
/**
|
|
42
|
+
* @helper
|
|
43
|
+
*/
|
|
44
|
+
setMutationRunnersByKey(key: string, value: any): void;
|
|
45
|
+
/**
|
|
46
|
+
* @helper
|
|
47
|
+
*/
|
|
48
|
+
deleteMutationRunnersByKey(key: string): void;
|
|
49
|
+
/**
|
|
50
|
+
* @helper
|
|
51
|
+
*/
|
|
52
|
+
getMutationRunnersByKey(key: string): ({
|
|
53
|
+
mutation$: Observable<MutationResult<unknown>>;
|
|
54
|
+
trigger: ({ args, options }: {
|
|
55
|
+
args: unknown;
|
|
56
|
+
options: MutationOptions<unknown, unknown>;
|
|
57
|
+
}) => void;
|
|
58
|
+
reset$: Subject<void>;
|
|
59
|
+
destroy: () => void;
|
|
60
|
+
mutationsRunning$: BehaviorSubject<number>;
|
|
61
|
+
getClosed: () => boolean;
|
|
62
|
+
} & {
|
|
63
|
+
mutationKey: MutationKey;
|
|
64
|
+
}) | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* @returns number of mutation runnings
|
|
67
|
+
*/
|
|
68
|
+
runningMutations({ mutationKey, predicate }?: MutationFilters): {
|
|
69
|
+
value$: Observable<number>;
|
|
70
|
+
lastValue: number;
|
|
71
|
+
};
|
|
39
72
|
observe<Result>({ key }: {
|
|
40
73
|
key: string;
|
|
41
74
|
}): {
|
|
@@ -20,9 +20,36 @@ import { type Query, type QueryResult } from "../types";
|
|
|
20
20
|
* Result correspond to the current running async query.
|
|
21
21
|
*/
|
|
22
22
|
export type MapOperator = "switch" | "concat" | "merge";
|
|
23
|
+
export type MutationStatus = "idle" | "pending" | "success" | "error";
|
|
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
|
+
*/
|
|
28
|
+
export interface MutationFilters {
|
|
29
|
+
/**
|
|
30
|
+
* Match mutation key exactly
|
|
31
|
+
*/
|
|
32
|
+
exact?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Include mutations matching this predicate function
|
|
35
|
+
*/
|
|
36
|
+
predicate?: (mutation: {
|
|
37
|
+
options: {
|
|
38
|
+
mutationKey: MutationKey;
|
|
39
|
+
};
|
|
40
|
+
}) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Include mutations matching this mutation key
|
|
43
|
+
*/
|
|
44
|
+
mutationKey?: MutationKey;
|
|
45
|
+
/**
|
|
46
|
+
* Filter by mutation status
|
|
47
|
+
*/
|
|
48
|
+
status?: MutationStatus;
|
|
49
|
+
}
|
|
23
50
|
export interface MutationResult<R> {
|
|
24
51
|
data: R | undefined;
|
|
25
|
-
status:
|
|
52
|
+
status: MutationStatus;
|
|
26
53
|
error: unknown;
|
|
27
54
|
}
|
|
28
55
|
export interface MutationObservedResult<R> extends MutationResult<R> {
|
|
@@ -32,7 +59,7 @@ export interface MutationObservedResult<R> extends MutationResult<R> {
|
|
|
32
59
|
isPending: boolean;
|
|
33
60
|
isPaused: boolean;
|
|
34
61
|
}
|
|
35
|
-
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>);
|
|
36
63
|
export interface MutationOptions<Result, MutationArg> {
|
|
37
64
|
enabled?: boolean;
|
|
38
65
|
retry?: false | number | ((attempt: number, error: unknown) => boolean);
|
|
@@ -56,7 +83,7 @@ export interface MutationOptions<Result, MutationArg> {
|
|
|
56
83
|
onError?: (error: unknown, arg: MutationArg) => void;
|
|
57
84
|
onSuccess?: (data: Result, arg: MutationArg) => void;
|
|
58
85
|
mutationFn: MutationFn<Result, MutationArg>;
|
|
59
|
-
mutationKey:
|
|
86
|
+
mutationKey: MutationKey;
|
|
60
87
|
mapOperator?: MapOperator;
|
|
61
88
|
__queryInitHook?: MonoTypeOperatorFunction<any>;
|
|
62
89
|
__queryRunnerHook?: MonoTypeOperatorFunction<any>;
|
|
@@ -7,7 +7,9 @@ export declare const QueryClientProvider: import("react").MemoExoticComponent<({
|
|
|
7
7
|
children: ReactNode;
|
|
8
8
|
client: QueryClient;
|
|
9
9
|
}) => import("react/jsx-runtime").JSX.Element>;
|
|
10
|
-
export declare const useQueryClient: (
|
|
10
|
+
export declare const useQueryClient: ({ unsafe }?: {
|
|
11
|
+
unsafe?: boolean | undefined;
|
|
12
|
+
}) => {
|
|
11
13
|
destroy: () => void;
|
|
12
14
|
pipeQueryResult: <R extends Partial<import("../client/types").QueryResult<T>>, T>({ options$ }: {
|
|
13
15
|
key: string;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { type MutationOptions } from "../../client/mutations/types";
|
|
2
|
-
import { type
|
|
1
|
+
import { type MutationKey, type MutationOptions } from "../../client/mutations/types";
|
|
2
|
+
import { type QueryClient } from "../../client/createClient";
|
|
3
3
|
export type AsyncQueryOptions<Result, Params> = Omit<MutationOptions<Result, Params>, "mutationKey"> & {
|
|
4
|
-
mutationKey?:
|
|
4
|
+
mutationKey?: MutationKey;
|
|
5
5
|
cancelOnUnMount?: boolean;
|
|
6
6
|
};
|
|
7
|
-
export declare function useMutation<Args = void, R = undefined>(options: AsyncQueryOptions<R, Args
|
|
7
|
+
export declare function useMutation<Args = void, R = undefined>(options: AsyncQueryOptions<R, Args>, queryClient?: QueryClient): {
|
|
8
8
|
isError: boolean;
|
|
9
9
|
isIdle: boolean;
|
|
10
10
|
isSuccess: boolean;
|
|
11
11
|
isPending: boolean;
|
|
12
12
|
isPaused: boolean;
|
|
13
13
|
data: R | undefined;
|
|
14
|
-
status: "
|
|
14
|
+
status: import("../../client/mutations/types").MutationStatus;
|
|
15
15
|
error: unknown;
|
|
16
16
|
mutate: (mutationArgs: Args) => void;
|
|
17
17
|
reset: () => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/tests/utils.d.ts
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="react" />
|
|
3
|
+
import { render } from "@testing-library/react";
|
|
4
|
+
import { QueryClient } from "../lib/queries/client/createClient";
|
|
1
5
|
export declare const waitForTimeout: (timeout: number) => Promise<unknown>;
|
|
6
|
+
/**
|
|
7
|
+
* @see https://github.com/TanStack/query/blob/main/packages/react-query/src/__tests__/utils.tsx
|
|
8
|
+
*/
|
|
9
|
+
export declare function sleep(timeout: number): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* @see https://github.com/TanStack/query/blob/main/packages/react-query/src/__tests__/utils.tsx
|
|
12
|
+
*/
|
|
13
|
+
export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
|
|
14
|
+
/**
|
|
15
|
+
* @see https://github.com/TanStack/query/blob/main/packages/react-query/src/__tests__/utils.tsx
|
|
16
|
+
*/
|
|
17
|
+
export declare function renderWithClient(client: QueryClient, ui: React.ReactElement): ReturnType<typeof render>;
|
|
18
|
+
/**
|
|
19
|
+
* @see https://github.com/TanStack/query/blob/main/packages/react-query/src/__tests__/utils.tsx
|
|
20
|
+
*/
|
|
21
|
+
export declare function createQueryClient(): QueryClient;
|
package/package.json
CHANGED