@mittwald/react-ghostmaker 1.4.2 → 1.4.3
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.
|
@@ -5,8 +5,10 @@ import { CustomerDetailed, CustomerGhost, Project, ProjectDetailed, ProjectGhost
|
|
|
5
5
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
6
6
|
import { invalidateGhosts } from "./invalidate";
|
|
7
7
|
import { makeGhost } from "./makeGhost";
|
|
8
|
+
import { cleanupTargetHashes } from "./useGhostChain";
|
|
8
9
|
beforeEach(() => {
|
|
9
10
|
vitest.useFakeTimers();
|
|
11
|
+
cleanupTargetHashes();
|
|
10
12
|
});
|
|
11
13
|
afterEach(() => {
|
|
12
14
|
vitest.runOnlyPendingTimers();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import is, { assert } from "@sindresorhus/is";
|
|
2
|
-
import { useEffect, useEffectEvent, useMemo
|
|
2
|
+
import { useEffect, useEffectEvent, useMemo } from "react";
|
|
3
3
|
import { transformFnProp, } from "./types";
|
|
4
4
|
import { useQueryClient, useSuspenseQuery } from "@tanstack/react-query";
|
|
5
5
|
import { queries } from "./queries";
|
|
@@ -56,19 +56,17 @@ const useGhostChainItem = (target, item, options = {}, context) => {
|
|
|
56
56
|
}
|
|
57
57
|
return query.data.result;
|
|
58
58
|
};
|
|
59
|
+
const targetHashes = new Map();
|
|
59
60
|
const useTargetAutoInvalidate = (target, queryKey) => {
|
|
60
61
|
const queryClient = useQueryClient();
|
|
61
62
|
const invalidate = () => queryClient.invalidateQueries({
|
|
62
63
|
queryKey,
|
|
63
64
|
});
|
|
64
65
|
const targetHash = hashObject(target);
|
|
65
|
-
const prevTargetHash = useRef(targetHash);
|
|
66
66
|
const joinedQueryKey = queryKey.join(".");
|
|
67
|
-
const
|
|
68
|
-
const needsRefresh =
|
|
69
|
-
|
|
70
|
-
prevTargetHash.current = targetHash;
|
|
71
|
-
prevQueryId.current = joinedQueryKey;
|
|
67
|
+
const prevTargetHash = targetHashes.get(joinedQueryKey);
|
|
68
|
+
const needsRefresh = prevTargetHash !== undefined && prevTargetHash !== targetHash;
|
|
69
|
+
targetHashes.set(joinedQueryKey, targetHash);
|
|
72
70
|
const onTargetChangeEvent = useEffectEvent(() => {
|
|
73
71
|
if (needsRefresh) {
|
|
74
72
|
invalidate();
|
|
@@ -78,3 +76,7 @@ const useTargetAutoInvalidate = (target, queryKey) => {
|
|
|
78
76
|
onTargetChangeEvent();
|
|
79
77
|
}, [needsRefresh]);
|
|
80
78
|
};
|
|
79
|
+
/** @internal */
|
|
80
|
+
export const cleanupTargetHashes = () => {
|
|
81
|
+
targetHashes.clear();
|
|
82
|
+
};
|