Open History to see the last commit
1import { injectSelector } from './injectSelector'
2import type { Signal } from '@angular/core'
3import type { Store, StoreActionMap } from '@tanstack/store'
4import type { InjectSelectorOptions } from './injectSelector'
5β
6/**
7 * Experimental combined read+write injection function for stores, mirroring
8 * injectAtom's pattern.
9 *
10 * Returns `[signal, actions]` when the store has an actions factory, or
11 * `[signal, setState]` for plain stores.
12 *
13 * @example
14 * ```ts
15 * // Store with actions
16 * readonly result = _injectStore(petStore, (s) => s.cats)
17 * // result[0] is Signal<number>, result[1] is actions
18 *
19 * // Store without actions
20 * readonly result = _injectStore(plainStore, (s) => s)
21 * // result[0] is Signal<number>, result[1] is setState
22 * ```
23 */
24export function _injectStore<
25 TState,
26 TActions extends StoreActionMap,
27 TSelected = NoInfer<TState>,
28>(
29 store: Store<TState, TActions>,
30 selector: (state: NoInfer<TState>) => TSelected,
31 options?: InjectSelectorOptions<TSelected>,
32): [
33 Signal<TSelected>,
34 [TActions] extends [never] ? Store<TState>['setState'] : TActions,
35] {
36 const selected = injectSelector(store, selector, options)
37 const actionsOrSetState =
38 (store.actions as StoreActionMap | undefined) ?? store.setState
39β
40 return [selected, actionsOrSetState] as any
41}