import { Observable } from 'rxjs'; import { Controller } from './controller'; import { Query, QueryOptions } from './query'; /** * 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 */ export type StateMutation = (state: State) => State; /** * A record of factories which create state mutations. */ export type StateUpdates = Readonly StateMutation>>; /** * Declare a record of factories for creating state mutations. */ export declare function declareStateUpdates(): = StateUpdates>(updates: Updates) => Updates; /** * Declare a record of factories for creating state mutations. */ export declare function declareStateUpdates = StateUpdates>(stateExample: State, updates: Updates): Updates; /** * Returns a mutation which applies all provided mutations for a state. * * You can use this helper to apply multiple changes at the same time. */ export declare function pipeStateMutations(mutations: ReadonlyArray>): StateMutation; /** * Read-only interface of a store. */ export type StoreQuery = Readonly & { /** * Returns a part of the state as `Observable` * The result observable produces distinct values by default. * * @example * ```ts * const state: StateReader<{form: {login: 'foo'}}> = // ... * const value$ = state.select((state) => state.form.login); * ``` */ select: (selector: (state: State) => R, options?: QueryOptions) => Observable; /** * Returns a part of the state as `Query`. * The result query produces distinct values by default. * * @example * ```ts * const state: StateReader<{form: {login: 'foo'}}> = // ... * const query = state.query((state) => state.form.login); * ``` * */ query: (selector: (state: State) => R, options?: QueryOptions) => Query; /** * Cast the store to a narrowed `Query` type. */ asQuery: () => Query; }>; /** * @internal * Updates the state by provided mutations * */ export type StoreUpdateFunction = (mutation: StateMutation | ReadonlyArray | undefined | null | false>) => void; /** Function which changes a state of the store */ export type StoreUpdate = (...args: Args) => void; /** Record of store update functions */ export type StoreUpdates> = Readonly<{ [K in keyof Updates]: StoreUpdate>; }>; /** * Store of a state */ export type Store = Controller & { id: number; name?: string; /** Sets a new state to the store */ set: (state: State) => void; /** Updates the store by provided mutations */ update: StoreUpdateFunction; }>; /** Store of a state with updating functions */ export type StoreWithUpdates> = Readonly & { updates: StoreUpdates; }>; export type StoreOptions = Readonly<{ name?: string; /** A comparator for detecting changes between old and new states */ comparator?: (prevState: State, nextState: State) => boolean; /** Callback is called when the store is destroyed */ onDestroy?: () => void; }>; /** @internal */ export type InternalStoreOptions = Readonly & { internal?: boolean; }>; /** * Creates the state store. * * @param initialState Initial state * @param options Parameters for the store */ export declare function createStore(initialState: State, options?: StoreOptions): Store; /** Creates StateUpdates for updating the store by provided state mutations */ export declare function createStoreUpdates>(storeUpdate: Store['update'], stateUpdates: Updates): StoreUpdates; /** Creates a proxy for the store with "updates" to change a state by provided mutations */ export declare function withStoreUpdates = StateUpdates>(store: Store, updates: Updates): StoreWithUpdates;