reactjrx 1.60.0 → 1.62.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 +316 -164
- package/dist/index.js +317 -165
- package/dist/lib/queries/client/mutations/Mutation.d.ts +15 -0
- package/dist/lib/queries/client/mutations/MutationClient.d.ts +25 -25
- package/dist/lib/queries/client/mutations/MutationResultObserver.d.ts +32 -0
- 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/operators.d.ts +2 -2
- package/dist/lib/queries/client/mutations/types.d.ts +76 -21
- 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 +7 -5
- 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
|
@@ -465,7 +465,9 @@ function useMutation(options, queryClient) {
|
|
|
465
465
|
const defaultKey = useConstant(() => [nanoid()]);
|
|
466
466
|
const key = serializeKey(options.mutationKey ?? defaultKey.current);
|
|
467
467
|
const observedMutation = react.useMemo(
|
|
468
|
-
() => finalQueryClient.mutationClient.observe({
|
|
468
|
+
() => finalQueryClient.mutationClient.mutationResultObserver.observe({
|
|
469
|
+
key
|
|
470
|
+
}),
|
|
469
471
|
[key]
|
|
470
472
|
);
|
|
471
473
|
const result = useObserve(observedMutation.result$) ?? observedMutation.lastValue;
|
|
@@ -481,21 +483,21 @@ function useMutation(options, queryClient) {
|
|
|
481
483
|
},
|
|
482
484
|
[finalQueryClient, key]
|
|
483
485
|
);
|
|
484
|
-
const
|
|
485
|
-
finalQueryClient.mutationClient.
|
|
486
|
+
const cancel = react.useCallback(() => {
|
|
487
|
+
finalQueryClient.mutationClient.cancel({
|
|
486
488
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
487
489
|
});
|
|
488
490
|
}, [finalQueryClient]);
|
|
489
491
|
react.useEffect(() => {
|
|
490
492
|
return () => {
|
|
491
493
|
if (optionsRef.current.cancelOnUnMount) {
|
|
492
|
-
finalQueryClient.mutationClient.
|
|
494
|
+
finalQueryClient.mutationClient.cancel({
|
|
493
495
|
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
494
496
|
});
|
|
495
497
|
}
|
|
496
498
|
};
|
|
497
499
|
}, []);
|
|
498
|
-
return { mutate,
|
|
500
|
+
return { mutate, cancel, ...result };
|
|
499
501
|
}
|
|
500
502
|
const arrayEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
501
503
|
function shallowEqual(objA, objB) {
|
|
@@ -1468,43 +1470,112 @@ const dispatchExternalRefetchToAllQueries = ({
|
|
|
1468
1470
|
}),
|
|
1469
1471
|
rxjs.filter((trigger2) => trigger2.type !== "refetch")
|
|
1470
1472
|
);
|
|
1473
|
+
const getDefaultMutationState = () => ({
|
|
1474
|
+
context: void 0,
|
|
1475
|
+
data: void 0,
|
|
1476
|
+
error: null,
|
|
1477
|
+
status: "idle",
|
|
1478
|
+
submittedAt: 0,
|
|
1479
|
+
variables: void 0
|
|
1480
|
+
});
|
|
1471
1481
|
const mergeResults = (stream$) => stream$.pipe(
|
|
1472
|
-
rxjs.scan(
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
}
|
|
1479
|
-
|
|
1480
|
-
data: void 0,
|
|
1481
|
-
error: void 0,
|
|
1482
|
-
status: "pending"
|
|
1483
|
-
}
|
|
1484
|
-
),
|
|
1482
|
+
rxjs.scan((acc, current) => {
|
|
1483
|
+
return {
|
|
1484
|
+
...acc,
|
|
1485
|
+
...current,
|
|
1486
|
+
data: current.data ?? acc.data,
|
|
1487
|
+
error: current.error ?? acc.error
|
|
1488
|
+
};
|
|
1489
|
+
}, getDefaultMutationState()),
|
|
1485
1490
|
rxjs.distinctUntilChanged(
|
|
1486
1491
|
({ data: prevData, ...prev }, { data: currData, ...curr }) => shallowEqual(prev, curr) && shallowEqual(prevData, currData)
|
|
1487
1492
|
)
|
|
1488
1493
|
);
|
|
1494
|
+
class Mutation {
|
|
1495
|
+
constructor({
|
|
1496
|
+
args,
|
|
1497
|
+
...options
|
|
1498
|
+
}) {
|
|
1499
|
+
__publicField(this, "stateSubject", new rxjs.ReplaySubject(1));
|
|
1500
|
+
/**
|
|
1501
|
+
* @important
|
|
1502
|
+
* convenience over usage of stateSubject for sync access
|
|
1503
|
+
*/
|
|
1504
|
+
__publicField(this, "state", getDefaultMutationState());
|
|
1505
|
+
__publicField(this, "options");
|
|
1506
|
+
__publicField(this, "mutation$");
|
|
1507
|
+
this.options = options;
|
|
1508
|
+
const mutationFn = options.mutationFn;
|
|
1509
|
+
this.state.variables = args;
|
|
1510
|
+
this.stateSubject.next(this.state);
|
|
1511
|
+
const mutationFnObservable = typeof mutationFn === "function" ? rxjs.defer(() => rxjs.from(mutationFn(args))) : mutationFn;
|
|
1512
|
+
const queryRunner$ = mutationFnObservable.pipe(
|
|
1513
|
+
retryOnError(options),
|
|
1514
|
+
rxjs.take(1),
|
|
1515
|
+
rxjs.map((data) => ({ data, isError: false })),
|
|
1516
|
+
rxjs.catchError((error) => {
|
|
1517
|
+
console.error(error);
|
|
1518
|
+
if (options.onError != null) {
|
|
1519
|
+
options.onError(error, args);
|
|
1520
|
+
}
|
|
1521
|
+
return rxjs.of({ data: error, isError: true });
|
|
1522
|
+
})
|
|
1523
|
+
);
|
|
1524
|
+
const initState$ = rxjs.of({
|
|
1525
|
+
...this.state,
|
|
1526
|
+
status: "pending",
|
|
1527
|
+
submittedAt: (/* @__PURE__ */ new Date()).getTime()
|
|
1528
|
+
});
|
|
1529
|
+
this.mutation$ = rxjs.merge(
|
|
1530
|
+
initState$,
|
|
1531
|
+
queryRunner$.pipe(
|
|
1532
|
+
rxjs.map(({ data, isError }) => {
|
|
1533
|
+
if (!isError) {
|
|
1534
|
+
if (options.onSuccess != null)
|
|
1535
|
+
options.onSuccess(data, args);
|
|
1536
|
+
}
|
|
1537
|
+
return isError ? {
|
|
1538
|
+
status: "error",
|
|
1539
|
+
error: data,
|
|
1540
|
+
data: void 0
|
|
1541
|
+
} : {
|
|
1542
|
+
status: "success",
|
|
1543
|
+
error: null,
|
|
1544
|
+
data
|
|
1545
|
+
};
|
|
1546
|
+
})
|
|
1547
|
+
)
|
|
1548
|
+
).pipe(
|
|
1549
|
+
mergeResults,
|
|
1550
|
+
rxjs.tap((value) => {
|
|
1551
|
+
this.state = { ...this.state, ...value };
|
|
1552
|
+
this.stateSubject.next(this.state);
|
|
1553
|
+
}),
|
|
1554
|
+
options.__queryRunnerHook ?? rxjs.identity,
|
|
1555
|
+
rxjs.share()
|
|
1556
|
+
);
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1489
1559
|
const createMutationRunner = ({
|
|
1490
1560
|
__queryFinalizeHook,
|
|
1491
1561
|
__queryInitHook,
|
|
1492
1562
|
__queryTriggerHook
|
|
1493
1563
|
}) => {
|
|
1494
1564
|
const trigger$ = new rxjs.Subject();
|
|
1495
|
-
const
|
|
1565
|
+
const cancel$ = new rxjs.Subject();
|
|
1496
1566
|
let closed = false;
|
|
1497
1567
|
const mapOperator$ = new rxjs.BehaviorSubject("merge");
|
|
1498
|
-
const
|
|
1568
|
+
const mutationsSubject = new rxjs.BehaviorSubject([]);
|
|
1499
1569
|
const destroy = () => {
|
|
1500
1570
|
if (closed) {
|
|
1501
1571
|
throw new Error("Trying to close an already closed mutation");
|
|
1502
1572
|
}
|
|
1503
1573
|
closed = true;
|
|
1504
1574
|
mapOperator$.complete();
|
|
1505
|
-
|
|
1575
|
+
mutationsSubject.complete();
|
|
1506
1576
|
trigger$.complete();
|
|
1507
|
-
|
|
1577
|
+
cancel$.next();
|
|
1578
|
+
cancel$.complete();
|
|
1508
1579
|
};
|
|
1509
1580
|
const stableMapOperator$ = mapOperator$.pipe(
|
|
1510
1581
|
rxjs.filter(isDefined),
|
|
@@ -1514,29 +1585,35 @@ const createMutationRunner = ({
|
|
|
1514
1585
|
__queryInitHook ?? rxjs.identity,
|
|
1515
1586
|
rxjs.mergeMap((mapOperator) => {
|
|
1516
1587
|
const switchOperator = mapOperator === "concat" ? rxjs.concatMap : mapOperator === "switch" ? rxjs.switchMap : rxjs.mergeMap;
|
|
1588
|
+
let mutationsForCurrentMapOperatorSubject = [];
|
|
1589
|
+
const removeMutation = (mutation) => {
|
|
1590
|
+
mutationsForCurrentMapOperatorSubject = mutationsForCurrentMapOperatorSubject.filter(
|
|
1591
|
+
(item) => item !== mutation
|
|
1592
|
+
);
|
|
1593
|
+
mutationsSubject.next(
|
|
1594
|
+
mutationsSubject.getValue().filter((item) => item !== mutation)
|
|
1595
|
+
);
|
|
1596
|
+
};
|
|
1517
1597
|
return trigger$.pipe(
|
|
1518
1598
|
rxjs.takeUntil(stableMapOperator$.pipe(rxjs.skip(1))),
|
|
1519
|
-
rxjs.
|
|
1520
|
-
|
|
1599
|
+
rxjs.map(({ args, options }) => {
|
|
1600
|
+
const mutation = new Mutation({
|
|
1601
|
+
args,
|
|
1602
|
+
...options,
|
|
1603
|
+
mapOperator
|
|
1604
|
+
});
|
|
1605
|
+
mutationsForCurrentMapOperatorSubject = [
|
|
1606
|
+
...mutationsForCurrentMapOperatorSubject,
|
|
1607
|
+
mutation
|
|
1608
|
+
];
|
|
1609
|
+
mutationsSubject.next([...mutationsSubject.getValue(), mutation]);
|
|
1610
|
+
return mutation;
|
|
1521
1611
|
}),
|
|
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)
|
|
1612
|
+
switchOperator((mutation) => {
|
|
1613
|
+
if (!mutationsSubject.getValue().includes(mutation))
|
|
1614
|
+
return rxjs.of({});
|
|
1615
|
+
const queryIsOver$ = mutation.mutation$.pipe(
|
|
1616
|
+
rxjs.map(({ data, error }) => error || data)
|
|
1540
1617
|
);
|
|
1541
1618
|
const isThisCurrentFunctionLastOneCalled = trigger$.pipe(
|
|
1542
1619
|
rxjs.take(1),
|
|
@@ -1544,48 +1621,36 @@ const createMutationRunner = ({
|
|
|
1544
1621
|
rxjs.startWith(true),
|
|
1545
1622
|
rxjs.takeUntil(queryIsOver$)
|
|
1546
1623
|
);
|
|
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
|
-
}
|
|
1624
|
+
const result$ = rxjs.combineLatest([
|
|
1625
|
+
mutation.mutation$,
|
|
1626
|
+
isThisCurrentFunctionLastOneCalled
|
|
1627
|
+
]).pipe(
|
|
1628
|
+
rxjs.map(([result, isLastMutationCalled]) => {
|
|
1629
|
+
if ((result.status === "success" || result.status === "error") && !isLastMutationCalled) {
|
|
1572
1630
|
return {};
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
)
|
|
1576
|
-
|
|
1577
|
-
|
|
1631
|
+
}
|
|
1632
|
+
return result;
|
|
1633
|
+
}),
|
|
1634
|
+
rxjs.takeUntil(cancel$.pipe()),
|
|
1635
|
+
mergeResults,
|
|
1578
1636
|
rxjs.finalize(() => {
|
|
1579
|
-
|
|
1637
|
+
removeMutation(mutation);
|
|
1580
1638
|
})
|
|
1581
1639
|
);
|
|
1640
|
+
return result$;
|
|
1582
1641
|
}),
|
|
1583
|
-
|
|
1584
|
-
|
|
1642
|
+
mergeResults,
|
|
1643
|
+
__queryTriggerHook ?? rxjs.identity
|
|
1585
1644
|
);
|
|
1586
1645
|
}),
|
|
1587
|
-
__queryFinalizeHook ?? rxjs.identity
|
|
1646
|
+
__queryFinalizeHook ?? rxjs.identity,
|
|
1647
|
+
rxjs.shareReplay(1)
|
|
1588
1648
|
);
|
|
1649
|
+
cancel$.subscribe(() => {
|
|
1650
|
+
if (mutationsSubject.getValue().length === 0)
|
|
1651
|
+
return;
|
|
1652
|
+
mutationsSubject.next([]);
|
|
1653
|
+
});
|
|
1589
1654
|
return {
|
|
1590
1655
|
mutation$,
|
|
1591
1656
|
trigger: ({
|
|
@@ -1595,36 +1660,138 @@ const createMutationRunner = ({
|
|
|
1595
1660
|
mapOperator$.next(options.mapOperator);
|
|
1596
1661
|
trigger$.next({ args, options });
|
|
1597
1662
|
},
|
|
1598
|
-
|
|
1663
|
+
cancel$,
|
|
1599
1664
|
destroy,
|
|
1600
|
-
|
|
1665
|
+
mutationsSubject,
|
|
1601
1666
|
getClosed: () => closed
|
|
1602
1667
|
};
|
|
1603
1668
|
};
|
|
1604
|
-
|
|
1605
|
-
|
|
1669
|
+
const createPredicateForFilters = ({
|
|
1670
|
+
mutationKey,
|
|
1671
|
+
status,
|
|
1672
|
+
predicate
|
|
1673
|
+
} = {}) => {
|
|
1674
|
+
const defaultPredicate = (mutation) => {
|
|
1675
|
+
if (mutationKey !== void 0 && // @todo optimize
|
|
1676
|
+
serializeKey(mutation.options.mutationKey) !== serializeKey(mutationKey)) {
|
|
1677
|
+
return false;
|
|
1678
|
+
}
|
|
1679
|
+
if (status && mutation.state.status !== status)
|
|
1680
|
+
return false;
|
|
1681
|
+
return true;
|
|
1682
|
+
};
|
|
1683
|
+
const finalPredicate = predicate ?? defaultPredicate;
|
|
1684
|
+
return finalPredicate;
|
|
1685
|
+
};
|
|
1686
|
+
class MutationResultObserver {
|
|
1687
|
+
constructor(mutationRunnersByKey$) {
|
|
1606
1688
|
/**
|
|
1607
|
-
*
|
|
1608
|
-
*
|
|
1689
|
+
* Mutation result subject. It can be used whether there is a mutation
|
|
1690
|
+
* running or not and can be directly observed.
|
|
1609
1691
|
*
|
|
1610
1692
|
* @important
|
|
1611
1693
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1612
1694
|
*/
|
|
1613
|
-
__publicField(this, "
|
|
1695
|
+
__publicField(this, "mutationResults$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1696
|
+
mutationRunnersByKey$.pipe(
|
|
1697
|
+
rxjs.switchMap((mapItem) => {
|
|
1698
|
+
const runners = Array.from(mapItem.values()).map((runner) => {
|
|
1699
|
+
const serializedMutationKey = serializeKey(runner.mutationKey);
|
|
1700
|
+
return runner.mutation$.pipe(
|
|
1701
|
+
rxjs.tap((result) => {
|
|
1702
|
+
this.updateResultForKey({
|
|
1703
|
+
serializedMutationKey,
|
|
1704
|
+
result
|
|
1705
|
+
});
|
|
1706
|
+
}),
|
|
1707
|
+
rxjs.finalize(() => {
|
|
1708
|
+
this.deleteResultForKey({ serializedMutationKey });
|
|
1709
|
+
})
|
|
1710
|
+
);
|
|
1711
|
+
});
|
|
1712
|
+
return rxjs.merge(...runners);
|
|
1713
|
+
})
|
|
1714
|
+
).subscribe();
|
|
1715
|
+
}
|
|
1716
|
+
getDefaultResultValue() {
|
|
1717
|
+
return {
|
|
1718
|
+
...getDefaultMutationState(),
|
|
1719
|
+
isSuccess: false,
|
|
1720
|
+
isPending: false,
|
|
1721
|
+
isIdle: true,
|
|
1722
|
+
isError: false
|
|
1723
|
+
};
|
|
1724
|
+
}
|
|
1725
|
+
deleteResultForKey({
|
|
1726
|
+
serializedMutationKey
|
|
1727
|
+
}) {
|
|
1728
|
+
const resultMap = this.mutationResults$.getValue();
|
|
1729
|
+
resultMap.delete(serializedMutationKey);
|
|
1730
|
+
this.mutationResults$.next(resultMap);
|
|
1731
|
+
}
|
|
1732
|
+
updateResultForKey({
|
|
1733
|
+
serializedMutationKey,
|
|
1734
|
+
result
|
|
1735
|
+
}) {
|
|
1736
|
+
const resultForKeySubject = this.mutationResults$.getValue().get(serializedMutationKey);
|
|
1737
|
+
const valueForResult = {
|
|
1738
|
+
...this.getDefaultResultValue(),
|
|
1739
|
+
...result,
|
|
1740
|
+
isSuccess: result.status === "success",
|
|
1741
|
+
isPending: result.status === "pending",
|
|
1742
|
+
isIdle: result.status === "idle",
|
|
1743
|
+
isError: result.status === "error"
|
|
1744
|
+
};
|
|
1745
|
+
if (!resultForKeySubject) {
|
|
1746
|
+
const resultMap = this.mutationResults$.getValue();
|
|
1747
|
+
resultMap.set(
|
|
1748
|
+
serializedMutationKey,
|
|
1749
|
+
new rxjs.BehaviorSubject(valueForResult)
|
|
1750
|
+
);
|
|
1751
|
+
this.mutationResults$.next(resultMap);
|
|
1752
|
+
} else {
|
|
1753
|
+
resultForKeySubject == null ? void 0 : resultForKeySubject.next(valueForResult);
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
observe({ key }) {
|
|
1757
|
+
var _a;
|
|
1758
|
+
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
1759
|
+
const lastValue = currentResultValue ?? this.getDefaultResultValue();
|
|
1760
|
+
const result$ = this.mutationResults$.pipe(
|
|
1761
|
+
rxjs.switchMap((resultMap) => {
|
|
1762
|
+
const subject = resultMap.get(key);
|
|
1763
|
+
return subject ?? rxjs.EMPTY;
|
|
1764
|
+
})
|
|
1765
|
+
).pipe(rxjs.filter(isDefined));
|
|
1766
|
+
return { result$, lastValue };
|
|
1767
|
+
}
|
|
1768
|
+
destroy() {
|
|
1769
|
+
this.mutationResults$.complete();
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
class MutationClient {
|
|
1773
|
+
constructor() {
|
|
1614
1774
|
/**
|
|
1615
|
-
*
|
|
1616
|
-
* running
|
|
1775
|
+
* Contain all active mutation for a given key.
|
|
1776
|
+
* A mutation ca have several triggers running (it is not necessarily one function running)
|
|
1617
1777
|
*
|
|
1618
1778
|
* @important
|
|
1619
1779
|
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1620
1780
|
*/
|
|
1621
|
-
__publicField(this, "
|
|
1781
|
+
__publicField(this, "mutationRunnersByKey$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1622
1782
|
__publicField(this, "mutate$", new rxjs.Subject());
|
|
1623
|
-
__publicField(this, "
|
|
1783
|
+
__publicField(this, "cancel$", new rxjs.Subject());
|
|
1624
1784
|
/**
|
|
1625
1785
|
* Observable to track how many running mutations per runner
|
|
1626
1786
|
*/
|
|
1627
1787
|
__publicField(this, "isMutatingSubject", new rxjs.BehaviorSubject([]));
|
|
1788
|
+
/**
|
|
1789
|
+
* List of all mutations running
|
|
1790
|
+
*/
|
|
1791
|
+
__publicField(this, "mutationsSubject", new rxjs.BehaviorSubject([]));
|
|
1792
|
+
__publicField(this, "mutationResultObserver", new MutationResultObserver(
|
|
1793
|
+
this.mutationRunnersByKey$
|
|
1794
|
+
));
|
|
1628
1795
|
this.mutate$.pipe(
|
|
1629
1796
|
rxjs.tap(({ options, args }) => {
|
|
1630
1797
|
const { mutationKey } = options;
|
|
@@ -1638,53 +1805,45 @@ class MutationClient {
|
|
|
1638
1805
|
mutationKey
|
|
1639
1806
|
};
|
|
1640
1807
|
this.setMutationRunnersByKey(serializedMutationKey, mutationForKey);
|
|
1641
|
-
mutationForKey.mutation$.subscribe(
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
resultForKeySubject = new rxjs.BehaviorSubject(result);
|
|
1645
|
-
const resultMap = this.mutationResults$.getValue();
|
|
1646
|
-
resultMap.set(serializedMutationKey, resultForKeySubject);
|
|
1647
|
-
this.mutationResults$.next(resultMap);
|
|
1648
|
-
} else {
|
|
1649
|
-
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
1650
|
-
}
|
|
1651
|
-
});
|
|
1652
|
-
mutationForKey.mutationsRunning$.pipe(
|
|
1808
|
+
mutationForKey.mutation$.subscribe();
|
|
1809
|
+
mutationForKey.mutationsSubject.pipe(
|
|
1810
|
+
rxjs.distinctUntilChanged(shallowEqual),
|
|
1653
1811
|
rxjs.skip(1),
|
|
1654
|
-
rxjs.filter((
|
|
1812
|
+
rxjs.filter((items) => items.length === 0)
|
|
1655
1813
|
).subscribe(() => {
|
|
1656
1814
|
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1657
|
-
const resultMap = this.mutationResults$.getValue();
|
|
1658
|
-
resultMap.delete(serializedMutationKey);
|
|
1659
|
-
this.mutationResults$.next(resultMap);
|
|
1660
1815
|
this.deleteMutationRunnersByKey(serializedMutationKey);
|
|
1661
1816
|
});
|
|
1662
1817
|
}
|
|
1663
1818
|
mutationForKey.trigger({ args, options });
|
|
1664
1819
|
})
|
|
1665
1820
|
).subscribe();
|
|
1666
|
-
this.
|
|
1821
|
+
this.cancel$.pipe(
|
|
1667
1822
|
rxjs.tap(({ key }) => {
|
|
1668
1823
|
var _a;
|
|
1669
1824
|
const serializedKey = serializeKey(key);
|
|
1670
|
-
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.
|
|
1825
|
+
(_a = this.mutationRunnersByKey$.getValue().get(serializedKey)) == null ? void 0 : _a.cancel$.next();
|
|
1671
1826
|
})
|
|
1672
1827
|
).subscribe();
|
|
1673
1828
|
this.mutationRunnersByKey$.pipe(
|
|
1674
1829
|
rxjs.switchMap((mapItem) => {
|
|
1675
|
-
const mutationRunners = Array.from(mapItem.
|
|
1676
|
-
|
|
1677
|
-
|
|
1830
|
+
const mutationRunners = Array.from(mapItem.values());
|
|
1831
|
+
const mutations$ = rxjs.combineLatest(
|
|
1832
|
+
mutationRunners.map(
|
|
1833
|
+
(runner) => runner.mutationsSubject.pipe(
|
|
1834
|
+
rxjs.map((mutations) => mutations.map((mutation) => mutation))
|
|
1835
|
+
)
|
|
1678
1836
|
)
|
|
1679
1837
|
);
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1838
|
+
return mutations$.pipe(
|
|
1839
|
+
rxjs.map(
|
|
1840
|
+
(mutationsByKeys) => mutationsByKeys.reduce((acc, value) => [...acc, ...value], [])
|
|
1841
|
+
)
|
|
1842
|
+
);
|
|
1843
|
+
}),
|
|
1844
|
+
rxjs.startWith([]),
|
|
1845
|
+
rxjs.distinctUntilChanged(shallowEqual)
|
|
1846
|
+
).subscribe(this.mutationsSubject);
|
|
1688
1847
|
}
|
|
1689
1848
|
/**
|
|
1690
1849
|
* @helper
|
|
@@ -1708,72 +1867,65 @@ class MutationClient {
|
|
|
1708
1867
|
getMutationRunnersByKey(key) {
|
|
1709
1868
|
return this.mutationRunnersByKey$.getValue().get(key);
|
|
1710
1869
|
}
|
|
1711
|
-
useIsMutating(
|
|
1712
|
-
const
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
) : true;
|
|
1716
|
-
const finalPredicate = predicate ?? defaultPredicate;
|
|
1717
|
-
const reduceByNumber = (entries) => entries.reduce((acc, [mutationKey2, value]) => {
|
|
1718
|
-
return finalPredicate({ options: { mutationKey: mutationKey2 } }) ? value + acc : acc;
|
|
1870
|
+
useIsMutating(filters = {}) {
|
|
1871
|
+
const predicate = createPredicateForFilters(filters);
|
|
1872
|
+
const reduceByNumber = (entries) => entries.reduce((acc, mutation) => {
|
|
1873
|
+
return predicate(mutation) && mutation.state.status === "pending" ? 1 + acc : acc;
|
|
1719
1874
|
}, 0);
|
|
1720
|
-
const lastValue = reduceByNumber(this.
|
|
1721
|
-
const value$ = this.
|
|
1722
|
-
rxjs.
|
|
1875
|
+
const lastValue = reduceByNumber(this.mutationsSubject.getValue());
|
|
1876
|
+
const value$ = this.mutationsSubject.pipe(
|
|
1877
|
+
rxjs.switchMap((mutations) => {
|
|
1878
|
+
const mutationsOnStateUpdate = mutations.map(
|
|
1879
|
+
(mutation) => mutation.stateSubject.pipe(rxjs.map(() => mutation))
|
|
1880
|
+
);
|
|
1881
|
+
return mutationsOnStateUpdate.length === 0 ? rxjs.of([]) : rxjs.combineLatest(mutationsOnStateUpdate);
|
|
1882
|
+
}),
|
|
1883
|
+
rxjs.map(reduceByNumber),
|
|
1723
1884
|
rxjs.distinctUntilChanged()
|
|
1724
1885
|
);
|
|
1725
1886
|
return { value$, lastValue };
|
|
1726
1887
|
}
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
).pipe(
|
|
1749
|
-
rxjs.filter(isDefined),
|
|
1750
|
-
rxjs.map((value) => ({
|
|
1751
|
-
...value,
|
|
1752
|
-
isIdle: value.status === "idle",
|
|
1753
|
-
isPaused: false,
|
|
1754
|
-
isError: value.error !== void 0,
|
|
1755
|
-
isPending: false,
|
|
1756
|
-
isSuccess: value.status === "success"
|
|
1757
|
-
}))
|
|
1888
|
+
mutationState({
|
|
1889
|
+
filters,
|
|
1890
|
+
select
|
|
1891
|
+
} = {}) {
|
|
1892
|
+
const predicate = createPredicateForFilters(filters);
|
|
1893
|
+
const finalSelect = select ?? ((mutation) => mutation.state);
|
|
1894
|
+
const lastValue = this.mutationsSubject.getValue().reduce((acc, mutation) => {
|
|
1895
|
+
const result = [...acc, mutation];
|
|
1896
|
+
return result;
|
|
1897
|
+
}, []).filter(predicate).map((mutation) => finalSelect(mutation));
|
|
1898
|
+
const value$ = this.mutationsSubject.pipe(
|
|
1899
|
+
rxjs.switchMap((mutations) => {
|
|
1900
|
+
const mutationsOnStateUpdate = mutations.map(
|
|
1901
|
+
(mutation) => mutation.stateSubject.pipe(
|
|
1902
|
+
rxjs.filter(() => predicate(mutation)),
|
|
1903
|
+
rxjs.map(() => finalSelect(mutation))
|
|
1904
|
+
)
|
|
1905
|
+
);
|
|
1906
|
+
return mutationsOnStateUpdate.length === 0 ? rxjs.of([]) : rxjs.combineLatest(mutationsOnStateUpdate);
|
|
1907
|
+
}),
|
|
1908
|
+
rxjs.distinctUntilChanged(shallowEqual)
|
|
1758
1909
|
);
|
|
1759
|
-
return {
|
|
1910
|
+
return { value$, lastValue };
|
|
1760
1911
|
}
|
|
1761
1912
|
mutate(params) {
|
|
1762
1913
|
this.mutate$.next(params);
|
|
1763
1914
|
}
|
|
1764
1915
|
/**
|
|
1765
|
-
* This will
|
|
1916
|
+
* This will cancel any current mutation runnings.
|
|
1766
1917
|
* No discrimination process
|
|
1767
1918
|
*/
|
|
1768
|
-
|
|
1769
|
-
this.
|
|
1919
|
+
cancel(params) {
|
|
1920
|
+
this.cancel$.next(params);
|
|
1770
1921
|
}
|
|
1771
1922
|
destroy() {
|
|
1772
|
-
this.
|
|
1923
|
+
this.cancel$.complete();
|
|
1773
1924
|
this.mutate$.complete();
|
|
1774
|
-
this.
|
|
1925
|
+
this.mutationResultObserver.destroy();
|
|
1775
1926
|
this.mutationRunnersByKey$.complete();
|
|
1776
1927
|
this.isMutatingSubject.complete();
|
|
1928
|
+
this.mutationsSubject.complete();
|
|
1777
1929
|
}
|
|
1778
1930
|
}
|
|
1779
1931
|
const createClient = () => {
|