import { Observable } from 'rxjs'; /** * Defines a state tree. */ export type IState = { [key: string]: any; }; /** * Defines an action that is dispatched to updated the state tree. */ export interface IAction { type: string; payload: { [key: string]: any }; } /** * Generic definition of a function that creates and fires an action. */ export type IDispatcher = ( store: IStore, payload: A['payload'], ) => void; /** * A function that changes a state-tree based on an action. */ export type Reducer = ( state: TState, action: TAction, options?: StoreContext, ) => TState | void; /** * An object passed to reducers and epics providing context. */ export type StoreContext = { context?: any; name?: string; // The optional display name of the store (passed into `createStore`). key?: string; // The key of the store if within a dictionary. }; /** * An event fired from an obervable when the state tree changes. */ export type StateChange = { type: TAction['type']; state: TState; action: TAction; payload: TAction['payload']; options: StoreContext; }; /** * An state change event that is passed to an "epic". */ export interface IActionEpic extends StateChange { dispatch: ( type: T['type'], payload?: T['payload'], ) => void; } /** * A store containing state and the necessary methods for * changing and responding to state-change actions. */ export interface IStore { readonly uid: string | number; readonly name?: string; readonly state$: Observable>; readonly state: S; readonly context?: any; dispatch: ( type: T['type'], payload?: T['payload'], ) => IStore; reducer: (fn: Reducer) => IStore; reduce: (action: T['type'], fn: Reducer) => IStore; on: ( action: T['type'] | Array, ) => Observable>; dict: (path: string, factory: StoreFactory) => IStore; add: (path: string, store: IStore) => IStore; remove: (path: string, options?: { key?: string }) => IStore; get: ( path?: string, options?: { key?: string; initialState?: S }, ) => IStore | undefined; } /** * Factory that creates a new store instance. */ export type StoreFactory = ( options?: StoreFactoryOptions, ) => IStore; export type StoreFactoryOptions = { initial?: S; path?: string; key?: string; [key: string]: any; // Other props. };