reactjrx 1.54.0 → 1.56.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 +379 -193
- package/dist/index.d.ts +1 -1
- package/dist/index.js +380 -194
- package/dist/lib/binding/useObserve.d.ts +1 -0
- package/dist/lib/queries/client/createClient.d.ts +3 -0
- package/dist/lib/queries/client/mutations/MutationClient.d.ts +57 -0
- package/dist/lib/queries/client/mutations/createMutationRunner.d.ts +13 -0
- package/dist/lib/queries/client/mutations/operators.d.ts +3 -0
- package/dist/lib/queries/client/mutations/types.d.ts +65 -0
- package/dist/lib/queries/react/Provider.d.ts +3 -1
- package/dist/lib/queries/react/keys/nanoid.d.ts +1 -0
- package/dist/lib/queries/{client → react}/keys/serializeKey.d.ts +1 -1
- package/dist/lib/queries/react/mutations/useMutation.d.ts +15 -79
- package/dist/lib/queries/react/mutations/useMutation.keys.test.d.ts +1 -0
- package/package.json +7 -4
package/dist/index.cjs
CHANGED
|
@@ -35,12 +35,17 @@ function primitiveEqual(objA, objB) {
|
|
|
35
35
|
return false;
|
|
36
36
|
}
|
|
37
37
|
function useObserve(source$, unsafeOptions, unsafeDeps) {
|
|
38
|
-
const options = unsafeOptions != null && !Array.isArray(unsafeOptions) ? unsafeOptions : {
|
|
38
|
+
const options = unsafeOptions != null && !Array.isArray(unsafeOptions) ? unsafeOptions : {
|
|
39
|
+
defaultValue: void 0,
|
|
40
|
+
key: "",
|
|
41
|
+
unsubscribeOnUnmount: true
|
|
42
|
+
};
|
|
39
43
|
const deps = unsafeDeps == null && Array.isArray(unsafeOptions) ? unsafeOptions : typeof source$ === "function" ? unsafeDeps ?? [] : [source$];
|
|
40
44
|
const valueRef = react.useRef(
|
|
41
45
|
"getValue" in source$ && typeof source$.getValue === "function" ? source$.getValue() : options.defaultValue
|
|
42
46
|
);
|
|
43
47
|
const sourceRef = useLiveRef(source$);
|
|
48
|
+
const optionsRef = useLiveRef(options);
|
|
44
49
|
const subscribe = react.useCallback(
|
|
45
50
|
(next) => {
|
|
46
51
|
const source = sourceRef.current;
|
|
@@ -64,6 +69,8 @@ function useObserve(source$, unsafeOptions, unsafeDeps) {
|
|
|
64
69
|
})
|
|
65
70
|
).subscribe(next);
|
|
66
71
|
return () => {
|
|
72
|
+
if (optionsRef.current.unsubscribeOnUnmount === false)
|
|
73
|
+
return;
|
|
67
74
|
sub.unsubscribe();
|
|
68
75
|
};
|
|
69
76
|
},
|
|
@@ -391,175 +398,6 @@ function retryBackoff(config) {
|
|
|
391
398
|
);
|
|
392
399
|
});
|
|
393
400
|
}
|
|
394
|
-
function shallowEqual(objA, objB) {
|
|
395
|
-
if (objA === null || objA === void 0 || objB === void 0) {
|
|
396
|
-
return objA === objB;
|
|
397
|
-
}
|
|
398
|
-
if (typeof objA !== "object" || typeof objB !== "object") {
|
|
399
|
-
return objA === objB;
|
|
400
|
-
}
|
|
401
|
-
if (objA.constructor !== (objB == null ? void 0 : objB.constructor)) {
|
|
402
|
-
return false;
|
|
403
|
-
}
|
|
404
|
-
const keysA = Object.keys(objA);
|
|
405
|
-
const keysB = Object.keys(objB);
|
|
406
|
-
if (keysA.length !== keysB.length) {
|
|
407
|
-
return false;
|
|
408
|
-
}
|
|
409
|
-
for (const key of keysA) {
|
|
410
|
-
if (!objB.hasOwnProperty(key) || objA[key] !== objB[key]) {
|
|
411
|
-
return false;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
return true;
|
|
415
|
-
}
|
|
416
|
-
const retryOnError = (options) => retryBackoff({
|
|
417
|
-
initialInterval: 100,
|
|
418
|
-
...typeof options.retry === "function" ? {
|
|
419
|
-
shouldRetry: options.retry
|
|
420
|
-
} : {
|
|
421
|
-
maxRetries: options.retry === false ? 0 : options.retry ?? 3
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
const mergeResults = (stream$) => stream$.pipe(
|
|
425
|
-
rxjs.scan(
|
|
426
|
-
(acc, current) => {
|
|
427
|
-
return {
|
|
428
|
-
...acc,
|
|
429
|
-
...current
|
|
430
|
-
};
|
|
431
|
-
},
|
|
432
|
-
{
|
|
433
|
-
data: void 0,
|
|
434
|
-
error: void 0,
|
|
435
|
-
fetchStatus: "idle",
|
|
436
|
-
status: "loading"
|
|
437
|
-
}
|
|
438
|
-
),
|
|
439
|
-
rxjs.distinctUntilChanged(
|
|
440
|
-
({ data: prevData, ...prev }, { data: currData, ...curr }) => shallowEqual(prev, curr) && shallowEqual(prevData, currData)
|
|
441
|
-
)
|
|
442
|
-
);
|
|
443
|
-
function useAsyncQuery(query, mapOperatorOrOptions, options = {}) {
|
|
444
|
-
const queryRef = useLiveRef(query);
|
|
445
|
-
const triggerSubject = useSubject();
|
|
446
|
-
const resetSubject = useSubject({
|
|
447
|
-
/**
|
|
448
|
-
* @important
|
|
449
|
-
* Because async query can still run after unmount, the user might
|
|
450
|
-
* want to use reset for whatever reason. We will only manually complete
|
|
451
|
-
* this subject whenever the main query hook finalize.
|
|
452
|
-
*/
|
|
453
|
-
completeOnUnmount: false
|
|
454
|
-
});
|
|
455
|
-
const optionsRef = useLiveRef(
|
|
456
|
-
typeof mapOperatorOrOptions === "object" ? mapOperatorOrOptions : options
|
|
457
|
-
);
|
|
458
|
-
const data$ = useBehaviorSubject({
|
|
459
|
-
data: void 0,
|
|
460
|
-
error: void 0,
|
|
461
|
-
status: "idle"
|
|
462
|
-
});
|
|
463
|
-
const mapOperator = typeof mapOperatorOrOptions === "string" ? mapOperatorOrOptions : "merge";
|
|
464
|
-
react.useEffect(() => {
|
|
465
|
-
const switchOperator = mapOperator === "concat" ? rxjs.concatMap : mapOperator === "switch" ? rxjs.switchMap : rxjs.mergeMap;
|
|
466
|
-
const subscription = rxjs.merge(
|
|
467
|
-
resetSubject.current.pipe(
|
|
468
|
-
rxjs.map(
|
|
469
|
-
() => ({
|
|
470
|
-
status: "idle",
|
|
471
|
-
data: void 0,
|
|
472
|
-
error: void 0
|
|
473
|
-
})
|
|
474
|
-
)
|
|
475
|
-
),
|
|
476
|
-
triggerSubject.current.pipe(
|
|
477
|
-
switchOperator((args) => {
|
|
478
|
-
const isLastMutationCalled = triggerSubject.current.pipe(
|
|
479
|
-
rxjs.take(1),
|
|
480
|
-
rxjs.map(() => mapOperator === "concat"),
|
|
481
|
-
rxjs.startWith(true)
|
|
482
|
-
);
|
|
483
|
-
return rxjs.merge(
|
|
484
|
-
rxjs.of({
|
|
485
|
-
status: "loading"
|
|
486
|
-
}),
|
|
487
|
-
rxjs.combineLatest([
|
|
488
|
-
rxjs.defer(() => rxjs.from(queryRef.current(args))).pipe(
|
|
489
|
-
retryOnError(optionsRef.current),
|
|
490
|
-
rxjs.first(),
|
|
491
|
-
rxjs.map((data) => ({ data, isError: false })),
|
|
492
|
-
rxjs.catchError((error) => {
|
|
493
|
-
console.error(error);
|
|
494
|
-
if (optionsRef.current.onError != null) {
|
|
495
|
-
optionsRef.current.onError(error, args);
|
|
496
|
-
}
|
|
497
|
-
return rxjs.of({ data: error, isError: true });
|
|
498
|
-
})
|
|
499
|
-
),
|
|
500
|
-
isLastMutationCalled
|
|
501
|
-
]).pipe(
|
|
502
|
-
rxjs.map(([{ data, isError }, isLastMutationCalled2]) => {
|
|
503
|
-
if (!isError) {
|
|
504
|
-
if (optionsRef.current.onSuccess != null)
|
|
505
|
-
optionsRef.current.onSuccess(data, args);
|
|
506
|
-
}
|
|
507
|
-
if (isLastMutationCalled2) {
|
|
508
|
-
return isError ? {
|
|
509
|
-
status: "error",
|
|
510
|
-
error: data,
|
|
511
|
-
data: void 0
|
|
512
|
-
} : {
|
|
513
|
-
status: "success",
|
|
514
|
-
error: void 0,
|
|
515
|
-
data
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
return void 0;
|
|
519
|
-
}),
|
|
520
|
-
rxjs.takeUntil(resetSubject.current)
|
|
521
|
-
)
|
|
522
|
-
);
|
|
523
|
-
}),
|
|
524
|
-
optionsRef.current.triggerHook ?? rxjs.identity,
|
|
525
|
-
rxjs.finalize(() => {
|
|
526
|
-
resetSubject.current.complete();
|
|
527
|
-
})
|
|
528
|
-
)
|
|
529
|
-
).pipe(
|
|
530
|
-
rxjs.filter((state) => !!state && !!Object.keys(state).length),
|
|
531
|
-
/**
|
|
532
|
-
* @important
|
|
533
|
-
* state update optimization
|
|
534
|
-
*/
|
|
535
|
-
rxjs.distinctUntilChanged(shallowEqual)
|
|
536
|
-
).subscribe((state) => {
|
|
537
|
-
data$.current.next({
|
|
538
|
-
...data$.current.getValue(),
|
|
539
|
-
...state
|
|
540
|
-
});
|
|
541
|
-
});
|
|
542
|
-
return () => {
|
|
543
|
-
if (optionsRef.current.cancelOnUnMount) {
|
|
544
|
-
subscription.unsubscribe();
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
|
-
}, [mapOperator]);
|
|
548
|
-
const result = useObserve(
|
|
549
|
-
() => data$.current,
|
|
550
|
-
{
|
|
551
|
-
defaultValue: data$.current.getValue()
|
|
552
|
-
},
|
|
553
|
-
[]
|
|
554
|
-
);
|
|
555
|
-
const mutate = react.useCallback((arg) => {
|
|
556
|
-
triggerSubject.current.next(arg);
|
|
557
|
-
}, []);
|
|
558
|
-
const reset = react.useCallback(() => {
|
|
559
|
-
resetSubject.current.next();
|
|
560
|
-
}, []);
|
|
561
|
-
return { ...result, isLoading: result.status === "loading", mutate, reset };
|
|
562
|
-
}
|
|
563
401
|
const Context = react.createContext({
|
|
564
402
|
client: null
|
|
565
403
|
});
|
|
@@ -574,7 +412,7 @@ const ClientEffect = ({
|
|
|
574
412
|
}, [client]);
|
|
575
413
|
return null;
|
|
576
414
|
};
|
|
577
|
-
const
|
|
415
|
+
const QueryClientProvider = react.memo(
|
|
578
416
|
({ children, client }) => {
|
|
579
417
|
const value = react.useMemo(() => ({ client: client.client }), [client]);
|
|
580
418
|
return /* @__PURE__ */ jsxRuntime.jsxs(Context.Provider, { value, children: [
|
|
@@ -590,7 +428,94 @@ const useQueryClient = () => {
|
|
|
590
428
|
}
|
|
591
429
|
return context.client;
|
|
592
430
|
};
|
|
431
|
+
const serializeObject = (object) => {
|
|
432
|
+
if (Array.isArray(object)) {
|
|
433
|
+
return object.reduce((acc, value, index) => {
|
|
434
|
+
if (index === object.length - 1)
|
|
435
|
+
return `${acc}${serializeObject(value)}]`;
|
|
436
|
+
return `${acc}${serializeObject(value)},`;
|
|
437
|
+
}, "[");
|
|
438
|
+
}
|
|
439
|
+
if (object === void 0)
|
|
440
|
+
return "";
|
|
441
|
+
return JSON.stringify(object, Object.keys(object).sort());
|
|
442
|
+
};
|
|
443
|
+
const serializeKey = (key) => {
|
|
444
|
+
if (key.length === 0)
|
|
445
|
+
return "[]";
|
|
446
|
+
return serializeObject(key);
|
|
447
|
+
};
|
|
448
|
+
const nanoid = (size = 21) => crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
449
|
+
byte &= 63;
|
|
450
|
+
if (byte < 36) {
|
|
451
|
+
id += byte.toString(36);
|
|
452
|
+
} else if (byte < 62) {
|
|
453
|
+
id += (byte - 26).toString(36).toUpperCase();
|
|
454
|
+
} else if (byte > 62) {
|
|
455
|
+
id += "-";
|
|
456
|
+
} else {
|
|
457
|
+
id += "_";
|
|
458
|
+
}
|
|
459
|
+
return id;
|
|
460
|
+
}, "");
|
|
461
|
+
function useMutation(options) {
|
|
462
|
+
const client = useQueryClient();
|
|
463
|
+
const optionsRef = useLiveRef(options);
|
|
464
|
+
const defaultKey = useConstant(() => [nanoid()]);
|
|
465
|
+
const key = serializeKey(options.mutationKey ?? defaultKey.current);
|
|
466
|
+
const observedMutation = react.useMemo(
|
|
467
|
+
() => client.mutationClient.observe({ key }),
|
|
468
|
+
[key]
|
|
469
|
+
);
|
|
470
|
+
const result = useObserve(observedMutation.result$) ?? observedMutation.lastValue;
|
|
471
|
+
const mutate = react.useCallback(
|
|
472
|
+
(mutationArgs) => {
|
|
473
|
+
client.mutationClient.mutate({
|
|
474
|
+
options: { ...optionsRef.current, mutationKey: key },
|
|
475
|
+
args: mutationArgs
|
|
476
|
+
});
|
|
477
|
+
},
|
|
478
|
+
[client, key]
|
|
479
|
+
);
|
|
480
|
+
const reset = react.useCallback(() => {
|
|
481
|
+
client.mutationClient.reset({
|
|
482
|
+
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
483
|
+
});
|
|
484
|
+
}, [client]);
|
|
485
|
+
react.useEffect(() => {
|
|
486
|
+
return () => {
|
|
487
|
+
if (optionsRef.current.cancelOnUnMount) {
|
|
488
|
+
client.mutationClient.reset({
|
|
489
|
+
key: optionsRef.current.mutationKey ?? defaultKey.current
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
}, []);
|
|
494
|
+
return { mutate, reset, ...result };
|
|
495
|
+
}
|
|
593
496
|
const arrayEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
497
|
+
function shallowEqual(objA, objB) {
|
|
498
|
+
if (objA === null || objA === void 0 || objB === void 0) {
|
|
499
|
+
return objA === objB;
|
|
500
|
+
}
|
|
501
|
+
if (typeof objA !== "object" || typeof objB !== "object") {
|
|
502
|
+
return objA === objB;
|
|
503
|
+
}
|
|
504
|
+
if (objA.constructor !== (objB == null ? void 0 : objB.constructor)) {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
const keysA = Object.keys(objA);
|
|
508
|
+
const keysB = Object.keys(objB);
|
|
509
|
+
if (keysA.length !== keysB.length) {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
for (const key of keysA) {
|
|
513
|
+
if (!objB.hasOwnProperty(key) || objA[key] !== objB[key]) {
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
594
519
|
function isDefined(arg) {
|
|
595
520
|
return arg !== null && arg !== void 0;
|
|
596
521
|
}
|
|
@@ -758,23 +683,33 @@ function useSubscribeEffect(source, unsafeOptions, deps = []) {
|
|
|
758
683
|
);
|
|
759
684
|
useSubscribe(enhancerMakeObservable, deps);
|
|
760
685
|
}
|
|
761
|
-
const
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
}, "[");
|
|
686
|
+
const retryOnError = (options) => retryBackoff({
|
|
687
|
+
initialInterval: 100,
|
|
688
|
+
...typeof options.retry === "function" ? {
|
|
689
|
+
shouldRetry: options.retry
|
|
690
|
+
} : {
|
|
691
|
+
maxRetries: options.retry === false ? 0 : options.retry ?? 3
|
|
768
692
|
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
}
|
|
693
|
+
});
|
|
694
|
+
const mergeResults$1 = (stream$) => stream$.pipe(
|
|
695
|
+
rxjs.scan(
|
|
696
|
+
(acc, current) => {
|
|
697
|
+
return {
|
|
698
|
+
...acc,
|
|
699
|
+
...current
|
|
700
|
+
};
|
|
701
|
+
},
|
|
702
|
+
{
|
|
703
|
+
data: void 0,
|
|
704
|
+
error: void 0,
|
|
705
|
+
fetchStatus: "idle",
|
|
706
|
+
status: "loading"
|
|
707
|
+
}
|
|
708
|
+
),
|
|
709
|
+
rxjs.distinctUntilChanged(
|
|
710
|
+
({ data: prevData, ...prev }, { data: currData, ...curr }) => shallowEqual(prev, curr) && shallowEqual(prevData, currData)
|
|
711
|
+
)
|
|
712
|
+
);
|
|
778
713
|
const resetStyle = { backgroundColor: "transparent", color: "inherit" };
|
|
779
714
|
function createLogger(env) {
|
|
780
715
|
const _logger = {
|
|
@@ -1529,11 +1464,257 @@ const dispatchExternalRefetchToAllQueries = ({
|
|
|
1529
1464
|
}),
|
|
1530
1465
|
rxjs.filter((trigger2) => trigger2.type !== "refetch")
|
|
1531
1466
|
);
|
|
1467
|
+
const mergeResults = (stream$) => stream$.pipe(
|
|
1468
|
+
rxjs.scan(
|
|
1469
|
+
(acc, current) => {
|
|
1470
|
+
return {
|
|
1471
|
+
...acc,
|
|
1472
|
+
...current
|
|
1473
|
+
};
|
|
1474
|
+
},
|
|
1475
|
+
{
|
|
1476
|
+
data: void 0,
|
|
1477
|
+
error: void 0,
|
|
1478
|
+
// fetchStatus: "idle",
|
|
1479
|
+
status: "loading"
|
|
1480
|
+
}
|
|
1481
|
+
),
|
|
1482
|
+
rxjs.distinctUntilChanged(
|
|
1483
|
+
({ data: prevData, ...prev }, { data: currData, ...curr }) => shallowEqual(prev, curr) && shallowEqual(prevData, currData)
|
|
1484
|
+
)
|
|
1485
|
+
);
|
|
1486
|
+
const createMutationRunner = ({
|
|
1487
|
+
__queryFinalizeHook,
|
|
1488
|
+
__queryInitHook,
|
|
1489
|
+
__queryTriggerHook
|
|
1490
|
+
}) => {
|
|
1491
|
+
const trigger$ = new rxjs.Subject();
|
|
1492
|
+
const reset$ = new rxjs.Subject();
|
|
1493
|
+
let closed = false;
|
|
1494
|
+
const mapOperator$ = new rxjs.BehaviorSubject("merge");
|
|
1495
|
+
const mutationsRunning$ = new rxjs.BehaviorSubject(0);
|
|
1496
|
+
const destroy = () => {
|
|
1497
|
+
if (closed) {
|
|
1498
|
+
throw new Error("Trying to close an already closed mutation");
|
|
1499
|
+
}
|
|
1500
|
+
closed = true;
|
|
1501
|
+
mapOperator$.complete();
|
|
1502
|
+
mutationsRunning$.complete();
|
|
1503
|
+
trigger$.complete();
|
|
1504
|
+
reset$.complete();
|
|
1505
|
+
};
|
|
1506
|
+
const stableMapOperator$ = mapOperator$.pipe(
|
|
1507
|
+
rxjs.filter(isDefined),
|
|
1508
|
+
rxjs.distinctUntilChanged()
|
|
1509
|
+
);
|
|
1510
|
+
const mutation$ = stableMapOperator$.pipe(
|
|
1511
|
+
__queryInitHook ?? rxjs.identity,
|
|
1512
|
+
rxjs.mergeMap((mapOperator) => {
|
|
1513
|
+
const switchOperator = mapOperator === "concat" ? rxjs.concatMap : mapOperator === "switch" ? rxjs.switchMap : rxjs.mergeMap;
|
|
1514
|
+
return trigger$.pipe(
|
|
1515
|
+
rxjs.takeUntil(stableMapOperator$.pipe(rxjs.skip(1))),
|
|
1516
|
+
rxjs.tap(() => {
|
|
1517
|
+
mutationsRunning$.next(mutationsRunning$.getValue() + 1);
|
|
1518
|
+
}),
|
|
1519
|
+
switchOperator(({ args, options }) => {
|
|
1520
|
+
const queryRunner$ = rxjs.defer(() => rxjs.from(options.mutationFn(args))).pipe(
|
|
1521
|
+
retryOnError(options),
|
|
1522
|
+
rxjs.take(1),
|
|
1523
|
+
rxjs.map((data) => ({ data, isError: false })),
|
|
1524
|
+
rxjs.catchError((error) => {
|
|
1525
|
+
console.error(error);
|
|
1526
|
+
if (options.onError != null) {
|
|
1527
|
+
options.onError(error, args);
|
|
1528
|
+
}
|
|
1529
|
+
return rxjs.of({ data: error, isError: true });
|
|
1530
|
+
}),
|
|
1531
|
+
rxjs.share()
|
|
1532
|
+
);
|
|
1533
|
+
const queryIsOver$ = queryRunner$.pipe(
|
|
1534
|
+
rxjs.map(({ data, isError }) => isError || data)
|
|
1535
|
+
);
|
|
1536
|
+
const isThisCurrentFunctionLastOneCalled = trigger$.pipe(
|
|
1537
|
+
rxjs.take(1),
|
|
1538
|
+
rxjs.map(() => mapOperator === "concat"),
|
|
1539
|
+
rxjs.startWith(true),
|
|
1540
|
+
rxjs.takeUntil(queryIsOver$)
|
|
1541
|
+
);
|
|
1542
|
+
const loading$ = rxjs.of({
|
|
1543
|
+
status: "loading"
|
|
1544
|
+
});
|
|
1545
|
+
return rxjs.merge(
|
|
1546
|
+
loading$,
|
|
1547
|
+
rxjs.combineLatest([
|
|
1548
|
+
queryRunner$,
|
|
1549
|
+
isThisCurrentFunctionLastOneCalled
|
|
1550
|
+
]).pipe(
|
|
1551
|
+
rxjs.map(([{ data, isError }, isLastMutationCalled]) => {
|
|
1552
|
+
if (!isError) {
|
|
1553
|
+
if (options.onSuccess != null)
|
|
1554
|
+
options.onSuccess(data, args);
|
|
1555
|
+
}
|
|
1556
|
+
if (isLastMutationCalled) {
|
|
1557
|
+
return isError ? {
|
|
1558
|
+
status: "error",
|
|
1559
|
+
error: data,
|
|
1560
|
+
data: void 0
|
|
1561
|
+
} : {
|
|
1562
|
+
status: "success",
|
|
1563
|
+
error: void 0,
|
|
1564
|
+
data
|
|
1565
|
+
};
|
|
1566
|
+
}
|
|
1567
|
+
return {};
|
|
1568
|
+
}),
|
|
1569
|
+
rxjs.takeUntil(reset$)
|
|
1570
|
+
)
|
|
1571
|
+
).pipe(
|
|
1572
|
+
options.__queryRunnerHook ?? rxjs.identity,
|
|
1573
|
+
rxjs.finalize(() => {
|
|
1574
|
+
mutationsRunning$.next(mutationsRunning$.getValue() - 1);
|
|
1575
|
+
})
|
|
1576
|
+
);
|
|
1577
|
+
}),
|
|
1578
|
+
__queryTriggerHook ?? rxjs.identity,
|
|
1579
|
+
mergeResults
|
|
1580
|
+
);
|
|
1581
|
+
}),
|
|
1582
|
+
__queryFinalizeHook ?? rxjs.identity
|
|
1583
|
+
);
|
|
1584
|
+
return {
|
|
1585
|
+
mutation$,
|
|
1586
|
+
trigger: ({
|
|
1587
|
+
args,
|
|
1588
|
+
options
|
|
1589
|
+
}) => {
|
|
1590
|
+
mapOperator$.next(options.mapOperator);
|
|
1591
|
+
trigger$.next({ args, options });
|
|
1592
|
+
},
|
|
1593
|
+
reset$,
|
|
1594
|
+
destroy,
|
|
1595
|
+
mutationsRunning$,
|
|
1596
|
+
getClosed: () => closed
|
|
1597
|
+
};
|
|
1598
|
+
};
|
|
1599
|
+
class MutationClient {
|
|
1600
|
+
constructor() {
|
|
1601
|
+
/**
|
|
1602
|
+
* Contain all active mutation for a given key.
|
|
1603
|
+
* A mutation ca have several triggers running (it is not necessarily one function running)
|
|
1604
|
+
*
|
|
1605
|
+
* @important
|
|
1606
|
+
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1607
|
+
*/
|
|
1608
|
+
__publicField(this, "mutationRunners$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1609
|
+
/**
|
|
1610
|
+
* Mutation result subject. It can be used whether there is a mutation
|
|
1611
|
+
* running or not and can be directly observed.
|
|
1612
|
+
*
|
|
1613
|
+
* @important
|
|
1614
|
+
* - automatically cleaned as soon as the last mutation is done for a given key
|
|
1615
|
+
*/
|
|
1616
|
+
__publicField(this, "mutationResults$", new rxjs.BehaviorSubject(/* @__PURE__ */ new Map()));
|
|
1617
|
+
__publicField(this, "mutate$", new rxjs.Subject());
|
|
1618
|
+
__publicField(this, "reset$", new rxjs.Subject());
|
|
1619
|
+
this.mutate$.pipe(
|
|
1620
|
+
rxjs.tap(({ options, args }) => {
|
|
1621
|
+
const { mutationKey } = options;
|
|
1622
|
+
let mutationForKey = this.mutationRunners$.getValue().get(mutationKey);
|
|
1623
|
+
if (!mutationForKey) {
|
|
1624
|
+
mutationForKey = createMutationRunner(options);
|
|
1625
|
+
this.mutationRunners$.getValue().set(mutationKey, mutationForKey);
|
|
1626
|
+
mutationForKey.mutation$.subscribe((result) => {
|
|
1627
|
+
let resultForKeySubject = this.mutationResults$.getValue().get(mutationKey);
|
|
1628
|
+
if (!resultForKeySubject) {
|
|
1629
|
+
resultForKeySubject = new rxjs.BehaviorSubject(result);
|
|
1630
|
+
const resultMap = this.mutationResults$.getValue();
|
|
1631
|
+
resultMap.set(mutationKey, resultForKeySubject);
|
|
1632
|
+
this.mutationResults$.next(resultMap);
|
|
1633
|
+
} else {
|
|
1634
|
+
resultForKeySubject == null ? void 0 : resultForKeySubject.next(result);
|
|
1635
|
+
}
|
|
1636
|
+
});
|
|
1637
|
+
mutationForKey.mutationsRunning$.pipe(
|
|
1638
|
+
rxjs.skip(1),
|
|
1639
|
+
rxjs.filter((number) => number === 0)
|
|
1640
|
+
).subscribe(() => {
|
|
1641
|
+
mutationForKey == null ? void 0 : mutationForKey.destroy();
|
|
1642
|
+
const resultMap = this.mutationResults$.getValue();
|
|
1643
|
+
resultMap.delete(mutationKey);
|
|
1644
|
+
this.mutationResults$.next(resultMap);
|
|
1645
|
+
const mutationsMap = this.mutationRunners$.getValue();
|
|
1646
|
+
mutationsMap.delete(mutationKey);
|
|
1647
|
+
this.mutationRunners$.next(mutationsMap);
|
|
1648
|
+
});
|
|
1649
|
+
}
|
|
1650
|
+
mutationForKey.trigger({ args, options });
|
|
1651
|
+
})
|
|
1652
|
+
).subscribe();
|
|
1653
|
+
this.reset$.pipe(
|
|
1654
|
+
rxjs.tap(({ key }) => {
|
|
1655
|
+
var _a;
|
|
1656
|
+
const serializedKey = serializeKey(key);
|
|
1657
|
+
(_a = this.mutationRunners$.getValue().get(serializedKey)) == null ? void 0 : _a.reset$.next();
|
|
1658
|
+
})
|
|
1659
|
+
).subscribe();
|
|
1660
|
+
}
|
|
1661
|
+
observe({ key }) {
|
|
1662
|
+
var _a;
|
|
1663
|
+
const currentResultValue = (_a = this.mutationResults$.getValue().get(key)) == null ? void 0 : _a.getValue();
|
|
1664
|
+
const mapResultToObservedResult = (value) => ({
|
|
1665
|
+
...value,
|
|
1666
|
+
isIdle: value.status === "idle",
|
|
1667
|
+
isPaused: false,
|
|
1668
|
+
isError: value.error !== void 0,
|
|
1669
|
+
isPending: false,
|
|
1670
|
+
isSuccess: value.status === "success"
|
|
1671
|
+
});
|
|
1672
|
+
const lastValue = currentResultValue ? mapResultToObservedResult(currentResultValue) : mapResultToObservedResult({
|
|
1673
|
+
data: void 0,
|
|
1674
|
+
error: void 0,
|
|
1675
|
+
status: "idle"
|
|
1676
|
+
});
|
|
1677
|
+
const result$ = this.mutationResults$.pipe(
|
|
1678
|
+
rxjs.switchMap((resultMap) => {
|
|
1679
|
+
const subject = resultMap.get(key);
|
|
1680
|
+
return subject ?? rxjs.EMPTY;
|
|
1681
|
+
})
|
|
1682
|
+
).pipe(
|
|
1683
|
+
rxjs.filter(isDefined),
|
|
1684
|
+
rxjs.map((value) => ({
|
|
1685
|
+
...value,
|
|
1686
|
+
isIdle: value.status === "idle",
|
|
1687
|
+
isPaused: false,
|
|
1688
|
+
isError: value.error !== void 0,
|
|
1689
|
+
isPending: false,
|
|
1690
|
+
isSuccess: value.status === "success"
|
|
1691
|
+
}))
|
|
1692
|
+
);
|
|
1693
|
+
return { result$, lastValue };
|
|
1694
|
+
}
|
|
1695
|
+
mutate(params) {
|
|
1696
|
+
this.mutate$.next(params);
|
|
1697
|
+
}
|
|
1698
|
+
/**
|
|
1699
|
+
* This will reset any current mutation runnings.
|
|
1700
|
+
* No discrimination process
|
|
1701
|
+
*/
|
|
1702
|
+
reset(params) {
|
|
1703
|
+
this.reset$.next(params);
|
|
1704
|
+
}
|
|
1705
|
+
destroy() {
|
|
1706
|
+
this.reset$.complete();
|
|
1707
|
+
this.mutate$.complete();
|
|
1708
|
+
this.mutationResults$.complete();
|
|
1709
|
+
this.mutationRunners$.complete();
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1532
1712
|
const createClient = () => {
|
|
1533
1713
|
const queryStore = createQueryStore();
|
|
1534
1714
|
const invalidationClient = createInvalidationClient({ queryStore });
|
|
1535
1715
|
const cacheClient = createCacheClient({ queryStore });
|
|
1536
1716
|
const refetchClient = createRefetchClient();
|
|
1717
|
+
const mutationClient = new MutationClient();
|
|
1537
1718
|
let hasCalledStart = false;
|
|
1538
1719
|
const query = ({
|
|
1539
1720
|
key,
|
|
@@ -1610,7 +1791,7 @@ const createClient = () => {
|
|
|
1610
1791
|
trigger$
|
|
1611
1792
|
})
|
|
1612
1793
|
),
|
|
1613
|
-
mergeResults,
|
|
1794
|
+
mergeResults$1,
|
|
1614
1795
|
rxjs.withLatestFrom(options$),
|
|
1615
1796
|
rxjs.takeWhile(([result, options]) => {
|
|
1616
1797
|
const shouldStop = result.data !== void 0 && options.terminateOnFirstResult;
|
|
@@ -1649,19 +1830,24 @@ const createClient = () => {
|
|
|
1649
1830
|
const queryListenerSub = queryListener$.subscribe();
|
|
1650
1831
|
const started = [queryStore.start()];
|
|
1651
1832
|
return () => {
|
|
1652
|
-
started.forEach((
|
|
1653
|
-
|
|
1833
|
+
started.forEach((destroy2) => {
|
|
1834
|
+
destroy2();
|
|
1654
1835
|
});
|
|
1655
1836
|
queryListenerSub.unsubscribe();
|
|
1656
1837
|
};
|
|
1657
1838
|
};
|
|
1839
|
+
const destroy = () => {
|
|
1840
|
+
mutationClient.destroy();
|
|
1841
|
+
};
|
|
1658
1842
|
return {
|
|
1659
1843
|
start,
|
|
1660
1844
|
query,
|
|
1661
1845
|
queryStore,
|
|
1846
|
+
mutationClient,
|
|
1662
1847
|
...invalidationClient,
|
|
1663
1848
|
...cacheClient,
|
|
1664
|
-
...refetchClient
|
|
1849
|
+
...refetchClient,
|
|
1850
|
+
destroy
|
|
1665
1851
|
};
|
|
1666
1852
|
};
|
|
1667
1853
|
class QueryClient {
|
|
@@ -1671,7 +1857,7 @@ class QueryClient {
|
|
|
1671
1857
|
}
|
|
1672
1858
|
}
|
|
1673
1859
|
exports.QueryClient = QueryClient;
|
|
1674
|
-
exports.QueryClientProvider =
|
|
1860
|
+
exports.QueryClientProvider = QueryClientProvider;
|
|
1675
1861
|
exports.SIGNAL_RESET = SIGNAL_RESET;
|
|
1676
1862
|
exports.createClient = createClient;
|
|
1677
1863
|
exports.createLocalforageAdapter = createLocalforageAdapter;
|
|
@@ -1681,9 +1867,9 @@ exports.getDelay = getDelay;
|
|
|
1681
1867
|
exports.retryBackoff = retryBackoff;
|
|
1682
1868
|
exports.signal = signal;
|
|
1683
1869
|
exports.trigger = trigger;
|
|
1684
|
-
exports.useAsyncQuery = useAsyncQuery;
|
|
1685
1870
|
exports.useBehaviorSubject = useBehaviorSubject;
|
|
1686
1871
|
exports.useLiveRef = useLiveRef;
|
|
1872
|
+
exports.useMutation = useMutation;
|
|
1687
1873
|
exports.useObserve = useObserve;
|
|
1688
1874
|
exports.useObserveCallback = useObserveCallback;
|
|
1689
1875
|
exports.usePersistSignals = usePersistSignals;
|
package/dist/index.d.ts
CHANGED
|
@@ -17,4 +17,4 @@ export * from "./lib/queries/react/mutations/useMutation";
|
|
|
17
17
|
export * from "./lib/queries/react/queries/useQuery";
|
|
18
18
|
export * from "./lib/queries/react/useSubscribeEffect";
|
|
19
19
|
export * from "./lib/queries/client/createClient";
|
|
20
|
-
export {
|
|
20
|
+
export { QueryClientProvider, useQueryClient } from "./lib/queries/react/Provider";
|