{"version":3,"file":"useSelector.cjs","names":[],"sources":["../src/useSelector.ts"],"sourcesContent":["import { useCallback } from 'react'\nimport { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'\n\nexport interface UseSelectorOptions<TSelected> {\n  compare?: (a: TSelected, b: TSelected) => boolean\n}\n\ntype SyncExternalStoreSubscribe = Parameters<\n  typeof useSyncExternalStoreWithSelector\n>[0]\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 * Selects a slice of state from an atom or store and subscribes the component\n * to that selection.\n *\n * This is the primary React read hook for TanStack Store. It works with any\n * source that exposes `get()` and `subscribe()`, including atoms, readonly\n * atoms, stores, and readonly stores.\n *\n * Omit the selector to 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: SyncExternalStoreSubscribe = useCallback(\n    (handleStoreChange) => {\n      const { unsubscribe } = source.subscribe(handleStoreChange)\n      return unsubscribe\n    },\n    [source],\n  )\n\n  const getSnapshot = useCallback(() => source.get(), [source])\n\n  return useSyncExternalStoreWithSelector(\n    subscribe,\n    getSnapshot,\n    getSnapshot,\n    selector,\n    compare,\n  )\n}\n"],"mappings":";;;;AAkBA,SAAS,eAAkB,GAAM,GAAM;AACrC,QAAO,MAAM;;;;;;;;;;;;;;;;;;;;;;AAuBf,SAAgB,YACd,QACA,YAA8C,MAAM,GACpD,SACW;CACX,MAAM,UAAU,SAAS,WAAW;CAEpC,MAAM,oCACH,sBAAsB;EACrB,MAAM,EAAE,gBAAgB,OAAO,UAAU,kBAAkB;AAC3D,SAAO;IAET,CAAC,OAAO,CACT;CAED,MAAM,2CAAgC,OAAO,KAAK,EAAE,CAAC,OAAO,CAAC;AAE7D,yFACE,WACA,aACA,aACA,UACA,QACD"}