/** @module @category Store */ import { useCallback, useMemo, useSyncExternalStore } from 'react'; import type { StoreApi } from 'zustand'; export type ActionFunction = (state: T, ...args: any[]) => Partial; type ActionFunctionsRecord = Record>; export type Actions = ((store: StoreApi) => ActionFunctionsRecord) | ActionFunctionsRecord; export type BoundActions> = A extends ActionFunctionsRecord ? BindFunctionsIn : A extends (store: StoreApi) => ActionFunctionsRecord ? BindFunctionsIn> : never; // Given function type F, returns a new function type // with F's first input argument removed and its return type set to void type Bind any> = F extends (firstArg, ...restArgs: infer RestArgsType) => any ? (...args: RestArgsType) => void : never; // Given record type R, returns a new record type with Bind applied to every function in R type BindFunctionsIn any>> = { [Prop in keyof R]: Bind; }; function bindActions(store: StoreApi, actions: Actions): BoundActions> { if (typeof actions == 'function') { actions = actions(store); } const bound = {}; for (const [actionName, actionFunction] of Object.entries(actions)) { const boundFunction: Bind> = function (...args) { store.setState((state) => { return actionFunction(state, ...args); }); }; bound[actionName] = boundFunction; } return bound; } /** * A single shared identity function, rather than one built per call: `getSnapshot` below takes * the selector as a dependency, so a fresh default each render would leave it unstable for every * caller that doesn't pass a selector of its own. */ const identitySelect = (x: unknown) => x; const defaultSelectFunction = () => identitySelect as (x: T) => U; function useStore(store: StoreApi): T; function useStore(store: StoreApi, select: (state: T) => U): U; function useStore>( store: StoreApi, select: undefined, actions: A, ): T & BoundActions; function useStore>( store: StoreApi, select: (state: T) => U, actions: A, ): U & BoundActions; function useStore>( store: StoreApi, select: (state: T) => U = defaultSelectFunction(), actions?: A, ) { // Use useSyncExternalStore to subscribe synchronously during render // This ensures React can properly track all state updates const subscribe = useCallback( (callback: () => void) => { return store.subscribe(callback); }, [store], ); const getSnapshot = useCallback(() => select(store.getState()), [store, select]); const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); let boundActions: BoundActions> | null = useMemo( () => (actions ? bindActions(store, actions) : null), [store, actions], ); // Returned as-is when there are no actions to merge in, so the reference stays stable for as // long as the selected value does. That value is not a copy — it is whatever the selector // returned, usually the store's own state. return useMemo(() => (boundActions ? { ...state, ...boundActions } : state), [state, boundActions]); } /** * * @param store A zustand store * @param actions * @returns */ function useStoreWithActions>(store: StoreApi, actions: A): T & BoundActions { return useStore(store, defaultSelectFunction(), actions); } /** * Whenever possible, use `useStore(yourStore)` instead. This function is for creating a * custom hook for a specific store. */ function createUseStore(store: StoreApi) { function useStore(): T; function useStore>(actions: A): T & BoundActions; function useStore>(actions?: A): T & BoundActions; function useStore>(actions?: A) { const subscribe = useCallback((callback: () => void) => store.subscribe(callback), []); const getSnapshot = useCallback(() => store.getState(), []); const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); let boundActions: BoundActions> | null = useMemo( () => (actions ? bindActions(store, actions) : null), [actions], ); return useMemo(() => (boundActions ? { ...state, ...boundActions } : state), [state, boundActions]); } return useStore; } export { createUseStore, useStore, useStoreWithActions };