/** * Store factory helpers. */ import { createStore } from './create-store'; import { getStore, hasStore } from './registry'; import type { Store, StoreDefinition } from './types'; /** * Creates a store factory that returns the store instance. * * The store is lazily created on first call and cached in the global store * registry. Subsequent calls return the same instance. After calling * `destroyStore(id)`, the next factory call will create a fresh store. * * @param id - Store identifier * @param definition - Store definition without id * @returns A function that returns the store instance * * @example * ```ts * const useCounter = defineStore('counter', { * state: () => ({ count: 0 }), * actions: { increment() { this.count++; } }, * }); * * const counter = useCounter(); * counter.increment(); * ``` */ export const defineStore = < S extends Record, G extends Record = Record, // eslint-disable-next-line @typescript-eslint/no-explicit-any -- actions may declare specific parameter types A extends Record any> = Record, >( id: string, definition: Omit, 'id'> ): (() => Store) => { // Check registry first to avoid noisy warnings from createStore() // when the factory is called multiple times (intended usage pattern). // createStore() only called when store doesn't exist or was destroyed. return () => { if (hasStore(id)) { return getStore(id) as Store; } return createStore({ id, ...definition }); }; };