Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

utils.ts

117 lines3.4 KB

1import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks'
2​
3/**
4 * This is taken from
5 * https://github.com/preactjs/preact/blob/main/compat/src/hooks.js#L8-L54 which
6 * is taken from
7 * https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84
8 * on a high level this cuts out the warnings, ... and attempts a smaller
9 * implementation. This way we don't have to import preact/compat with side
10 * effects
11 */
12type InternalStore = {
13 _value: any
14 _getSnapshot: () => any
16type StoreRef = {
17 _instance: InternalStore
19/**
20 * Subscribes to an external store and returns its current snapshot,
21 * re-rendering when it changes.
22 *
23 * @param subscribe - Subscribes to the store, and returns a function that
24 * unsubscribes.
25 * @param getSnapshot - Returns the current snapshot of the store.
26 * @returns The current snapshot.
27 */
28export function useSyncExternalStore(
29 subscribe: (onStoreChange: () => void) => () => void,
30 getSnapshot: () => any,
31) {
32 const value = getSnapshot()
33​
34 const [{ _instance }, forceUpdate] = useState<StoreRef>({
35 _instance: { _value: value, _getSnapshot: getSnapshot },
36 })
37​
38 useLayoutEffect(() => {
39 _instance._value = value
40 _instance._getSnapshot = getSnapshot
41​
42 if (didSnapshotChange(_instance)) {
43 forceUpdate({ _instance })
44 }
45 }, [subscribe, value, getSnapshot])
46​
47 useEffect(() => {
48 if (didSnapshotChange(_instance)) {
49 forceUpdate({ _instance })
50 }
51​
52 return subscribe(() => {
53 if (didSnapshotChange(_instance)) {
54 forceUpdate({ _instance })
55 }
56 })
57 }, [subscribe])
58​
59 return value
61​
62/**
63 * Checks whether the store's snapshot changed since it was last read.
64 *
65 * @param inst - The stored snapshot and the function that reads the current
66 * one.
67 * @returns `true` if the current snapshot differs, or if reading it throws.
68 */
69function didSnapshotChange(inst: {
70 _getSnapshot: () => any
71 _value: any
72}): boolean {
73 const latestGetSnapshot = inst._getSnapshot
74 const prevValue = inst._value
75 try {
76 const nextValue = latestGetSnapshot()
77 return !Object.is(prevValue, nextValue)
78 } catch (_error) {
79 return true
80 }
82​
83/**
84 * Like {@link useSyncExternalStore}, but returns a selected part of the
85 * snapshot, only updating it when `isEqual` reports a change.
86 *
87 * @param subscribe - Subscribes to the store, and returns a function that
88 * unsubscribes.
89 * @param getSnapshot - Returns the current snapshot of the store.
90 * @param selector - Selects the part of the snapshot to return.
91 * @param isEqual - Compares the previous and the new selection.
92 * @returns The selected part of the current snapshot.
93 */
94export function useSyncExternalStoreWithSelector<TSnapshot, TSelected>(
95 subscribe: (onStoreChange: () => void) => () => void,
96 getSnapshot: () => TSnapshot,
97 selector: (snapshot: TSnapshot) => TSelected,
98 isEqual: (a: TSelected, b: TSelected) => boolean,
99): TSelected {
100 const selectedSnapshotRef = useRef<TSelected | undefined>(undefined)
101​
102 const getSelectedSnapshot = () => {
103 const snapshot = getSnapshot()
104 const selected = selector(snapshot)
105​
106 if (
107 selectedSnapshotRef.current === undefined ||
108 !isEqual(selectedSnapshotRef.current, selected)
109 ) {
110 selectedSnapshotRef.current = selected
111 }
112​
113 return selectedSnapshotRef.current
114 }
115​
116 return useSyncExternalStore(subscribe, getSelectedSnapshot)