UNPKG

nrgy

Version:

The library for reactive programming using efficient computing and MVC/MVVM patterns

164 lines (152 loc) 5.05 kB
import { W as WritableAtom, A as AtomOptions } from './atom-DIp83xZa.js'; /** * 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<Array<string>> => * (state) => ([...state, name]); * ``` * * @param state a previous state * @returns a next state */ type StateMutation<State> = (state: State) => State; /** * A record of factories which create state mutations. */ type StateUpdates<State> = Readonly<Record<string, (...args: any[]) => StateMutation<State>>>; /** * Declare a record of factories for creating state mutations. */ declare function declareStateUpdates<State>(): <Updates extends StateUpdates<State> = StateUpdates<State>>(updates: Updates) => Updates; /** * Declare a record of factories for creating state mutations. */ declare function declareStateUpdates<State, Updates extends StateUpdates<State> = StateUpdates<State>>(stateExample: State, updates: Updates): Updates; /** * Function which changes a state of the store */ type StoreUpdate<Args extends unknown[]> = (...args: Args) => void; /** * Record of store update functions */ type StoreUpdates<State, Updates extends StateUpdates<State>> = Readonly<{ [K in keyof Updates]: StoreUpdate<Parameters<Updates[K]>>; }>; /** * Store and updating functions */ type Store<State, Updates extends StateUpdates<State>> = WritableAtom<State> & { readonly updates: StoreUpdates<State, Updates>; }; type StoreOptions<State, Updates extends StateUpdates<State> = StateUpdates<State>> = AtomOptions<State> & { updates: Updates; }; /** * Creates the state store. * * @param initialState Initial state * @param options Options for the store */ declare function createStore<State, Updates extends StateUpdates<State> = StateUpdates<State>>(initialState: State, options: StoreOptions<State, Updates>): Store<State, Updates>; /** * Creates StateUpdates for updating the store by provided state mutations */ declare function createStoreUpdates<State, Updates extends StateUpdates<State>>(atomUpdate: WritableAtom<State>['update'], stateUpdates: Updates): StoreUpdates<State, Updates>; /** * Options for declaring a store */ type DeclareStoreOptions<State, Updates extends StateUpdates<State> = StateUpdates<State>> = Readonly<{ initialState: State; updates: Updates; options?: AtomOptions<State>; }>; /** * @internal */ type FactoryStateArg<State> = (State extends (state: State) => State ? never : State) | StateMutation<State>; /** * Factory function for creating a store * * @param initialState Initial state * @param options Options for the store */ type StoreFactory<State, Updates extends StateUpdates<State>> = { (initialState?: FactoryStateArg<State>, options?: AtomOptions<State>): Store<State, Updates>; new (initialState?: FactoryStateArg<State>, options?: AtomOptions<State>): Store<State, Updates>; /** * 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<typeof createUserStore>; ``` */ declare function declareStore<State, Updates extends StateUpdates<State> = StateUpdates<State>>(storeOptions: DeclareStoreOptions<State, Updates>): StoreFactory<State, Updates>; 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 };