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