{"version":3,"file":"useSelector.cjs","names":[],"sources":["../src/useSelector.ts"],"sourcesContent":["import { onScopeDispose, readonly, shallowRef, toRaw } from 'vue-demi'\nimport type { Ref } from 'vue-demi'\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 Vue read hook for TanStack Store. It returns a readonly\n * ref containing the selected value.\n *\n * Omit the selector to subscribe to the whole value.\n *\n * @example\n * ```ts\n * const count = useSelector(counterStore, (state) => state.count)\n * console.log(count.value)\n * ```\n *\n * @example\n * ```ts\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): Readonly<Ref<TSelected>> {\n  const compare = options?.compare ?? defaultCompare\n  const slice = shallowRef(selector(source.get())) as Ref<TSelected>\n  const unsubscribe = source.subscribe((snapshot) => {\n    const selected = selector(snapshot)\n    if (compare(toRaw(slice.value), selected)) {\n      return\n    }\n    slice.value = selected\n  }).unsubscribe\n\n  onScopeDispose(() => {\n    unsubscribe()\n  })\n\n  return readonly(slice) as Readonly<Ref<TSelected>>\n}\n"],"mappings":";;;AAcA,SAAS,eAAkB,GAAM,GAAM;AACrC,QAAO,MAAM;;;;;;;;;;;;;;;;;;;;;;AAuBf,SAAgB,YACd,QACA,YAA8C,MAAM,GACpD,SAC0B;CAC1B,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,iCAAmB,SAAS,OAAO,KAAK,CAAC,CAAC;CAChD,MAAM,cAAc,OAAO,WAAW,aAAa;EACjD,MAAM,WAAW,SAAS,SAAS;AACnC,MAAI,4BAAc,MAAM,MAAM,EAAE,SAAS,CACvC;AAEF,QAAM,QAAQ;GACd,CAAC;AAEH,oCAAqB;AACnB,eAAa;GACb;AAEF,+BAAgB,MAAM"}