{"version":3,"file":"useSelector.cjs","names":[],"sources":["../src/useSelector.ts"],"sourcesContent":["import {\n  useCallback,\n  useEffect,\n  useLayoutEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'preact/hooks'\n\nexport interface UseSelectorOptions<TSelected> {\n  compare?: (a: TSelected, b: TSelected) => boolean\n}\n\ntype InternalStore = {\n  _value: any\n  _getSnapshot: () => any\n}\n\ntype StoreRef = {\n  _instance: InternalStore\n}\n\ntype SelectionSource<T> = {\n  get: () => T\n  subscribe: (listener: (value: T) => void) => {\n    unsubscribe: () => void\n  }\n}\n\nfunction defaultCompare<T>(a: T, b: T) {\n  return a === b\n}\n\n/**\n * This is taken from https://github.com/preactjs/preact/blob/main/compat/src/hooks.js#L8-L54\n * which is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84\n * on a high level this cuts out the warnings, ... and attempts a smaller implementation.\n * This way we don't have to import preact/compat with side effects\n */\nfunction useSyncExternalStore(\n  subscribe: (onStoreChange: () => void) => () => void,\n  getSnapshot: () => any,\n) {\n  const value = getSnapshot()\n\n  const [{ _instance }, forceUpdate] = useState<StoreRef>({\n    _instance: { _value: value, _getSnapshot: getSnapshot },\n  })\n\n  useLayoutEffect(() => {\n    _instance._value = value\n    _instance._getSnapshot = getSnapshot\n\n    if (didSnapshotChange(_instance)) {\n      forceUpdate({ _instance })\n    }\n  }, [_instance, subscribe, value, getSnapshot])\n\n  useEffect(() => {\n    if (didSnapshotChange(_instance)) {\n      forceUpdate({ _instance })\n    }\n\n    return subscribe(() => {\n      if (didSnapshotChange(_instance)) {\n        forceUpdate({ _instance })\n      }\n    })\n  }, [_instance, subscribe])\n\n  return value\n}\n\nfunction didSnapshotChange(inst: InternalStore): boolean {\n  const latestGetSnapshot = inst._getSnapshot\n  const prevValue = inst._value\n  try {\n    const nextValue = latestGetSnapshot()\n    return !Object.is(prevValue, nextValue)\n    // eslint-disable-next-line no-unused-vars\n  } catch (_error) {\n    return true\n  }\n}\n\nfunction useSyncExternalStoreWithSelector<TSnapshot, TSelected>(\n  subscribe: (onStoreChange: () => void) => () => void,\n  getSnapshot: () => TSnapshot,\n  selector: (snapshot: TSnapshot) => TSelected,\n  compare: (a: TSelected, b: TSelected) => boolean,\n): TSelected {\n  const selectedSnapshotRef = useRef<TSelected | undefined>(undefined)\n\n  const getSelectedSnapshot = () => {\n    const snapshot = getSnapshot()\n    const selected = selector(snapshot)\n\n    if (\n      selectedSnapshotRef.current === undefined ||\n      !compare(selectedSnapshotRef.current, selected)\n    ) {\n      selectedSnapshotRef.current = selected\n    }\n\n    return selectedSnapshotRef.current\n  }\n\n  return useSyncExternalStore(subscribe, getSelectedSnapshot)\n}\n\n/**\n * Selects a slice of state from an atom or store and subscribes the component\n * to that selection.\n *\n * This is the primary Preact read hook for TanStack Store. Use it when a\n * component only needs part of a source value, or omit the selector to\n * subscribe to the whole value.\n *\n * @example\n * ```tsx\n * const count = useSelector(counterStore, (state) => state.count)\n * ```\n *\n * @example\n * ```tsx\n * const value = useSelector(countAtom)\n * ```\n */\nexport function useSelector<TSource, TSelected = NoInfer<TSource>>(\n  source: SelectionSource<TSource>,\n  selector: (snapshot: TSource) => TSelected = (s) => s as unknown as TSelected,\n  options?: UseSelectorOptions<TSelected>,\n): TSelected {\n  const compare = options?.compare ?? defaultCompare\n\n  const subscribe = useCallback(\n    (handleStoreChange: () => void) => {\n      const { unsubscribe } = source.subscribe(handleStoreChange)\n      return unsubscribe\n    },\n    [source],\n  )\n\n  const getSnapshot = useCallback(() => source.get(), [source])\n\n  // Memoize the selector's output by the source snapshot so that selectors\n  // returning a fresh reference each call (e.g. `(s) => [s.one, s.two]`) still\n  // produce a stable reference for unchanged snapshots. Without this, the\n  // shim's identity-based memo would always miss and trigger an update loop.\n  // Scope the cache to the selector so a new selector can read the same\n  // snapshot with updated props instead of reusing the previous selection.\n  const memoizedSelector = useMemo(() => {\n    let hasMemo = false\n    let previousSnapshot: TSource\n    let previousSelection: TSelected\n\n    return (snapshot: TSource): TSelected => {\n      if (hasMemo && Object.is(previousSnapshot, snapshot)) {\n        return previousSelection\n      }\n      const next = selector(snapshot)\n      hasMemo = true\n      previousSnapshot = snapshot\n      previousSelection = next\n      return next\n    }\n  }, [selector])\n\n  return useSyncExternalStoreWithSelector(\n    subscribe,\n    getSnapshot,\n    memoizedSelector,\n    compare,\n  )\n}\n"],"mappings":";;;AA6BA,SAAS,eAAkB,GAAM,GAAM;AACrC,QAAO,MAAM;;;;;;;;AASf,SAAS,qBACP,WACA,aACA;CACA,MAAM,QAAQ,aAAa;CAE3B,MAAM,CAAC,EAAE,aAAa,0CAAkC,EACtD,WAAW;EAAE,QAAQ;EAAO,cAAc;EAAa,EACxD,CAAC;AAEF,yCAAsB;AACpB,YAAU,SAAS;AACnB,YAAU,eAAe;AAEzB,MAAI,kBAAkB,UAAU,CAC9B,aAAY,EAAE,WAAW,CAAC;IAE3B;EAAC;EAAW;EAAW;EAAO;EAAY,CAAC;AAE9C,mCAAgB;AACd,MAAI,kBAAkB,UAAU,CAC9B,aAAY,EAAE,WAAW,CAAC;AAG5B,SAAO,gBAAgB;AACrB,OAAI,kBAAkB,UAAU,CAC9B,aAAY,EAAE,WAAW,CAAC;IAE5B;IACD,CAAC,WAAW,UAAU,CAAC;AAE1B,QAAO;;AAGT,SAAS,kBAAkB,MAA8B;CACvD,MAAM,oBAAoB,KAAK;CAC/B,MAAM,YAAY,KAAK;AACvB,KAAI;EACF,MAAM,YAAY,mBAAmB;AACrC,SAAO,CAAC,OAAO,GAAG,WAAW,UAAU;UAEhC,QAAQ;AACf,SAAO;;;AAIX,SAAS,iCACP,WACA,aACA,UACA,SACW;CACX,MAAM,+CAAoD,OAAU;CAEpE,MAAM,4BAA4B;EAEhC,MAAM,WAAW,SADA,aAAa,CACK;AAEnC,MACE,oBAAoB,YAAY,UAChC,CAAC,QAAQ,oBAAoB,SAAS,SAAS,CAE/C,qBAAoB,UAAU;AAGhC,SAAO,oBAAoB;;AAG7B,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