( subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => any, )
| 14 | _instance: InternalStore |
| 15 | } |
| 16 | export 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 | |
| 50 | function didSnapshotChange(inst: { |
| 51 | _getSnapshot: () => any |
no test coverage detected