reactjrx 1.57.0 → 1.58.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 +87 -23
- package/dist/index.js +87 -23
- 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 +26 -2
- 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
|
}
|
|
@@ -1604,7 +1608,7 @@ class MutationClient {
|
|
|
1604
1608
|
* @important
|
|
1605
1609
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1606
1610
|
*/
|
|
1607
|
-
__publicField(this, "
|
|
1611
|
+
__publicField(this, "mutationRunnersByKey$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1608
1612
|
/**
|
|
1609
1613
|
* Mutation result subject. It can be used whether there is a mutation
|
|
1610
1614
|
* running or not and can be directly observed.
|
|
@@ -1618,16 +1622,22 @@ class MutationClient {
|
|
|
1618
1622
|
this.mutate$.pipe(
|
|
1619
1623
|
rxjs.tap(({ options, args }) => {
|
|
1620
1624
|
const { mutationKey } = options;
|
|
1621
|
-
|
|
1625
|
+
const serializedMutationKey = serializeKey(mutationKey);
|
|
1626
|
+
let mutationForKey = this.getMutationRunnersByKey(
|
|
1627
|
+
serializedMutationKey
|
|
1628
|
+
);
|
|
1622
1629
|
if (!mutationForKey) {
|
|
1623
|
-
mutationForKey =
|
|
1624
|
-
|
|
1630
|
+
mutationForKey = {
|
|
1631
|
+
...createMutationRunner(options),
|
|
1632
|
+
mutationKey
|
|
1633
|
+
};
|
|
1634
|
+
this.setMutationRunnersByKey(serializedMutationKey, mutationForKey);
|
|
1625
1635
|
mutationForKey.mutation$.subscribe((result) => {
|
|
1626
|
-
let resultForKeySubject = this.mutationResults$.getValue().get(
|
|
1636
|
+
let resultForKeySubject = this.mutationResults$.getValue().get(serializedMutationKey);
|
|
1627
1637
|
if (!resultForKeySubject) {
|
|
1628
1638
|
resultForKeySubject = new rxjs.BehaviorSubject(result);
|
|
1629
1639
|
const resultMap = this.mutationResults$.getValue();
|
|
1630
|
-
resultMap.set(
|
|
1640
|
+
resultMap.set(serializedMutationKey, resultForKeySubject);
|
|
1631
1641
|
this.mutationResults$.next(resultMap);
|
|
1632
1642
|
} else {
|
|
1633
1643
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
@@ -1639,11 +1649,9 @@ class MutationClient {
|
|
|
1639
1649
|
).subscribe(() => {
|
|
1640
1650
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1641
1651
|
const resultMap = this.mutationResults$.getValue();
|
|
1642
|
-
resultMap.delete(
|
|
1652
|
+
resultMap.delete(serializedMutationKey);
|
|
1643
1653
|
this.mutationResults$.next(resultMap);
|
|
1644
|
-
|
|
1645
|
-
mutationsMap.delete(mutationKey);
|
|
1646
|
-
this.mutationRunners$.next(mutationsMap);
|
|
1654
|
+
this.deleteMutationRunnersByKey(serializedMutationKey);
|
|
1647
1655
|
});
|
|
1648
1656
|
}
|
|
1649
1657
|
mutationForKey.trigger({ args, options });
|
|
@@ -1653,10 +1661,66 @@ class MutationClient {
|
|
|
1653
1661
|
rxjs.tap(({ key }) => {
|
|
1654
1662
|
var _a;
|
|
1655
1663
|
const serializedKey = serializeKey(key);
|
|
1656
|
-
(_a = this.
|
|
1664
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
|
|
1657
1665
|
})
|
|
1658
1666
|
).subscribe();
|
|
1659
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
|
+
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)),
|
|
1720
|
+
rxjs.distinctUntilChanged()
|
|
1721
|
+
);
|
|
1722
|
+
return { value$, lastValue };
|
|
1723
|
+
}
|
|
1660
1724
|
observe({ key }) {
|
|
1661
1725
|
var _a;
|
|
1662
1726
|
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
@@ -1705,7 +1769,7 @@ class MutationClient {
|
|
|
1705
1769
|
this.reset$.complete();
|
|
1706
1770
|
this.mutate$.complete();
|
|
1707
1771
|
this.mutationResults$.complete();
|
|
1708
|
-
this.
|
|
1772
|
+
this.mutationRunnersByKey$.complete();
|
|
1709
1773
|
}
|
|
1710
1774
|
}
|
|
1711
1775
|
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
|
}
|
|
@@ -1602,7 +1606,7 @@ class MutationClient {
|
|
|
1602
1606
|
* @important
|
|
1603
1607
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1604
1608
|
*/
|
|
1605
|
-
__publicField(this, "
|
|
1609
|
+
__publicField(this, "mutationRunnersByKey$", new BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1606
1610
|
/**
|
|
1607
1611
|
* Mutation result subject. It can be used whether there is a mutation
|
|
1608
1612
|
* running or not and can be directly observed.
|
|
@@ -1616,16 +1620,22 @@ class MutationClient {
|
|
|
1616
1620
|
this.mutate$.pipe(
|
|
1617
1621
|
tap(({ options, args }) => {
|
|
1618
1622
|
const { mutationKey } = options;
|
|
1619
|
-
|
|
1623
|
+
const serializedMutationKey = serializeKey(mutationKey);
|
|
1624
|
+
let mutationForKey = this.getMutationRunnersByKey(
|
|
1625
|
+
serializedMutationKey
|
|
1626
|
+
);
|
|
1620
1627
|
if (!mutationForKey) {
|
|
1621
|
-
mutationForKey =
|
|
1622
|
-
|
|
1628
|
+
mutationForKey = {
|
|
1629
|
+
...createMutationRunner(options),
|
|
1630
|
+
mutationKey
|
|
1631
|
+
};
|
|
1632
|
+
this.setMutationRunnersByKey(serializedMutationKey, mutationForKey);
|
|
1623
1633
|
mutationForKey.mutation$.subscribe((result) => {
|
|
1624
|
-
let resultForKeySubject = this.mutationResults$.getValue().get(
|
|
1634
|
+
let resultForKeySubject = this.mutationResults$.getValue().get(serializedMutationKey);
|
|
1625
1635
|
if (!resultForKeySubject) {
|
|
1626
1636
|
resultForKeySubject = new BehaviorSubject(result);
|
|
1627
1637
|
const resultMap = this.mutationResults$.getValue();
|
|
1628
|
-
resultMap.set(
|
|
1638
|
+
resultMap.set(serializedMutationKey, resultForKeySubject);
|
|
1629
1639
|
this.mutationResults$.next(resultMap);
|
|
1630
1640
|
} else {
|
|
1631
1641
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
@@ -1637,11 +1647,9 @@ class MutationClient {
|
|
|
1637
1647
|
).subscribe(() => {
|
|
1638
1648
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1639
1649
|
const resultMap = this.mutationResults$.getValue();
|
|
1640
|
-
resultMap.delete(
|
|
1650
|
+
resultMap.delete(serializedMutationKey);
|
|
1641
1651
|
this.mutationResults$.next(resultMap);
|
|
1642
|
-
|
|
1643
|
-
mutationsMap.delete(mutationKey);
|
|
1644
|
-
this.mutationRunners$.next(mutationsMap);
|
|
1652
|
+
this.deleteMutationRunnersByKey(serializedMutationKey);
|
|
1645
1653
|
});
|
|
1646
1654
|
}
|
|
1647
1655
|
mutationForKey.trigger({ args, options });
|
|
@@ -1651,10 +1659,66 @@ class MutationClient {
|
|
|
1651
1659
|
tap(({ key }) => {
|
|
1652
1660
|
var _a;
|
|
1653
1661
|
const serializedKey = serializeKey(key);
|
|
1654
|
-
(_a = this.
|
|
1662
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
|
|
1655
1663
|
})
|
|
1656
1664
|
).subscribe();
|
|
1657
1665
|
}
|
|
1666
|
+
/**
|
|
1667
|
+
* @helper
|
|
1668
|
+
*/
|
|
1669
|
+
setMutationRunnersByKey(key, value) {
|
|
1670
|
+
const map2 = this.mutationRunnersByKey$.getValue();
|
|
1671
|
+
map2.set(key, value);
|
|
1672
|
+
this.mutationRunnersByKey$.next(map2);
|
|
1673
|
+
}
|
|
1674
|
+
/**
|
|
1675
|
+
* @helper
|
|
1676
|
+
*/
|
|
1677
|
+
deleteMutationRunnersByKey(key) {
|
|
1678
|
+
const map2 = this.mutationRunnersByKey$.getValue();
|
|
1679
|
+
map2.delete(key);
|
|
1680
|
+
this.mutationRunnersByKey$.next(map2);
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* @helper
|
|
1684
|
+
*/
|
|
1685
|
+
getMutationRunnersByKey(key) {
|
|
1686
|
+
return this.mutationRunnersByKey$.getValue().get(key);
|
|
1687
|
+
}
|
|
1688
|
+
/**
|
|
1689
|
+
* @returns number of mutation runnings
|
|
1690
|
+
*/
|
|
1691
|
+
runningMutations({ mutationKey, predicate } = {}) {
|
|
1692
|
+
const defaultPredicate = ({ options }) => mutationKey ? (
|
|
1693
|
+
// @todo optimize
|
|
1694
|
+
serializeKey(options.mutationKey) === serializeKey(mutationKey)
|
|
1695
|
+
) : true;
|
|
1696
|
+
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)),
|
|
1718
|
+
distinctUntilChanged()
|
|
1719
|
+
);
|
|
1720
|
+
return { value$, lastValue };
|
|
1721
|
+
}
|
|
1658
1722
|
observe({ key }) {
|
|
1659
1723
|
var _a;
|
|
1660
1724
|
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
@@ -1703,7 +1767,7 @@ class MutationClient {
|
|
|
1703
1767
|
this.reset$.complete();
|
|
1704
1768
|
this.mutate$.complete();
|
|
1705
1769
|
this.mutationResults$.complete();
|
|
1706
|
-
this.
|
|
1770
|
+
this.mutationRunnersByKey$.complete();
|
|
1707
1771
|
}
|
|
1708
1772
|
}
|
|
1709
1773
|
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,33 @@ 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
|
+
export interface MutationFilters {
|
|
26
|
+
/**
|
|
27
|
+
* Match mutation key exactly
|
|
28
|
+
*/
|
|
29
|
+
exact?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Include mutations matching this predicate function
|
|
32
|
+
*/
|
|
33
|
+
predicate?: (mutation: {
|
|
34
|
+
options: {
|
|
35
|
+
mutationKey: MutationKey;
|
|
36
|
+
};
|
|
37
|
+
}) => boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Include mutations matching this mutation key
|
|
40
|
+
*/
|
|
41
|
+
mutationKey?: MutationKey;
|
|
42
|
+
/**
|
|
43
|
+
* Filter by mutation status
|
|
44
|
+
*/
|
|
45
|
+
status?: MutationStatus;
|
|
46
|
+
}
|
|
23
47
|
export interface MutationResult<R> {
|
|
24
48
|
data: R | undefined;
|
|
25
|
-
status:
|
|
49
|
+
status: MutationStatus;
|
|
26
50
|
error: unknown;
|
|
27
51
|
}
|
|
28
52
|
export interface MutationObservedResult<R> extends MutationResult<R> {
|
|
@@ -56,7 +80,7 @@ export interface MutationOptions<Result, MutationArg> {
|
|
|
56
80
|
onError?: (error: unknown, arg: MutationArg) => void;
|
|
57
81
|
onSuccess?: (data: Result, arg: MutationArg) => void;
|
|
58
82
|
mutationFn: MutationFn<Result, MutationArg>;
|
|
59
|
-
mutationKey:
|
|
83
|
+
mutationKey: MutationKey;
|
|
60
84
|
mapOperator?: MapOperator;
|
|
61
85
|
__queryInitHook?: MonoTypeOperatorFunction<any>;
|
|
62
86
|
__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