import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'preact/hooks' export interface UseSelectorOptions { compare?: (a: TSelected, b: TSelected) => boolean } type InternalStore = { _value: any _getSnapshot: () => any } type StoreRef = { _instance: InternalStore } type SelectionSource = { get: () => T subscribe: (listener: (value: T) => void) => { unsubscribe: () => void } } function defaultCompare(a: T, b: T) { return a === b } /** * This is taken from https://github.com/preactjs/preact/blob/main/compat/src/hooks.js#L8-L54 * which is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84 * on a high level this cuts out the warnings, ... and attempts a smaller implementation. * This way we don't have to import preact/compat with side effects */ function useSyncExternalStore( subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => any, ) { const value = getSnapshot() const [{ _instance }, forceUpdate] = useState({ _instance: { _value: value, _getSnapshot: getSnapshot }, }) useLayoutEffect(() => { _instance._value = value _instance._getSnapshot = getSnapshot if (didSnapshotChange(_instance)) { forceUpdate({ _instance }) } }, [_instance, subscribe, value, getSnapshot]) useEffect(() => { if (didSnapshotChange(_instance)) { forceUpdate({ _instance }) } return subscribe(() => { if (didSnapshotChange(_instance)) { forceUpdate({ _instance }) } }) }, [_instance, subscribe]) return value } function didSnapshotChange(inst: InternalStore): boolean { const latestGetSnapshot = inst._getSnapshot const prevValue = inst._value try { const nextValue = latestGetSnapshot() return !Object.is(prevValue, nextValue) // eslint-disable-next-line no-unused-vars } catch (_error) { return true } } function useSyncExternalStoreWithSelector( subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => TSnapshot, selector: (snapshot: TSnapshot) => TSelected, compare: (a: TSelected, b: TSelected) => boolean, ): TSelected { const selectedSnapshotRef = useRef(undefined) const getSelectedSnapshot = () => { const snapshot = getSnapshot() const selected = selector(snapshot) if ( selectedSnapshotRef.current === undefined || !compare(selectedSnapshotRef.current, selected) ) { selectedSnapshotRef.current = selected } return selectedSnapshotRef.current } return useSyncExternalStore(subscribe, getSelectedSnapshot) } /** * Selects a slice of state from an atom or store and subscribes the component * to that selection. * * This is the primary Preact read hook for TanStack Store. Use it when a * component only needs part of a source value, or omit the selector to * subscribe to the whole value. * * @example * ```tsx * const count = useSelector(counterStore, (state) => state.count) * ``` * * @example * ```tsx * const value = useSelector(countAtom) * ``` */ export function useSelector>( source: SelectionSource, selector: (snapshot: TSource) => TSelected = (s) => s as unknown as TSelected, options?: UseSelectorOptions, ): TSelected { const compare = options?.compare ?? defaultCompare const subscribe = useCallback( (handleStoreChange: () => void) => { const { unsubscribe } = source.subscribe(handleStoreChange) return unsubscribe }, [source], ) const getSnapshot = useCallback(() => source.get(), [source]) // Memoize the selector's output by the source snapshot so that selectors // returning a fresh reference each call (e.g. `(s) => [s.one, s.two]`) still // produce a stable reference for unchanged snapshots. Without this, the // shim's identity-based memo would always miss and trigger an update loop. // Scope the cache to the selector so a new selector can read the same // snapshot with updated props instead of reusing the previous selection. const memoizedSelector = useMemo(() => { let hasMemo = false let previousSnapshot: TSource let previousSelection: TSelected return (snapshot: TSource): TSelected => { if (hasMemo && Object.is(previousSnapshot, snapshot)) { return previousSelection } const next = selector(snapshot) hasMemo = true previousSnapshot = snapshot previousSelection = next return next } }, [selector]) return useSyncExternalStoreWithSelector( subscribe, getSnapshot, memoizedSelector, compare, ) }