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/main/compat/src/hooks.js#L8-L54
5 * which is taken from https://github.com/facebook/react/blob/main/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 }, [subscribe, value, getSnapshot])
40β
41 useEffect(() => {
42 if (didSnapshotChange(_instance)) {
43 forceUpdate({ _instance })
44 }
45β
46 return subscribe(() => {
47 if (didSnapshotChange(_instance)) {
48 forceUpdate({ _instance })
49 }
50 })
51 }, [subscribe])
52β
53 return value
54}
55β
56/**
57 * Checks whether the store's snapshot changed since it was last read.
58 * @param inst - The stored snapshot and the function that reads the current one.
59 * @returns `true` if the current snapshot differs, or if reading it throws.
60 */
61function didSnapshotChange(inst: {
62 _getSnapshot: () => any
63 _value: any
64}): boolean {
65 const latestGetSnapshot = inst._getSnapshot
66 const prevValue = inst._value
67 try {
68 const nextValue = latestGetSnapshot()
69 return !Object.is(prevValue, nextValue)
70 } catch (_error) {
71 return true
72 }
73}
74β
75/**
76 * Like {@link useSyncExternalStore}, but returns a selected part of the snapshot, only updating it
77 * when `isEqual` reports a change.
78 * @param subscribe - Subscribes to the store, and returns a function that unsubscribes.
79 * @param getSnapshot - Returns the current snapshot of the store.
80 * @param selector - Selects the part of the snapshot to return.
81 * @param isEqual - Compares the previous and the new selection.
82 * @returns The selected part of the current snapshot.
83 */
84export function useSyncExternalStoreWithSelector<TSnapshot, TSelected>(
85 subscribe: (onStoreChange: () => void) => () => void,
86 getSnapshot: () => TSnapshot,
87 selector: (snapshot: TSnapshot) => TSelected,
88 isEqual: (a: TSelected, b: TSelected) => boolean,
89): TSelected {
90 const selectedSnapshotRef = useRef<TSelected | undefined>(undefined)
91β
92 const getSelectedSnapshot = () => {
93 const snapshot = getSnapshot()
94 const selected = selector(snapshot)
95β
96 if (
97 selectedSnapshotRef.current === undefined ||
98 !isEqual(selectedSnapshotRef.current, selected)
99 ) {
100 selectedSnapshotRef.current = selected
101 }
102β
103 return selectedSnapshotRef.current
104 }
105β
106 return useSyncExternalStore(subscribe, getSelectedSnapshot)
107}