{"version":3,"file":"useSelector.cjs","names":[],"sources":["../src/useSelector.ts"],"sourcesContent":["import { createSignal, onCleanup } from 'solid-js'\nimport type { Accessor } from 'solid-js'\n\nexport interface UseSelectorOptions<TSelected> {\n  compare?: (a: TSelected, b: TSelected) => boolean\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 * Selects a slice of state from an atom or store and subscribes the component\n * to that selection.\n *\n * This is the primary Solid read hook for TanStack Store. It returns a Solid\n * accessor so consumers can read the selected value reactively.\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 * return <p>{count()}</p>\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): Accessor<TSelected> {\n  const compare = options?.compare ?? defaultCompare\n  const [signal, setSignal] = createSignal(selector(source.get()), {\n    equals: compare,\n  })\n\n  const unsubscribe = source.subscribe((snapshot) => {\n    setSignal(() => selector(snapshot))\n  }).unsubscribe\n\n  onCleanup(() => {\n    unsubscribe()\n  })\n\n  return signal\n}\n"],"mappings":";;;AAcA,SAAS,eAAkB,GAAM,GAAM;AACrC,QAAO,MAAM;;;;;;;;;;;;;;;;;;;;;;;AAwBf,SAAgB,YACd,QACA,YAA8C,MAAM,GACpD,SACqB;CACrB,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,CAAC,QAAQ,wCAA0B,SAAS,OAAO,KAAK,CAAC,EAAE,EAC/D,QAAQ,SACT,CAAC;CAEF,MAAM,cAAc,OAAO,WAAW,aAAa;AACjD,kBAAgB,SAAS,SAAS,CAAC;GACnC,CAAC;AAEH,+BAAgB;AACd,eAAa;GACb;AAEF,QAAO"}