MCPcopy Create free account
hub / github.com/TanStack/query / useSyncExternalStore

Function useSyncExternalStore

packages/preact-query/src/utils.ts:16–48  ·  view source on GitHub ↗
(
  subscribe: (onStoreChange: () => void) => () => void,
  getSnapshot: () => any,
)

Source from the content-addressed store, hash-verified

14 _instance: InternalStore
15}
16export function useSyncExternalStore(
17 subscribe: (onStoreChange: () => void) => () => void,
18 getSnapshot: () => any,
19) {
20 const value = getSnapshot()
21
22 const [{ _instance }, forceUpdate] = useState<StoreRef>({
23 _instance: { _value: value, _getSnapshot: getSnapshot },
24 })
25
26 useLayoutEffect(() => {
27 _instance._value = value
28 _instance._getSnapshot = getSnapshot
29
30 if (didSnapshotChange(_instance)) {
31 forceUpdate({ _instance })
32 }
33 }, [subscribe, value, getSnapshot])
34
35 useEffect(() => {
36 if (didSnapshotChange(_instance)) {
37 forceUpdate({ _instance })
38 }
39
40 return subscribe(() => {
41 if (didSnapshotChange(_instance)) {
42 forceUpdate({ _instance })
43 }
44 })
45 }, [subscribe])
46
47 return value
48}
49
50function didSnapshotChange(inst: {
51 _getSnapshot: () => any

Callers 6

useBaseQueryFunction · 0.90
useMutationFunction · 0.90
useMutationStateFunction · 0.90
useQueriesFunction · 0.90
useIsFetchingFunction · 0.90

Calls 3

didSnapshotChangeFunction · 0.85
forceUpdateFunction · 0.85
subscribeFunction · 0.85

Tested by

no test coverage detected