import { W as WritableAtom, A as AtomOptions } from './atom-C10tpvQt.cjs'; /** * A function to update a state. * * It is recommended to return a new state or the previous one. * * Actually, the function can change the state in place, but it is responsible * for a developer to provide `comparator` function to the store which handles * the changes. * * For making changes use a currying function to provide arguments: * ```ts * const addPizzaToCart = (name: string): StateMutation> => * (state) => ([...state, name]); * ``` * * @param state a previous state * @returns a next state */ type StateMutation = (state: State) => State; /** * A record of factories which create state mutations. */ type StateUpdates = Readonly StateMutation>>; /** * Declare a record of factories for creating state mutations. */ declare function declareStateUpdates(): = StateUpdates>(updates: Updates) => Updates; /** * Declare a record of factories for creating state mutations. */ declare function declareStateUpdates = StateUpdates>(stateExample: State, updates: Updates): Updates; /** * Function which changes a state of the store */ type StoreUpdate = (...args: Args) => void; /** * Record of store update functions */ type StoreUpdates> = Readonly<{ [K in keyof Updates]: StoreUpdate>; }>; /** * Store and updating functions */ type Store> = WritableAtom & { readonly updates: StoreUpdates; }; type StoreOptions = StateUpdates> = AtomOptions & { updates: Updates; }; /** * Creates the state store. * * @param initialState Initial state * @param options Options for the store */ declare function createStore = StateUpdates>(initialState: State, options: StoreOptions): Store; /** * Creates StateUpdates for updating the store by provided state mutations */ declare function createStoreUpdates>(atomUpdate: WritableAtom['update'], stateUpdates: Updates): StoreUpdates; /** * Options for declaring a store */ type DeclareStoreOptions = StateUpdates> = Readonly<{ initialState: State; updates: Updates; options?: AtomOptions; }>; /** * @internal */ type FactoryStateArg = (State extends (state: State) => State ? never : State) | StateMutation; /** * Factory function for creating a store * * @param initialState Initial state * @param options Options for the store */ type StoreFactory> = { (initialState?: FactoryStateArg, options?: AtomOptions): Store; new (initialState?: FactoryStateArg, options?: AtomOptions): Store; /** * Initial state */ readonly initialState: State; /** * State mutators */ readonly updates: Updates; }; /** * declare the base interface for create store * @example ```ts type State = { id: string; name: string; isAdmin: boolean }; const initialState: State = { id: '', name: '', isAdmin: false }; const createUserStore = declareStore({ initialState, updates: { setId: (id: string) => (state) => { return { ...state, id: id, }; }, setName: (name: string) => (state) => { return { ...state, name: name, }; }, update: (id: string name: string) => (state) => { return { ...state, id: id, name: name, }; }, setIsAdmin: () => (state) => { return { ...state, isAdmin: true, }; }, }, }); const userStore1 = createUserStore({ id: '1', name: 'User 1', isAdmin: false }); const userStore2 = createUserStore({ id: '2', name: 'User 2', isAdmin: true }); // OR const users = [ createUserStore({id: 1, name: 'User 1'}), createUserStore({id: 2, name: 'User 2'}), ] userStore1.updates.setName('User from store 1'); assets.isEqual(userStore1.get().name, 'User from store 1') assets.isEqual(userStore2.get().name, 'User 2') // type of createUserStore type UserStore = ReturnType; ``` */ declare function declareStore = StateUpdates>(storeOptions: DeclareStoreOptions): StoreFactory; export { type DeclareStoreOptions as D, type Store as S, type StoreUpdate as a, type StoreUpdates as b, type StateUpdates as c, type StateMutation as d, createStore as e, createStoreUpdates as f, declareStateUpdates as g, type StoreFactory as h, declareStore as i };