reactjrx 1.56.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 +89 -26
- package/dist/index.js +89 -26
- 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
|
}
|
|
@@ -1475,8 +1479,7 @@ const mergeResults = (stream$) => stream$.pipe(
|
|
|
1475
1479
|
{
|
|
1476
1480
|
data: void 0,
|
|
1477
1481
|
error: void 0,
|
|
1478
|
-
|
|
1479
|
-
status: "loading"
|
|
1482
|
+
status: "pending"
|
|
1480
1483
|
}
|
|
1481
1484
|
),
|
|
1482
1485
|
rxjs.distinctUntilChanged(
|
|
@@ -1540,7 +1543,7 @@ const createMutationRunner = ({
|
|
|
1540
1543
|
rxjs.takeUntil(queryIsOver$)
|
|
1541
1544
|
);
|
|
1542
1545
|
const loading$ = rxjs.of({
|
|
1543
|
-
status: "
|
|
1546
|
+
status: "pending"
|
|
1544
1547
|
});
|
|
1545
1548
|
return rxjs.merge(
|
|
1546
1549
|
loading$,
|
|
@@ -1605,7 +1608,7 @@ class MutationClient {
|
|
|
1605
1608
|
* @important
|
|
1606
1609
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1607
1610
|
*/
|
|
1608
|
-
__publicField(this, "
|
|
1611
|
+
__publicField(this, "mutationRunnersByKey$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1609
1612
|
/**
|
|
1610
1613
|
* Mutation result subject. It can be used whether there is a mutation
|
|
1611
1614
|
* running or not and can be directly observed.
|
|
@@ -1619,16 +1622,22 @@ class MutationClient {
|
|
|
1619
1622
|
this.mutate$.pipe(
|
|
1620
1623
|
rxjs.tap(({ options, args }) => {
|
|
1621
1624
|
const { mutationKey } = options;
|
|
1622
|
-
|
|
1625
|
+
const serializedMutationKey = serializeKey(mutationKey);
|
|
1626
|
+
let mutationForKey = this.getMutationRunnersByKey(
|
|
1627
|
+
serializedMutationKey
|
|
1628
|
+
);
|
|
1623
1629
|
if (!mutationForKey) {
|
|
1624
|
-
mutationForKey =
|
|
1625
|
-
|
|
1630
|
+
mutationForKey = {
|
|
1631
|
+
...createMutationRunner(options),
|
|
1632
|
+
mutationKey
|
|
1633
|
+
};
|
|
1634
|
+
this.setMutationRunnersByKey(serializedMutationKey, mutationForKey);
|
|
1626
1635
|
mutationForKey.mutation$.subscribe((result) => {
|
|
1627
|
-
let resultForKeySubject = this.mutationResults$.getValue().get(
|
|
1636
|
+
let resultForKeySubject = this.mutationResults$.getValue().get(serializedMutationKey);
|
|
1628
1637
|
if (!resultForKeySubject) {
|
|
1629
1638
|
resultForKeySubject = new rxjs.BehaviorSubject(result);
|
|
1630
1639
|
const resultMap = this.mutationResults$.getValue();
|
|
1631
|
-
resultMap.set(
|
|
1640
|
+
resultMap.set(serializedMutationKey, resultForKeySubject);
|
|
1632
1641
|
this.mutationResults$.next(resultMap);
|
|
1633
1642
|
} else {
|
|
1634
1643
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
@@ -1640,11 +1649,9 @@ class MutationClient {
|
|
|
1640
1649
|
).subscribe(() => {
|
|
1641
1650
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1642
1651
|
const resultMap = this.mutationResults$.getValue();
|
|
1643
|
-
resultMap.delete(
|
|
1652
|
+
resultMap.delete(serializedMutationKey);
|
|
1644
1653
|
this.mutationResults$.next(resultMap);
|
|
1645
|
-
|
|
1646
|
-
mutationsMap.delete(mutationKey);
|
|
1647
|
-
this.mutationRunners$.next(mutationsMap);
|
|
1654
|
+
this.deleteMutationRunnersByKey(serializedMutationKey);
|
|
1648
1655
|
});
|
|
1649
1656
|
}
|
|
1650
1657
|
mutationForKey.trigger({ args, options });
|
|
@@ -1654,10 +1661,66 @@ class MutationClient {
|
|
|
1654
1661
|
rxjs.tap(({ key }) => {
|
|
1655
1662
|
var _a;
|
|
1656
1663
|
const serializedKey = serializeKey(key);
|
|
1657
|
-
(_a = this.
|
|
1664
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
|
|
1658
1665
|
})
|
|
1659
1666
|
).subscribe();
|
|
1660
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
|
+
}
|
|
1661
1724
|
observe({ key }) {
|
|
1662
1725
|
var _a;
|
|
1663
1726
|
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
@@ -1706,7 +1769,7 @@ class MutationClient {
|
|
|
1706
1769
|
this.reset$.complete();
|
|
1707
1770
|
this.mutate$.complete();
|
|
1708
1771
|
this.mutationResults$.complete();
|
|
1709
|
-
this.
|
|
1772
|
+
this.mutationRunnersByKey$.complete();
|
|
1710
1773
|
}
|
|
1711
1774
|
}
|
|
1712
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
|
}
|
|
@@ -1473,8 +1477,7 @@ const mergeResults = (stream$) => stream$.pipe(
|
|
|
1473
1477
|
{
|
|
1474
1478
|
data: void 0,
|
|
1475
1479
|
error: void 0,
|
|
1476
|
-
|
|
1477
|
-
status: "loading"
|
|
1480
|
+
status: "pending"
|
|
1478
1481
|
}
|
|
1479
1482
|
),
|
|
1480
1483
|
distinctUntilChanged(
|
|
@@ -1538,7 +1541,7 @@ const createMutationRunner = ({
|
|
|
1538
1541
|
takeUntil(queryIsOver$)
|
|
1539
1542
|
);
|
|
1540
1543
|
const loading$ = of({
|
|
1541
|
-
status: "
|
|
1544
|
+
status: "pending"
|
|
1542
1545
|
});
|
|
1543
1546
|
return merge(
|
|
1544
1547
|
loading$,
|
|
@@ -1603,7 +1606,7 @@ class MutationClient {
|
|
|
1603
1606
|
* @important
|
|
1604
1607
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1605
1608
|
*/
|
|
1606
|
-
__publicField(this, "
|
|
1609
|
+
__publicField(this, "mutationRunnersByKey$", new BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1607
1610
|
/**
|
|
1608
1611
|
* Mutation result subject. It can be used whether there is a mutation
|
|
1609
1612
|
* running or not and can be directly observed.
|
|
@@ -1617,16 +1620,22 @@ class MutationClient {
|
|
|
1617
1620
|
this.mutate$.pipe(
|
|
1618
1621
|
tap(({ options, args }) => {
|
|
1619
1622
|
const { mutationKey } = options;
|
|
1620
|
-
|
|
1623
|
+
const serializedMutationKey = serializeKey(mutationKey);
|
|
1624
|
+
let mutationForKey = this.getMutationRunnersByKey(
|
|
1625
|
+
serializedMutationKey
|
|
1626
|
+
);
|
|
1621
1627
|
if (!mutationForKey) {
|
|
1622
|
-
mutationForKey =
|
|
1623
|
-
|
|
1628
|
+
mutationForKey = {
|
|
1629
|
+
...createMutationRunner(options),
|
|
1630
|
+
mutationKey
|
|
1631
|
+
};
|
|
1632
|
+
this.setMutationRunnersByKey(serializedMutationKey, mutationForKey);
|
|
1624
1633
|
mutationForKey.mutation$.subscribe((result) => {
|
|
1625
|
-
let resultForKeySubject = this.mutationResults$.getValue().get(
|
|
1634
|
+
let resultForKeySubject = this.mutationResults$.getValue().get(serializedMutationKey);
|
|
1626
1635
|
if (!resultForKeySubject) {
|
|
1627
1636
|
resultForKeySubject = new BehaviorSubject(result);
|
|
1628
1637
|
const resultMap = this.mutationResults$.getValue();
|
|
1629
|
-
resultMap.set(
|
|
1638
|
+
resultMap.set(serializedMutationKey, resultForKeySubject);
|
|
1630
1639
|
this.mutationResults$.next(resultMap);
|
|
1631
1640
|
} else {
|
|
1632
1641
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
@@ -1638,11 +1647,9 @@ class MutationClient {
|
|
|
1638
1647
|
).subscribe(() => {
|
|
1639
1648
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1640
1649
|
const resultMap = this.mutationResults$.getValue();
|
|
1641
|
-
resultMap.delete(
|
|
1650
|
+
resultMap.delete(serializedMutationKey);
|
|
1642
1651
|
this.mutationResults$.next(resultMap);
|
|
1643
|
-
|
|
1644
|
-
mutationsMap.delete(mutationKey);
|
|
1645
|
-
this.mutationRunners$.next(mutationsMap);
|
|
1652
|
+
this.deleteMutationRunnersByKey(serializedMutationKey);
|
|
1646
1653
|
});
|
|
1647
1654
|
}
|
|
1648
1655
|
mutationForKey.trigger({ args, options });
|
|
@@ -1652,10 +1659,66 @@ class MutationClient {
|
|
|
1652
1659
|
tap(({ key }) => {
|
|
1653
1660
|
var _a;
|
|
1654
1661
|
const serializedKey = serializeKey(key);
|
|
1655
|
-
(_a = this.
|
|
1662
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
|
|
1656
1663
|
})
|
|
1657
1664
|
).subscribe();
|
|
1658
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
|
+
}
|
|
1659
1722
|
observe({ key }) {
|
|
1660
1723
|
var _a;
|
|
1661
1724
|
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
@@ -1704,7 +1767,7 @@ class MutationClient {
|
|
|
1704
1767
|
this.reset$.complete();
|
|
1705
1768
|
this.mutate$.complete();
|
|
1706
1769
|
this.mutationResults$.complete();
|
|
1707
|
-
this.
|
|
1770
|
+
this.mutationRunnersByKey$.complete();
|
|
1708
1771
|
}
|
|
1709
1772
|
}
|
|
1710
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