import { useRef } from "react"; import { createFlatStore, createFlatStoreOfSignals } from "../flat-store"; import { untracked } from "../utils"; import { useSignalEffectOnce } from "./utility"; export type AnyRecord = Record; /** * Create a flat store and its setter. */ export const useFlatStore = (storeCreator: () => T) => { const storeRef = useRef> | null>(); if (!storeRef.current) { storeRef.current = createFlatStore(untracked(storeCreator)); } return storeRef.current; }; export const useFlatStoreOfSignals = ( storeCreator: () => T ) => { const storeRef = useRef > | null>(); if (!storeRef.current) { storeRef.current = createFlatStoreOfSignals(untracked(storeCreator)); } return storeRef.current; }; export const useComputedFlatStore = ( storeUpdater: () => T ) => { const [store, setStore] = useFlatStore(storeUpdater); useSignalEffectOnce(() => { setStore(storeUpdater()); }); return store as Readonly; };