Open History to see the last commit
1import { shallow, useSelector } from '@tanstack/vue-store'
2import { defineComponent } from 'vue'
3import type {
4 Atom,
5 ReadonlyAtom,
6 ReadonlyStore,
7 Store,
8} from '@tanstack/vue-store'
9import type { VueComponentWithSlots } from './vueTypes.lib'
10β
11export type SubscribeSource<TValue> =
12 Atom<TValue> | ReadonlyAtom<TValue> | Store<TValue> | ReadonlyStore<TValue>
13β
14export interface SubscribeProps<in out TSourceData, in out TSelected> {
15 source: SubscribeSource<TSourceData>
16 selector: (state: TSourceData) => TSelected
17 when?: (selected: NoInfer<TSelected>) => boolean
18}
19β
20export type SubscribeComponent = new <TSourceData, const TSelected>(
21 props: SubscribeProps<TSourceData, TSelected>,
22) => InstanceType<
23 VueComponentWithSlots<
24 SubscribeProps<TSourceData, TSelected>,
25 { default: NoInfer<TSelected> }
26 >
27>
28β
29const SubscribeImpl = defineComponent<SubscribeProps<any, any>>(
30 (props, { slots }) => {
31 const selected = useSelector(props.source, props.selector, {
32 compare: shallow,
33 })
34β
35 return () => {
36 if (props.when?.(selected.value) === false) return null
37 return slots.default?.(selected.value)
38 }
39 },
40 {
41 name: 'TanStackForm.Subscribe',
42 props: ['source', 'selector', 'when'],
43 },
44)
45β
46export const Subscribe = SubscribeImpl as never as SubscribeComponent