reactjrx 1.59.0 → 1.61.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 +225 -101
- package/dist/index.js +225 -101
- package/dist/lib/queries/client/mutations/Mutation.d.ts +15 -0
- package/dist/lib/queries/client/mutations/MutationClient.d.ts +25 -12
- package/dist/lib/queries/client/mutations/createMutationRunner.d.ts +6 -4
- package/dist/lib/queries/client/mutations/defaultMutationState.d.ts +2 -0
- package/dist/lib/queries/client/mutations/filters.d.ts +2 -0
- package/dist/lib/queries/client/mutations/types.d.ts +12 -8
- package/dist/lib/queries/client/types.d.ts +5 -0
- package/dist/lib/queries/react/mutations/useIsMutating.d.ts +3 -0
- package/dist/lib/queries/react/mutations/useIsMutating.test.d.ts +1 -0
- package/dist/lib/queries/react/mutations/useMutation.d.ts +1 -1
- package/dist/lib/queries/react/mutations/useMutationState.d.ts +7 -3
- package/dist/tests/utils.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -481,21 +481,21 @@ function useMutation(options, queryClient) {
|
|
|
481
481
|
},
|
|
482
482
|
[finalQueryClient, key]
|
|
483
483
|
);
|
|
484
|
-
const
|
|
485
|
-
finalQueryClient.mutationClient.
|
|
484
|
+
const cancel = react.useCallback(() => {
|
|
485
|
+
finalQueryClient.mutationClient.cancel({
|
|
486
486
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
487
487
|
});
|
|
488
488
|
}, [finalQueryClient]);
|
|
489
489
|
react.useEffect(() => {
|
|
490
490
|
return () => {
|
|
491
491
|
if (optionsRef.current.cancelOnUnMount) {
|
|
492
|
-
finalQueryClient.mutationClient.
|
|
492
|
+
finalQueryClient.mutationClient.cancel({
|
|
493
493
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
494
494
|
});
|
|
495
495
|
}
|
|
496
496
|
};
|
|
497
497
|
}, []);
|
|
498
|
-
return { mutate,
|
|
498
|
+
return { mutate, cancel, ...result };
|
|
499
499
|
}
|
|
500
500
|
const arrayEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
501
501
|
function shallowEqual(objA, objB) {
|
|
@@ -1473,7 +1473,9 @@ const mergeResults = (stream$) => stream$.pipe(
|
|
|
1473
1473
|
(acc, current) => {
|
|
1474
1474
|
return {
|
|
1475
1475
|
...acc,
|
|
1476
|
-
...current
|
|
1476
|
+
...current,
|
|
1477
|
+
data: current.data ?? acc.data,
|
|
1478
|
+
error: current.error ?? acc.error
|
|
1477
1479
|
};
|
|
1478
1480
|
},
|
|
1479
1481
|
{
|
|
@@ -1486,25 +1488,99 @@ const mergeResults = (stream$) => stream$.pipe(
|
|
|
1486
1488
|
({ data: prevData, ...prev }, { data: currData, ...curr }) => shallowEqual(prev, curr) && shallowEqual(prevData, currData)
|
|
1487
1489
|
)
|
|
1488
1490
|
);
|
|
1491
|
+
const getDefaultMutationState = () => ({
|
|
1492
|
+
context: {},
|
|
1493
|
+
data: void 0,
|
|
1494
|
+
error: null,
|
|
1495
|
+
status: "idle",
|
|
1496
|
+
submittedAt: 0,
|
|
1497
|
+
variables: void 0
|
|
1498
|
+
});
|
|
1499
|
+
class Mutation {
|
|
1500
|
+
constructor({
|
|
1501
|
+
args,
|
|
1502
|
+
...options
|
|
1503
|
+
}) {
|
|
1504
|
+
__publicField(this, "stateSubject", new rxjs.BehaviorSubject(
|
|
1505
|
+
getDefaultMutationState()
|
|
1506
|
+
));
|
|
1507
|
+
/**
|
|
1508
|
+
* @important
|
|
1509
|
+
* proxy for rect-query, not really useful
|
|
1510
|
+
*/
|
|
1511
|
+
__publicField(this, "state", this.stateSubject.getValue());
|
|
1512
|
+
__publicField(this, "options");
|
|
1513
|
+
__publicField(this, "mutation$");
|
|
1514
|
+
this.options = options;
|
|
1515
|
+
const mutationFn = options.mutationFn;
|
|
1516
|
+
this.state.variables = args;
|
|
1517
|
+
this.stateSubject.next(this.state);
|
|
1518
|
+
const mutationFnObservable = typeof mutationFn === "function" ? rxjs.defer(() => rxjs.from(mutationFn(args))) : mutationFn;
|
|
1519
|
+
const queryRunner$ = mutationFnObservable.pipe(
|
|
1520
|
+
retryOnError(options),
|
|
1521
|
+
rxjs.take(1),
|
|
1522
|
+
rxjs.map((data) => ({ data, isError: false })),
|
|
1523
|
+
rxjs.catchError((error) => {
|
|
1524
|
+
console.error(error);
|
|
1525
|
+
if (options.onError != null) {
|
|
1526
|
+
options.onError(error, args);
|
|
1527
|
+
}
|
|
1528
|
+
return rxjs.of({ data: error, isError: true });
|
|
1529
|
+
})
|
|
1530
|
+
);
|
|
1531
|
+
const loading$ = rxjs.of({
|
|
1532
|
+
status: "pending"
|
|
1533
|
+
});
|
|
1534
|
+
this.mutation$ = rxjs.merge(
|
|
1535
|
+
loading$,
|
|
1536
|
+
queryRunner$.pipe(
|
|
1537
|
+
rxjs.map(({ data, isError }) => {
|
|
1538
|
+
if (!isError) {
|
|
1539
|
+
if (options.onSuccess != null)
|
|
1540
|
+
options.onSuccess(data, args);
|
|
1541
|
+
}
|
|
1542
|
+
return isError ? {
|
|
1543
|
+
status: "error",
|
|
1544
|
+
error: data,
|
|
1545
|
+
data: void 0
|
|
1546
|
+
} : {
|
|
1547
|
+
status: "success",
|
|
1548
|
+
error: null,
|
|
1549
|
+
data
|
|
1550
|
+
};
|
|
1551
|
+
})
|
|
1552
|
+
)
|
|
1553
|
+
).pipe(
|
|
1554
|
+
mergeResults,
|
|
1555
|
+
rxjs.tap((value) => {
|
|
1556
|
+
this.state = { ...this.state, ...value };
|
|
1557
|
+
this.stateSubject.next(this.state);
|
|
1558
|
+
}),
|
|
1559
|
+
options.__queryRunnerHook ?? rxjs.identity,
|
|
1560
|
+
rxjs.share()
|
|
1561
|
+
);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1489
1564
|
const createMutationRunner = ({
|
|
1490
1565
|
__queryFinalizeHook,
|
|
1491
1566
|
__queryInitHook,
|
|
1492
1567
|
__queryTriggerHook
|
|
1493
1568
|
}) => {
|
|
1494
1569
|
const trigger$ = new rxjs.Subject();
|
|
1495
|
-
const
|
|
1570
|
+
const cancel$ = new rxjs.Subject();
|
|
1496
1571
|
let closed = false;
|
|
1497
1572
|
const mapOperator$ = new rxjs.BehaviorSubject("merge");
|
|
1498
|
-
const
|
|
1573
|
+
const mutationsSubject = new rxjs.BehaviorSubject([]);
|
|
1499
1574
|
const destroy = () => {
|
|
1500
1575
|
if (closed) {
|
|
1501
1576
|
throw new Error("Trying to close an already closed mutation");
|
|
1502
1577
|
}
|
|
1503
1578
|
closed = true;
|
|
1504
1579
|
mapOperator$.complete();
|
|
1505
|
-
|
|
1580
|
+
mutationsSubject.complete();
|
|
1506
1581
|
trigger$.complete();
|
|
1507
|
-
|
|
1582
|
+
cancel$.next();
|
|
1583
|
+
cancel$.complete();
|
|
1508
1584
|
};
|
|
1509
1585
|
const stableMapOperator$ = mapOperator$.pipe(
|
|
1510
1586
|
rxjs.filter(isDefined),
|
|
@@ -1514,29 +1590,35 @@ const createMutationRunner = ({
|
|
|
1514
1590
|
__queryInitHook ?? rxjs.identity,
|
|
1515
1591
|
rxjs.mergeMap((mapOperator) => {
|
|
1516
1592
|
const switchOperator = mapOperator === "concat" ? rxjs.concatMap : mapOperator === "switch" ? rxjs.switchMap : rxjs.mergeMap;
|
|
1593
|
+
let mutationsForCurrentMapOperatorSubject = [];
|
|
1594
|
+
const removeMutation = (mutation) => {
|
|
1595
|
+
mutationsForCurrentMapOperatorSubject = mutationsForCurrentMapOperatorSubject.filter(
|
|
1596
|
+
(item) => item !== mutation
|
|
1597
|
+
);
|
|
1598
|
+
mutationsSubject.next(
|
|
1599
|
+
mutationsSubject.getValue().filter((item) => item !== mutation)
|
|
1600
|
+
);
|
|
1601
|
+
};
|
|
1517
1602
|
return trigger$.pipe(
|
|
1518
1603
|
rxjs.takeUntil(stableMapOperator$.pipe(rxjs.skip(1))),
|
|
1519
|
-
rxjs.
|
|
1520
|
-
|
|
1604
|
+
rxjs.map(({ args, options }) => {
|
|
1605
|
+
const mutation = new Mutation({
|
|
1606
|
+
args,
|
|
1607
|
+
...options,
|
|
1608
|
+
mapOperator
|
|
1609
|
+
});
|
|
1610
|
+
mutationsForCurrentMapOperatorSubject = [
|
|
1611
|
+
...mutationsForCurrentMapOperatorSubject,
|
|
1612
|
+
mutation
|
|
1613
|
+
];
|
|
1614
|
+
mutationsSubject.next([...mutationsSubject.getValue(), mutation]);
|
|
1615
|
+
return mutation;
|
|
1521
1616
|
}),
|
|
1522
|
-
switchOperator((
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
const
|
|
1526
|
-
|
|
1527
|
-
rxjs.take(1),
|
|
1528
|
-
rxjs.map((data) => ({ data, isError: false })),
|
|
1529
|
-
rxjs.catchError((error) => {
|
|
1530
|
-
console.error(error);
|
|
1531
|
-
if (options.onError != null) {
|
|
1532
|
-
options.onError(error, args);
|
|
1533
|
-
}
|
|
1534
|
-
return rxjs.of({ data: error, isError: true });
|
|
1535
|
-
}),
|
|
1536
|
-
rxjs.share()
|
|
1537
|
-
);
|
|
1538
|
-
const queryIsOver$ = queryRunner$.pipe(
|
|
1539
|
-
rxjs.map(({ data, isError }) => isError || data)
|
|
1617
|
+
switchOperator((mutation) => {
|
|
1618
|
+
if (!mutationsSubject.getValue().includes(mutation))
|
|
1619
|
+
return rxjs.of({});
|
|
1620
|
+
const queryIsOver$ = mutation.mutation$.pipe(
|
|
1621
|
+
rxjs.map(({ data, error }) => error || data)
|
|
1540
1622
|
);
|
|
1541
1623
|
const isThisCurrentFunctionLastOneCalled = trigger$.pipe(
|
|
1542
1624
|
rxjs.take(1),
|
|
@@ -1544,48 +1626,35 @@ const createMutationRunner = ({
|
|
|
1544
1626
|
rxjs.startWith(true),
|
|
1545
1627
|
rxjs.takeUntil(queryIsOver$)
|
|
1546
1628
|
);
|
|
1547
|
-
const
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
queryRunner$,
|
|
1554
|
-
isThisCurrentFunctionLastOneCalled
|
|
1555
|
-
]).pipe(
|
|
1556
|
-
rxjs.map(([{ data, isError }, isLastMutationCalled]) => {
|
|
1557
|
-
if (!isError) {
|
|
1558
|
-
if (options.onSuccess != null)
|
|
1559
|
-
options.onSuccess(data, args);
|
|
1560
|
-
}
|
|
1561
|
-
if (isLastMutationCalled) {
|
|
1562
|
-
return isError ? {
|
|
1563
|
-
status: "error",
|
|
1564
|
-
error: data,
|
|
1565
|
-
data: void 0
|
|
1566
|
-
} : {
|
|
1567
|
-
status: "success",
|
|
1568
|
-
error: void 0,
|
|
1569
|
-
data
|
|
1570
|
-
};
|
|
1571
|
-
}
|
|
1629
|
+
const result$ = rxjs.combineLatest([
|
|
1630
|
+
mutation.mutation$,
|
|
1631
|
+
isThisCurrentFunctionLastOneCalled
|
|
1632
|
+
]).pipe(
|
|
1633
|
+
rxjs.map(([result, isLastMutationCalled]) => {
|
|
1634
|
+
if ((result.status === "success" || result.status === "error") && !isLastMutationCalled) {
|
|
1572
1635
|
return {};
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
)
|
|
1576
|
-
|
|
1577
|
-
|
|
1636
|
+
}
|
|
1637
|
+
return result;
|
|
1638
|
+
}),
|
|
1639
|
+
rxjs.takeUntil(cancel$.pipe()),
|
|
1640
|
+
mergeResults,
|
|
1578
1641
|
rxjs.finalize(() => {
|
|
1579
|
-
|
|
1642
|
+
removeMutation(mutation);
|
|
1580
1643
|
})
|
|
1581
1644
|
);
|
|
1645
|
+
return result$;
|
|
1582
1646
|
}),
|
|
1583
|
-
|
|
1584
|
-
|
|
1647
|
+
mergeResults,
|
|
1648
|
+
__queryTriggerHook ?? rxjs.identity
|
|
1585
1649
|
);
|
|
1586
1650
|
}),
|
|
1587
1651
|
__queryFinalizeHook ?? rxjs.identity
|
|
1588
1652
|
);
|
|
1653
|
+
cancel$.subscribe(() => {
|
|
1654
|
+
if (mutationsSubject.getValue().length === 0)
|
|
1655
|
+
return;
|
|
1656
|
+
mutationsSubject.next([]);
|
|
1657
|
+
});
|
|
1589
1658
|
return {
|
|
1590
1659
|
mutation$,
|
|
1591
1660
|
trigger: ({
|
|
@@ -1595,12 +1664,29 @@ const createMutationRunner = ({
|
|
|
1595
1664
|
mapOperator$.next(options.mapOperator);
|
|
1596
1665
|
trigger$.next({ args, options });
|
|
1597
1666
|
},
|
|
1598
|
-
|
|
1667
|
+
cancel$,
|
|
1599
1668
|
destroy,
|
|
1600
|
-
|
|
1669
|
+
mutationsSubject,
|
|
1601
1670
|
getClosed: () => closed
|
|
1602
1671
|
};
|
|
1603
1672
|
};
|
|
1673
|
+
const createPredicateForFilters = ({
|
|
1674
|
+
mutationKey,
|
|
1675
|
+
status,
|
|
1676
|
+
predicate
|
|
1677
|
+
} = {}) => {
|
|
1678
|
+
const defaultPredicate = (mutation) => {
|
|
1679
|
+
if (mutationKey !== void 0 && // @todo optimize
|
|
1680
|
+
serializeKey(mutation.options.mutationKey) !== serializeKey(mutationKey)) {
|
|
1681
|
+
return false;
|
|
1682
|
+
}
|
|
1683
|
+
if (status && mutation.stateSubject.getValue().status !== status)
|
|
1684
|
+
return false;
|
|
1685
|
+
return true;
|
|
1686
|
+
};
|
|
1687
|
+
const finalPredicate = predicate ?? defaultPredicate;
|
|
1688
|
+
return finalPredicate;
|
|
1689
|
+
};
|
|
1604
1690
|
class MutationClient {
|
|
1605
1691
|
constructor() {
|
|
1606
1692
|
/**
|
|
@@ -1620,7 +1706,15 @@ class MutationClient {
|
|
|
1620
1706
|
*/
|
|
1621
1707
|
__publicField(this, "mutationResults$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1622
1708
|
__publicField(this, "mutate$", new rxjs.Subject());
|
|
1623
|
-
__publicField(this, "
|
|
1709
|
+
__publicField(this, "cancel$", new rxjs.Subject());
|
|
1710
|
+
/**
|
|
1711
|
+
* Observable to track how many running mutations per runner
|
|
1712
|
+
*/
|
|
1713
|
+
__publicField(this, "isMutatingSubject", new rxjs.BehaviorSubject([]));
|
|
1714
|
+
/**
|
|
1715
|
+
* List of all mutations running
|
|
1716
|
+
*/
|
|
1717
|
+
__publicField(this, "mutationsSubject", new rxjs.BehaviorSubject([]));
|
|
1624
1718
|
this.mutate$.pipe(
|
|
1625
1719
|
rxjs.tap(({ options, args }) => {
|
|
1626
1720
|
const { mutationKey } = options;
|
|
@@ -1645,9 +1739,10 @@ class MutationClient {
|
|
|
1645
1739
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
1646
1740
|
}
|
|
1647
1741
|
});
|
|
1648
|
-
mutationForKey.
|
|
1742
|
+
mutationForKey.mutationsSubject.pipe(
|
|
1743
|
+
rxjs.distinctUntilChanged(shallowEqual),
|
|
1649
1744
|
rxjs.skip(1),
|
|
1650
|
-
rxjs.filter((
|
|
1745
|
+
rxjs.filter((items) => items.length === 0)
|
|
1651
1746
|
).subscribe(() => {
|
|
1652
1747
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1653
1748
|
const resultMap = this.mutationResults$.getValue();
|
|
@@ -1659,13 +1754,32 @@ class MutationClient {
|
|
|
1659
1754
|
mutationForKey.trigger({ args, options });
|
|
1660
1755
|
})
|
|
1661
1756
|
).subscribe();
|
|
1662
|
-
this.
|
|
1757
|
+
this.cancel$.pipe(
|
|
1663
1758
|
rxjs.tap(({ key }) => {
|
|
1664
1759
|
var _a;
|
|
1665
1760
|
const serializedKey = serializeKey(key);
|
|
1666
|
-
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.
|
|
1761
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.cancel$.next();
|
|
1667
1762
|
})
|
|
1668
1763
|
).subscribe();
|
|
1764
|
+
this.mutationRunnersByKey$.pipe(
|
|
1765
|
+
rxjs.switchMap((mapItem) => {
|
|
1766
|
+
const mutationRunners = Array.from(mapItem.values());
|
|
1767
|
+
const mutations$ = rxjs.combineLatest(
|
|
1768
|
+
mutationRunners.map(
|
|
1769
|
+
(runner) => runner.mutationsSubject.pipe(
|
|
1770
|
+
rxjs.map((mutations) => mutations.map((mutation) => mutation))
|
|
1771
|
+
)
|
|
1772
|
+
)
|
|
1773
|
+
);
|
|
1774
|
+
return mutations$.pipe(
|
|
1775
|
+
rxjs.map(
|
|
1776
|
+
(mutationsByKeys) => mutationsByKeys.reduce((acc, value) => [...acc, ...value], [])
|
|
1777
|
+
)
|
|
1778
|
+
);
|
|
1779
|
+
}),
|
|
1780
|
+
rxjs.startWith([]),
|
|
1781
|
+
rxjs.distinctUntilChanged(shallowEqual)
|
|
1782
|
+
).subscribe(this.mutationsSubject);
|
|
1669
1783
|
}
|
|
1670
1784
|
/**
|
|
1671
1785
|
* @helper
|
|
@@ -1689,37 +1803,45 @@ class MutationClient {
|
|
|
1689
1803
|
getMutationRunnersByKey(key) {
|
|
1690
1804
|
return this.mutationRunnersByKey$.getValue().get(key);
|
|
1691
1805
|
}
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1806
|
+
useIsMutating(filters = {}) {
|
|
1807
|
+
const predicate = createPredicateForFilters(filters);
|
|
1808
|
+
const reduceByNumber = (entries) => entries.reduce((acc, mutation) => {
|
|
1809
|
+
return predicate(mutation) && mutation.stateSubject.getValue().status === "pending" ? 1 + acc : acc;
|
|
1810
|
+
}, 0);
|
|
1811
|
+
const lastValue = reduceByNumber(this.mutationsSubject.getValue());
|
|
1812
|
+
const value$ = this.mutationsSubject.pipe(
|
|
1813
|
+
rxjs.switchMap((mutations) => {
|
|
1814
|
+
const mutationsOnStateUpdate = mutations.map(
|
|
1815
|
+
(mutation) => mutation.stateSubject.pipe(rxjs.map(() => mutation))
|
|
1816
|
+
);
|
|
1817
|
+
return mutationsOnStateUpdate.length === 0 ? rxjs.of([]) : rxjs.combineLatest(mutationsOnStateUpdate);
|
|
1818
|
+
}),
|
|
1819
|
+
rxjs.map(reduceByNumber),
|
|
1820
|
+
rxjs.distinctUntilChanged()
|
|
1703
1821
|
);
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1822
|
+
return { value$, lastValue };
|
|
1823
|
+
}
|
|
1824
|
+
mutationState({
|
|
1825
|
+
filters,
|
|
1826
|
+
select
|
|
1827
|
+
} = {}) {
|
|
1828
|
+
const predicate = createPredicateForFilters(filters);
|
|
1829
|
+
const finalSelect = select ?? ((mutation) => mutation.stateSubject.getValue());
|
|
1830
|
+
const lastValue = this.mutationsSubject.getValue().reduce((acc, mutation) => {
|
|
1831
|
+
const result = [...acc, mutation];
|
|
1832
|
+
return result;
|
|
1833
|
+
}, []).filter(predicate).map((mutation) => finalSelect(mutation));
|
|
1834
|
+
const value$ = this.mutationsSubject.pipe(
|
|
1835
|
+
rxjs.switchMap((mutations) => {
|
|
1836
|
+
const mutationsOnStateUpdate = mutations.map(
|
|
1837
|
+
(mutation) => mutation.stateSubject.pipe(
|
|
1838
|
+
rxjs.filter(() => predicate(mutation)),
|
|
1839
|
+
rxjs.map(() => finalSelect(mutation))
|
|
1717
1840
|
)
|
|
1718
|
-
|
|
1719
|
-
return
|
|
1841
|
+
);
|
|
1842
|
+
return mutationsOnStateUpdate.length === 0 ? rxjs.of([]) : rxjs.combineLatest(mutationsOnStateUpdate);
|
|
1720
1843
|
}),
|
|
1721
|
-
rxjs.
|
|
1722
|
-
rxjs.distinctUntilChanged()
|
|
1844
|
+
rxjs.distinctUntilChanged(shallowEqual)
|
|
1723
1845
|
);
|
|
1724
1846
|
return { value$, lastValue };
|
|
1725
1847
|
}
|
|
@@ -1761,17 +1883,19 @@ class MutationClient {
|
|
|
1761
1883
|
this.mutate$.next(params);
|
|
1762
1884
|
}
|
|
1763
1885
|
/**
|
|
1764
|
-
* This will
|
|
1886
|
+
* This will cancel any current mutation runnings.
|
|
1765
1887
|
* No discrimination process
|
|
1766
1888
|
*/
|
|
1767
|
-
|
|
1768
|
-
this.
|
|
1889
|
+
cancel(params) {
|
|
1890
|
+
this.cancel$.next(params);
|
|
1769
1891
|
}
|
|
1770
1892
|
destroy() {
|
|
1771
|
-
this.
|
|
1893
|
+
this.cancel$.complete();
|
|
1772
1894
|
this.mutate$.complete();
|
|
1773
1895
|
this.mutationResults$.complete();
|
|
1774
1896
|
this.mutationRunnersByKey$.complete();
|
|
1897
|
+
this.isMutatingSubject.complete();
|
|
1898
|
+
this.mutationsSubject.complete();
|
|
1775
1899
|
}
|
|
1776
1900
|
}
|
|
1777
1901
|
const createClient = () => {
|
package/dist/index.js
CHANGED
|
@@ -479,21 +479,21 @@ function useMutation(options, queryClient) {
|
|
|
479
479
|
},
|
|
480
480
|
[finalQueryClient, key]
|
|
481
481
|
);
|
|
482
|
-
const
|
|
483
|
-
finalQueryClient.mutationClient.
|
|
482
|
+
const cancel = useCallback(() => {
|
|
483
|
+
finalQueryClient.mutationClient.cancel({
|
|
484
484
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
485
485
|
});
|
|
486
486
|
}, [finalQueryClient]);
|
|
487
487
|
useEffect(() => {
|
|
488
488
|
return () => {
|
|
489
489
|
if (optionsRef.current.cancelOnUnMount) {
|
|
490
|
-
finalQueryClient.mutationClient.
|
|
490
|
+
finalQueryClient.mutationClient.cancel({
|
|
491
491
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
492
492
|
});
|
|
493
493
|
}
|
|
494
494
|
};
|
|
495
495
|
}, []);
|
|
496
|
-
return { mutate,
|
|
496
|
+
return { mutate, cancel, ...result };
|
|
497
497
|
}
|
|
498
498
|
const arrayEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
499
499
|
function shallowEqual(objA, objB) {
|
|
@@ -1471,7 +1471,9 @@ const mergeResults = (stream$) => stream$.pipe(
|
|
|
1471
1471
|
(acc, current) => {
|
|
1472
1472
|
return {
|
|
1473
1473
|
...acc,
|
|
1474
|
-
...current
|
|
1474
|
+
...current,
|
|
1475
|
+
data: current.data ?? acc.data,
|
|
1476
|
+
error: current.error ?? acc.error
|
|
1475
1477
|
};
|
|
1476
1478
|
},
|
|
1477
1479
|
{
|
|
@@ -1484,25 +1486,99 @@ const mergeResults = (stream$) => stream$.pipe(
|
|
|
1484
1486
|
({ data: prevData, ...prev }, { data: currData, ...curr }) => shallowEqual(prev, curr) && shallowEqual(prevData, currData)
|
|
1485
1487
|
)
|
|
1486
1488
|
);
|
|
1489
|
+
const getDefaultMutationState = () => ({
|
|
1490
|
+
context: {},
|
|
1491
|
+
data: void 0,
|
|
1492
|
+
error: null,
|
|
1493
|
+
status: "idle",
|
|
1494
|
+
submittedAt: 0,
|
|
1495
|
+
variables: void 0
|
|
1496
|
+
});
|
|
1497
|
+
class Mutation {
|
|
1498
|
+
constructor({
|
|
1499
|
+
args,
|
|
1500
|
+
...options
|
|
1501
|
+
}) {
|
|
1502
|
+
__publicField(this, "stateSubject", new BehaviorSubject(
|
|
1503
|
+
getDefaultMutationState()
|
|
1504
|
+
));
|
|
1505
|
+
/**
|
|
1506
|
+
* @important
|
|
1507
|
+
* proxy for rect-query, not really useful
|
|
1508
|
+
*/
|
|
1509
|
+
__publicField(this, "state", this.stateSubject.getValue());
|
|
1510
|
+
__publicField(this, "options");
|
|
1511
|
+
__publicField(this, "mutation$");
|
|
1512
|
+
this.options = options;
|
|
1513
|
+
const mutationFn = options.mutationFn;
|
|
1514
|
+
this.state.variables = args;
|
|
1515
|
+
this.stateSubject.next(this.state);
|
|
1516
|
+
const mutationFnObservable = typeof mutationFn === "function" ? defer(() => from(mutationFn(args))) : mutationFn;
|
|
1517
|
+
const queryRunner$ = mutationFnObservable.pipe(
|
|
1518
|
+
retryOnError(options),
|
|
1519
|
+
take(1),
|
|
1520
|
+
map((data) => ({ data, isError: false })),
|
|
1521
|
+
catchError((error) => {
|
|
1522
|
+
console.error(error);
|
|
1523
|
+
if (options.onError != null) {
|
|
1524
|
+
options.onError(error, args);
|
|
1525
|
+
}
|
|
1526
|
+
return of({ data: error, isError: true });
|
|
1527
|
+
})
|
|
1528
|
+
);
|
|
1529
|
+
const loading$ = of({
|
|
1530
|
+
status: "pending"
|
|
1531
|
+
});
|
|
1532
|
+
this.mutation$ = merge(
|
|
1533
|
+
loading$,
|
|
1534
|
+
queryRunner$.pipe(
|
|
1535
|
+
map(({ data, isError }) => {
|
|
1536
|
+
if (!isError) {
|
|
1537
|
+
if (options.onSuccess != null)
|
|
1538
|
+
options.onSuccess(data, args);
|
|
1539
|
+
}
|
|
1540
|
+
return isError ? {
|
|
1541
|
+
status: "error",
|
|
1542
|
+
error: data,
|
|
1543
|
+
data: void 0
|
|
1544
|
+
} : {
|
|
1545
|
+
status: "success",
|
|
1546
|
+
error: null,
|
|
1547
|
+
data
|
|
1548
|
+
};
|
|
1549
|
+
})
|
|
1550
|
+
)
|
|
1551
|
+
).pipe(
|
|
1552
|
+
mergeResults,
|
|
1553
|
+
tap((value) => {
|
|
1554
|
+
this.state = { ...this.state, ...value };
|
|
1555
|
+
this.stateSubject.next(this.state);
|
|
1556
|
+
}),
|
|
1557
|
+
options.__queryRunnerHook ?? identity,
|
|
1558
|
+
share()
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1487
1562
|
const createMutationRunner = ({
|
|
1488
1563
|
__queryFinalizeHook,
|
|
1489
1564
|
__queryInitHook,
|
|
1490
1565
|
__queryTriggerHook
|
|
1491
1566
|
}) => {
|
|
1492
1567
|
const trigger$ = new Subject();
|
|
1493
|
-
const
|
|
1568
|
+
const cancel$ = new Subject();
|
|
1494
1569
|
let closed = false;
|
|
1495
1570
|
const mapOperator$ = new BehaviorSubject("merge");
|
|
1496
|
-
const
|
|
1571
|
+
const mutationsSubject = new BehaviorSubject([]);
|
|
1497
1572
|
const destroy = () => {
|
|
1498
1573
|
if (closed) {
|
|
1499
1574
|
throw new Error("Trying to close an already closed mutation");
|
|
1500
1575
|
}
|
|
1501
1576
|
closed = true;
|
|
1502
1577
|
mapOperator$.complete();
|
|
1503
|
-
|
|
1578
|
+
mutationsSubject.complete();
|
|
1504
1579
|
trigger$.complete();
|
|
1505
|
-
|
|
1580
|
+
cancel$.next();
|
|
1581
|
+
cancel$.complete();
|
|
1506
1582
|
};
|
|
1507
1583
|
const stableMapOperator$ = mapOperator$.pipe(
|
|
1508
1584
|
filter(isDefined),
|
|
@@ -1512,29 +1588,35 @@ const createMutationRunner = ({
|
|
|
1512
1588
|
__queryInitHook ?? identity,
|
|
1513
1589
|
mergeMap((mapOperator) => {
|
|
1514
1590
|
const switchOperator = mapOperator === "concat" ? concatMap$1 : mapOperator === "switch" ? switchMap : mergeMap;
|
|
1591
|
+
let mutationsForCurrentMapOperatorSubject = [];
|
|
1592
|
+
const removeMutation = (mutation) => {
|
|
1593
|
+
mutationsForCurrentMapOperatorSubject = mutationsForCurrentMapOperatorSubject.filter(
|
|
1594
|
+
(item) => item !== mutation
|
|
1595
|
+
);
|
|
1596
|
+
mutationsSubject.next(
|
|
1597
|
+
mutationsSubject.getValue().filter((item) => item !== mutation)
|
|
1598
|
+
);
|
|
1599
|
+
};
|
|
1515
1600
|
return trigger$.pipe(
|
|
1516
1601
|
takeUntil(stableMapOperator$.pipe(skip(1))),
|
|
1517
|
-
|
|
1518
|
-
|
|
1602
|
+
map(({ args, options }) => {
|
|
1603
|
+
const mutation = new Mutation({
|
|
1604
|
+
args,
|
|
1605
|
+
...options,
|
|
1606
|
+
mapOperator
|
|
1607
|
+
});
|
|
1608
|
+
mutationsForCurrentMapOperatorSubject = [
|
|
1609
|
+
...mutationsForCurrentMapOperatorSubject,
|
|
1610
|
+
mutation
|
|
1611
|
+
];
|
|
1612
|
+
mutationsSubject.next([...mutationsSubject.getValue(), mutation]);
|
|
1613
|
+
return mutation;
|
|
1519
1614
|
}),
|
|
1520
|
-
switchOperator((
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
const
|
|
1524
|
-
|
|
1525
|
-
take(1),
|
|
1526
|
-
map((data) => ({ data, isError: false })),
|
|
1527
|
-
catchError((error) => {
|
|
1528
|
-
console.error(error);
|
|
1529
|
-
if (options.onError != null) {
|
|
1530
|
-
options.onError(error, args);
|
|
1531
|
-
}
|
|
1532
|
-
return of({ data: error, isError: true });
|
|
1533
|
-
}),
|
|
1534
|
-
share()
|
|
1535
|
-
);
|
|
1536
|
-
const queryIsOver$ = queryRunner$.pipe(
|
|
1537
|
-
map(({ data, isError }) => isError || data)
|
|
1615
|
+
switchOperator((mutation) => {
|
|
1616
|
+
if (!mutationsSubject.getValue().includes(mutation))
|
|
1617
|
+
return of({});
|
|
1618
|
+
const queryIsOver$ = mutation.mutation$.pipe(
|
|
1619
|
+
map(({ data, error }) => error || data)
|
|
1538
1620
|
);
|
|
1539
1621
|
const isThisCurrentFunctionLastOneCalled = trigger$.pipe(
|
|
1540
1622
|
take(1),
|
|
@@ -1542,48 +1624,35 @@ const createMutationRunner = ({
|
|
|
1542
1624
|
startWith(true),
|
|
1543
1625
|
takeUntil(queryIsOver$)
|
|
1544
1626
|
);
|
|
1545
|
-
const
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
queryRunner$,
|
|
1552
|
-
isThisCurrentFunctionLastOneCalled
|
|
1553
|
-
]).pipe(
|
|
1554
|
-
map(([{ data, isError }, isLastMutationCalled]) => {
|
|
1555
|
-
if (!isError) {
|
|
1556
|
-
if (options.onSuccess != null)
|
|
1557
|
-
options.onSuccess(data, args);
|
|
1558
|
-
}
|
|
1559
|
-
if (isLastMutationCalled) {
|
|
1560
|
-
return isError ? {
|
|
1561
|
-
status: "error",
|
|
1562
|
-
error: data,
|
|
1563
|
-
data: void 0
|
|
1564
|
-
} : {
|
|
1565
|
-
status: "success",
|
|
1566
|
-
error: void 0,
|
|
1567
|
-
data
|
|
1568
|
-
};
|
|
1569
|
-
}
|
|
1627
|
+
const result$ = combineLatest([
|
|
1628
|
+
mutation.mutation$,
|
|
1629
|
+
isThisCurrentFunctionLastOneCalled
|
|
1630
|
+
]).pipe(
|
|
1631
|
+
map(([result, isLastMutationCalled]) => {
|
|
1632
|
+
if ((result.status === "success" || result.status === "error") && !isLastMutationCalled) {
|
|
1570
1633
|
return {};
|
|
1571
|
-
}
|
|
1572
|
-
|
|
1573
|
-
)
|
|
1574
|
-
|
|
1575
|
-
|
|
1634
|
+
}
|
|
1635
|
+
return result;
|
|
1636
|
+
}),
|
|
1637
|
+
takeUntil(cancel$.pipe()),
|
|
1638
|
+
mergeResults,
|
|
1576
1639
|
finalize(() => {
|
|
1577
|
-
|
|
1640
|
+
removeMutation(mutation);
|
|
1578
1641
|
})
|
|
1579
1642
|
);
|
|
1643
|
+
return result$;
|
|
1580
1644
|
}),
|
|
1581
|
-
|
|
1582
|
-
|
|
1645
|
+
mergeResults,
|
|
1646
|
+
__queryTriggerHook ?? identity
|
|
1583
1647
|
);
|
|
1584
1648
|
}),
|
|
1585
1649
|
__queryFinalizeHook ?? identity
|
|
1586
1650
|
);
|
|
1651
|
+
cancel$.subscribe(() => {
|
|
1652
|
+
if (mutationsSubject.getValue().length === 0)
|
|
1653
|
+
return;
|
|
1654
|
+
mutationsSubject.next([]);
|
|
1655
|
+
});
|
|
1587
1656
|
return {
|
|
1588
1657
|
mutation$,
|
|
1589
1658
|
trigger: ({
|
|
@@ -1593,12 +1662,29 @@ const createMutationRunner = ({
|
|
|
1593
1662
|
mapOperator$.next(options.mapOperator);
|
|
1594
1663
|
trigger$.next({ args, options });
|
|
1595
1664
|
},
|
|
1596
|
-
|
|
1665
|
+
cancel$,
|
|
1597
1666
|
destroy,
|
|
1598
|
-
|
|
1667
|
+
mutationsSubject,
|
|
1599
1668
|
getClosed: () => closed
|
|
1600
1669
|
};
|
|
1601
1670
|
};
|
|
1671
|
+
const createPredicateForFilters = ({
|
|
1672
|
+
mutationKey,
|
|
1673
|
+
status,
|
|
1674
|
+
predicate
|
|
1675
|
+
} = {}) => {
|
|
1676
|
+
const defaultPredicate = (mutation) => {
|
|
1677
|
+
if (mutationKey !== void 0 && // @todo optimize
|
|
1678
|
+
serializeKey(mutation.options.mutationKey) !== serializeKey(mutationKey)) {
|
|
1679
|
+
return false;
|
|
1680
|
+
}
|
|
1681
|
+
if (status && mutation.stateSubject.getValue().status !== status)
|
|
1682
|
+
return false;
|
|
1683
|
+
return true;
|
|
1684
|
+
};
|
|
1685
|
+
const finalPredicate = predicate ?? defaultPredicate;
|
|
1686
|
+
return finalPredicate;
|
|
1687
|
+
};
|
|
1602
1688
|
class MutationClient {
|
|
1603
1689
|
constructor() {
|
|
1604
1690
|
/**
|
|
@@ -1618,7 +1704,15 @@ class MutationClient {
|
|
|
1618
1704
|
*/
|
|
1619
1705
|
__publicField(this, "mutationResults$", new BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1620
1706
|
__publicField(this, "mutate$", new Subject());
|
|
1621
|
-
__publicField(this, "
|
|
1707
|
+
__publicField(this, "cancel$", new Subject());
|
|
1708
|
+
/**
|
|
1709
|
+
* Observable to track how many running mutations per runner
|
|
1710
|
+
*/
|
|
1711
|
+
__publicField(this, "isMutatingSubject", new BehaviorSubject([]));
|
|
1712
|
+
/**
|
|
1713
|
+
* List of all mutations running
|
|
1714
|
+
*/
|
|
1715
|
+
__publicField(this, "mutationsSubject", new BehaviorSubject([]));
|
|
1622
1716
|
this.mutate$.pipe(
|
|
1623
1717
|
tap(({ options, args }) => {
|
|
1624
1718
|
const { mutationKey } = options;
|
|
@@ -1643,9 +1737,10 @@ class MutationClient {
|
|
|
1643
1737
|
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
1644
1738
|
}
|
|
1645
1739
|
});
|
|
1646
|
-
mutationForKey.
|
|
1740
|
+
mutationForKey.mutationsSubject.pipe(
|
|
1741
|
+
distinctUntilChanged(shallowEqual),
|
|
1647
1742
|
skip(1),
|
|
1648
|
-
filter((
|
|
1743
|
+
filter((items) => items.length === 0)
|
|
1649
1744
|
).subscribe(() => {
|
|
1650
1745
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1651
1746
|
const resultMap = this.mutationResults$.getValue();
|
|
@@ -1657,13 +1752,32 @@ class MutationClient {
|
|
|
1657
1752
|
mutationForKey.trigger({ args, options });
|
|
1658
1753
|
})
|
|
1659
1754
|
).subscribe();
|
|
1660
|
-
this.
|
|
1755
|
+
this.cancel$.pipe(
|
|
1661
1756
|
tap(({ key }) => {
|
|
1662
1757
|
var _a;
|
|
1663
1758
|
const serializedKey = serializeKey(key);
|
|
1664
|
-
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.
|
|
1759
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.cancel$.next();
|
|
1665
1760
|
})
|
|
1666
1761
|
).subscribe();
|
|
1762
|
+
this.mutationRunnersByKey$.pipe(
|
|
1763
|
+
switchMap((mapItem) => {
|
|
1764
|
+
const mutationRunners = Array.from(mapItem.values());
|
|
1765
|
+
const mutations$ = combineLatest(
|
|
1766
|
+
mutationRunners.map(
|
|
1767
|
+
(runner) => runner.mutationsSubject.pipe(
|
|
1768
|
+
map((mutations) => mutations.map((mutation) => mutation))
|
|
1769
|
+
)
|
|
1770
|
+
)
|
|
1771
|
+
);
|
|
1772
|
+
return mutations$.pipe(
|
|
1773
|
+
map(
|
|
1774
|
+
(mutationsByKeys) => mutationsByKeys.reduce((acc, value) => [...acc, ...value], [])
|
|
1775
|
+
)
|
|
1776
|
+
);
|
|
1777
|
+
}),
|
|
1778
|
+
startWith([]),
|
|
1779
|
+
distinctUntilChanged(shallowEqual)
|
|
1780
|
+
).subscribe(this.mutationsSubject);
|
|
1667
1781
|
}
|
|
1668
1782
|
/**
|
|
1669
1783
|
* @helper
|
|
@@ -1687,37 +1801,45 @@ class MutationClient {
|
|
|
1687
1801
|
getMutationRunnersByKey(key) {
|
|
1688
1802
|
return this.mutationRunnersByKey$.getValue().get(key);
|
|
1689
1803
|
}
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1804
|
+
useIsMutating(filters = {}) {
|
|
1805
|
+
const predicate = createPredicateForFilters(filters);
|
|
1806
|
+
const reduceByNumber = (entries) => entries.reduce((acc, mutation) => {
|
|
1807
|
+
return predicate(mutation) && mutation.stateSubject.getValue().status === "pending" ? 1 + acc : acc;
|
|
1808
|
+
}, 0);
|
|
1809
|
+
const lastValue = reduceByNumber(this.mutationsSubject.getValue());
|
|
1810
|
+
const value$ = this.mutationsSubject.pipe(
|
|
1811
|
+
switchMap((mutations) => {
|
|
1812
|
+
const mutationsOnStateUpdate = mutations.map(
|
|
1813
|
+
(mutation) => mutation.stateSubject.pipe(map(() => mutation))
|
|
1814
|
+
);
|
|
1815
|
+
return mutationsOnStateUpdate.length === 0 ? of([]) : combineLatest(mutationsOnStateUpdate);
|
|
1816
|
+
}),
|
|
1817
|
+
map(reduceByNumber),
|
|
1818
|
+
distinctUntilChanged()
|
|
1701
1819
|
);
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1820
|
+
return { value$, lastValue };
|
|
1821
|
+
}
|
|
1822
|
+
mutationState({
|
|
1823
|
+
filters,
|
|
1824
|
+
select
|
|
1825
|
+
} = {}) {
|
|
1826
|
+
const predicate = createPredicateForFilters(filters);
|
|
1827
|
+
const finalSelect = select ?? ((mutation) => mutation.stateSubject.getValue());
|
|
1828
|
+
const lastValue = this.mutationsSubject.getValue().reduce((acc, mutation) => {
|
|
1829
|
+
const result = [...acc, mutation];
|
|
1830
|
+
return result;
|
|
1831
|
+
}, []).filter(predicate).map((mutation) => finalSelect(mutation));
|
|
1832
|
+
const value$ = this.mutationsSubject.pipe(
|
|
1833
|
+
switchMap((mutations) => {
|
|
1834
|
+
const mutationsOnStateUpdate = mutations.map(
|
|
1835
|
+
(mutation) => mutation.stateSubject.pipe(
|
|
1836
|
+
filter(() => predicate(mutation)),
|
|
1837
|
+
map(() => finalSelect(mutation))
|
|
1715
1838
|
)
|
|
1716
|
-
|
|
1717
|
-
return
|
|
1839
|
+
);
|
|
1840
|
+
return mutationsOnStateUpdate.length === 0 ? of([]) : combineLatest(mutationsOnStateUpdate);
|
|
1718
1841
|
}),
|
|
1719
|
-
|
|
1720
|
-
distinctUntilChanged()
|
|
1842
|
+
distinctUntilChanged(shallowEqual)
|
|
1721
1843
|
);
|
|
1722
1844
|
return { value$, lastValue };
|
|
1723
1845
|
}
|
|
@@ -1759,17 +1881,19 @@ class MutationClient {
|
|
|
1759
1881
|
this.mutate$.next(params);
|
|
1760
1882
|
}
|
|
1761
1883
|
/**
|
|
1762
|
-
* This will
|
|
1884
|
+
* This will cancel any current mutation runnings.
|
|
1763
1885
|
* No discrimination process
|
|
1764
1886
|
*/
|
|
1765
|
-
|
|
1766
|
-
this.
|
|
1887
|
+
cancel(params) {
|
|
1888
|
+
this.cancel$.next(params);
|
|
1767
1889
|
}
|
|
1768
1890
|
destroy() {
|
|
1769
|
-
this.
|
|
1891
|
+
this.cancel$.complete();
|
|
1770
1892
|
this.mutate$.complete();
|
|
1771
1893
|
this.mutationResults$.complete();
|
|
1772
1894
|
this.mutationRunnersByKey$.complete();
|
|
1895
|
+
this.isMutatingSubject.complete();
|
|
1896
|
+
this.mutationsSubject.complete();
|
|
1773
1897
|
}
|
|
1774
1898
|
}
|
|
1775
1899
|
const createClient = () => {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BehaviorSubject, type Observable } from "rxjs";
|
|
2
|
+
import { type MutationState, type MutationOptions, type MutationResult } from "./types";
|
|
3
|
+
export declare class Mutation<Data> {
|
|
4
|
+
stateSubject: BehaviorSubject<MutationState<Data, unknown, void, unknown>>;
|
|
5
|
+
/**
|
|
6
|
+
* @important
|
|
7
|
+
* proxy for rect-query, not really useful
|
|
8
|
+
*/
|
|
9
|
+
state: MutationState<Data>;
|
|
10
|
+
options: MutationOptions<Data, any>;
|
|
11
|
+
mutation$: Observable<MutationResult<Data>>;
|
|
12
|
+
constructor({ args, ...options }: {
|
|
13
|
+
args: any;
|
|
14
|
+
} & MutationOptions<Data, any>);
|
|
15
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Subject, BehaviorSubject, type Observable } from "rxjs";
|
|
2
|
-
import { type MutationResult, type MutationOptions, type MutationObservedResult, type MutationFilters, type MutationKey } from "./types";
|
|
2
|
+
import { type MutationResult, type MutationOptions, type MutationObservedResult, type MutationFilters, type MutationKey, type MutationState } from "./types";
|
|
3
3
|
import { type QueryKey } from "../keys/types";
|
|
4
|
+
import { type Mutation } from "./Mutation";
|
|
4
5
|
export declare class MutationClient {
|
|
5
6
|
/**
|
|
6
7
|
* Contain all active mutation for a given key.
|
|
@@ -15,9 +16,9 @@ export declare class MutationClient {
|
|
|
15
16
|
args: unknown;
|
|
16
17
|
options: MutationOptions<unknown, unknown>;
|
|
17
18
|
}) => void;
|
|
18
|
-
|
|
19
|
+
cancel$: Subject<void>;
|
|
19
20
|
destroy: () => void;
|
|
20
|
-
|
|
21
|
+
mutationsSubject: BehaviorSubject<Mutation<any>[]>;
|
|
21
22
|
getClosed: () => boolean;
|
|
22
23
|
} & {
|
|
23
24
|
mutationKey: MutationKey;
|
|
@@ -34,9 +35,17 @@ export declare class MutationClient {
|
|
|
34
35
|
options: MutationOptions<any, any>;
|
|
35
36
|
args: any;
|
|
36
37
|
}>;
|
|
37
|
-
|
|
38
|
+
cancel$: Subject<{
|
|
38
39
|
key: QueryKey;
|
|
39
40
|
}>;
|
|
41
|
+
/**
|
|
42
|
+
* Observable to track how many running mutations per runner
|
|
43
|
+
*/
|
|
44
|
+
isMutatingSubject: BehaviorSubject<(readonly [MutationKey, number])[]>;
|
|
45
|
+
/**
|
|
46
|
+
* List of all mutations running
|
|
47
|
+
*/
|
|
48
|
+
mutationsSubject: BehaviorSubject<Mutation<any>[]>;
|
|
40
49
|
constructor();
|
|
41
50
|
/**
|
|
42
51
|
* @helper
|
|
@@ -55,20 +64,24 @@ export declare class MutationClient {
|
|
|
55
64
|
args: unknown;
|
|
56
65
|
options: MutationOptions<unknown, unknown>;
|
|
57
66
|
}) => void;
|
|
58
|
-
|
|
67
|
+
cancel$: Subject<void>;
|
|
59
68
|
destroy: () => void;
|
|
60
|
-
|
|
69
|
+
mutationsSubject: BehaviorSubject<Mutation<any>[]>;
|
|
61
70
|
getClosed: () => boolean;
|
|
62
71
|
} & {
|
|
63
72
|
mutationKey: MutationKey;
|
|
64
73
|
}) | undefined;
|
|
65
|
-
|
|
66
|
-
* @returns number of mutation runnings
|
|
67
|
-
*/
|
|
68
|
-
runningMutations({ mutationKey, predicate }?: MutationFilters): {
|
|
74
|
+
useIsMutating<TData>(filters?: MutationFilters<TData>): {
|
|
69
75
|
value$: Observable<number>;
|
|
70
76
|
lastValue: number;
|
|
71
77
|
};
|
|
78
|
+
mutationState<TData, Selected = MutationState<TData>>({ filters, select }?: {
|
|
79
|
+
filters?: MutationFilters<TData>;
|
|
80
|
+
select?: (mutation: Mutation<TData>) => Selected;
|
|
81
|
+
}): {
|
|
82
|
+
value$: Observable<Selected[]>;
|
|
83
|
+
lastValue: Selected[];
|
|
84
|
+
};
|
|
72
85
|
observe<Result>({ key }: {
|
|
73
86
|
key: string;
|
|
74
87
|
}): {
|
|
@@ -80,10 +93,10 @@ export declare class MutationClient {
|
|
|
80
93
|
args: MutationArgs;
|
|
81
94
|
}): void;
|
|
82
95
|
/**
|
|
83
|
-
* This will
|
|
96
|
+
* This will cancel any current mutation runnings.
|
|
84
97
|
* No discrimination process
|
|
85
98
|
*/
|
|
86
|
-
|
|
99
|
+
cancel(params: {
|
|
87
100
|
key: QueryKey;
|
|
88
101
|
}): void;
|
|
89
102
|
destroy(): void;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { BehaviorSubject, Subject } from "rxjs";
|
|
2
|
-
import { type MutationOptions
|
|
2
|
+
import { type MutationOptions } from "./types";
|
|
3
|
+
import { Mutation } from "./Mutation";
|
|
4
|
+
export type MutationRunner = ReturnType<typeof createMutationRunner>;
|
|
3
5
|
export declare const createMutationRunner: <T, MutationArg>({ __queryFinalizeHook, __queryInitHook, __queryTriggerHook }: Pick<MutationOptions<any, any>, "__queryInitHook" | "__queryTriggerHook" | "__queryFinalizeHook">) => {
|
|
4
|
-
mutation$: import("rxjs").Observable<MutationResult<
|
|
6
|
+
mutation$: import("rxjs").Observable<import("./types").MutationResult<unknown>>;
|
|
5
7
|
trigger: ({ args, options }: {
|
|
6
8
|
args: MutationArg;
|
|
7
9
|
options: MutationOptions<T, MutationArg>;
|
|
8
10
|
}) => void;
|
|
9
|
-
|
|
11
|
+
cancel$: Subject<void>;
|
|
10
12
|
destroy: () => void;
|
|
11
|
-
|
|
13
|
+
mutationsSubject: BehaviorSubject<Mutation<any>[]>;
|
|
12
14
|
getClosed: () => boolean;
|
|
13
15
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type MonoTypeOperatorFunction, type Observable } from "rxjs";
|
|
2
2
|
import { type Query, type QueryResult } from "../types";
|
|
3
|
+
import { type Mutation } from "./Mutation";
|
|
3
4
|
/**
|
|
4
5
|
* The default value `merge` is suitable for most use case.
|
|
5
6
|
* You should not have to worry too much about it and only consider changing
|
|
@@ -25,19 +26,14 @@ export type MutationKey = unknown[];
|
|
|
25
26
|
/**
|
|
26
27
|
* @todo this should be used in a lot of place so we can probably make a helper for that
|
|
27
28
|
*/
|
|
28
|
-
export interface MutationFilters {
|
|
29
|
+
export interface MutationFilters<TData> {
|
|
29
30
|
/**
|
|
30
31
|
* Match mutation key exactly
|
|
31
32
|
*/
|
|
32
|
-
exact?: boolean;
|
|
33
33
|
/**
|
|
34
34
|
* Include mutations matching this predicate function
|
|
35
35
|
*/
|
|
36
|
-
predicate?: (mutation:
|
|
37
|
-
options: {
|
|
38
|
-
mutationKey: MutationKey;
|
|
39
|
-
};
|
|
40
|
-
}) => boolean;
|
|
36
|
+
predicate?: (mutation: Mutation<TData>) => boolean;
|
|
41
37
|
/**
|
|
42
38
|
* Include mutations matching this mutation key
|
|
43
39
|
*/
|
|
@@ -59,7 +55,7 @@ export interface MutationObservedResult<R> extends MutationResult<R> {
|
|
|
59
55
|
isPending: boolean;
|
|
60
56
|
isPaused: boolean;
|
|
61
57
|
}
|
|
62
|
-
export type MutationFn<
|
|
58
|
+
export type MutationFn<Data, MutationArg> = Observable<Data> | ((arg: MutationArg) => Promise<Data>) | ((arg: MutationArg) => Observable<Data>);
|
|
63
59
|
export interface MutationOptions<Result, MutationArg> {
|
|
64
60
|
enabled?: boolean;
|
|
65
61
|
retry?: false | number | ((attempt: number, error: unknown) => boolean);
|
|
@@ -90,3 +86,11 @@ export interface MutationOptions<Result, MutationArg> {
|
|
|
90
86
|
__queryTriggerHook?: MonoTypeOperatorFunction<Partial<Result>>;
|
|
91
87
|
__queryFinalizeHook?: MonoTypeOperatorFunction<Partial<Result>>;
|
|
92
88
|
}
|
|
89
|
+
export interface MutationState<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> {
|
|
90
|
+
context: TContext | undefined;
|
|
91
|
+
data: TData | undefined;
|
|
92
|
+
error: TError;
|
|
93
|
+
status: MutationStatus;
|
|
94
|
+
variables: TVariables | undefined;
|
|
95
|
+
submittedAt: number;
|
|
96
|
+
}
|
|
@@ -9,6 +9,11 @@ export interface QueryResult<T> {
|
|
|
9
9
|
status: "loading" | "error" | "success";
|
|
10
10
|
error: unknown;
|
|
11
11
|
}
|
|
12
|
+
export interface Register {
|
|
13
|
+
}
|
|
14
|
+
export type DefaultError = Register extends {
|
|
15
|
+
defaultError: infer TError;
|
|
16
|
+
} ? TError : Error;
|
|
12
17
|
export interface Query {
|
|
13
18
|
}
|
|
14
19
|
export interface QueryCommand<T> {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
import { type MutationFilters } from "../../client/mutations/types";
|
|
2
|
-
import { type
|
|
3
|
-
export
|
|
1
|
+
import { type MutationState, type MutationFilters } from "../../client/mutations/types";
|
|
2
|
+
import { type Mutation } from "../../client/mutations/Mutation";
|
|
3
|
+
export interface MutationStateOptions<TResult, TData> {
|
|
4
|
+
filters?: MutationFilters<TData>;
|
|
5
|
+
select?: (mutation: Mutation<any>) => TResult;
|
|
6
|
+
}
|
|
7
|
+
export declare const useMutationState: <TData, TResult = MutationState<unknown, unknown, void, unknown>>({ filters, select }?: MutationStateOptions<TResult, TData>) => TResult[];
|
package/dist/tests/utils.d.ts
CHANGED
|
@@ -19,3 +19,4 @@ export declare function renderWithClient(client: QueryClient, ui: React.ReactEle
|
|
|
19
19
|
* @see https://github.com/TanStack/query/blob/main/packages/react-query/src/__tests__/utils.tsx
|
|
20
20
|
*/
|
|
21
21
|
export declare function createQueryClient(): QueryClient;
|
|
22
|
+
export declare const doNotExecute: (_func: () => void) => boolean;
|