UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

123 lines (122 loc) 4.53 kB
import { RootReducerErrorHandler } from "../reducers/RootReducer"; import { Action as ReduxAction, AnyAction, Observable, Reducer, Store, StoreEnhancer, Unsubscribe } from "redux"; import "symbol-observable"; import type { ILeafReducer, RootState, State, StateArgs } from "../reducers"; import type { ActionFactory, ActionFactoryConstructor, ActionMessage } from "../actions"; import { ActionContainer } from "../actions"; import { TStateChangeHandler } from "./StateObserver"; import { DefaultLeafReducer } from "../reducers/DefaultLeafReducer"; import { InternalState } from "../internal/InternalState"; import DumbReducer from "../reducers/DumbReducer"; import { SelectorChainType, SelectorFn } from "../selectors"; export interface ObserverDisposer { (): void; } export declare function processLeafReducersAndStates(leafReducersOrStates: Array<ILeafReducer<any, any> | State | Function>): (ILeafReducer<any, any> | DumbReducer<State<string>>)[]; /** * Manage the redux store for RADS */ export declare class ObservableStore<S extends RootState = any> implements Store<S> { rootStateType: new () => S; defaultStateValue: any; /** * Factory method for creating a new observable store * * @param enhancer * @returns {ObservableStore<S>} * @param rootStateType * @param defaultStateValue * @param leafReducersOrStates */ static createObservableStore<S extends RootState>(leafReducersOrStates: Array<ILeafReducer<any, any> | State | Function>, enhancer?: StoreEnhancer<any>, rootStateType?: new () => S, defaultStateValue?: any): ObservableStore<S>; /** * Create simple reducers * * @param {string | State} statesOrKeys * @returns {Array<State>} */ static makeSimpleReducers<Args extends StateArgs[], S extends State = State>(...statesOrKeys: Args): Array<ILeafReducer<S>>; /** * Create a internal reducer * * @returns {DefaultLeafReducer<InternalState, ActionMessage<InternalState>>} */ static createInternalReducer(): DefaultLeafReducer<string, State<string>, typeof InternalState, ActionMessage<State<string>>>; private rootReducer; private observers; private rootReducerFn; private readonly store; private readonly actionFactories; readonly actionContainer: ActionContainer; constructor(leafReducersOrStates: Array<ILeafReducer<any, any> | State | Function>, enhancer?: StoreEnhancer<ObservableStore<S>, unknown>, rootStateType?: new () => S, defaultStateValue?: any); setOnError(onError: RootReducerErrorHandler): this; /** * Get a prepared set of actions * * @param keyOrCtor - class name of class constructor to search for */ getActions<A extends ActionFactory>(keyOrCtor: ActionFactoryConstructor<A> | string): A; /** * Create a new root reducer * * @param leafReducers * @returns {any} */ private createRootReducer; /** * Retrieve the redux store under everything * * @returns {any} */ getReduxStore(): Store<S, AnyAction>; /** * Update the reducers */ replaceReducers(...leafReducers: ILeafReducer<any, any>[]): void; subscribe(listener: () => void): Unsubscribe; replaceReducer(nextReducer: Reducer<S>): void; /** * Retrieve the current state * @returns {*} */ getState(): S; getInternalState(): InternalState; /** * Dispatch typed message * * @param action * @returns {A|undefined|IAction} */ dispatch<A extends ReduxAction>(action: A): A; /** * Schedule notifications to go out on next tick */ scheduleNotification(): void; /** * */ onChange(): void; /** * Create a new selector from the store's state */ selector<Chain extends SelectorChainType<S, S> = SelectorChainType<S, S>>(): Chain; /** * Observe state path for changes * * @param selector * @param handler * @returns {function()} unsubscribe observer */ observe<T>(selector: SelectorFn<S, T>, handler: TStateChangeHandler<S, T>): ObserverDisposer; /** * Observe state path for changes * * @param path * @param handler * @returns {function()} unsubscribe observer */ observe<T = any>(path: string | string[], handler: TStateChangeHandler<S, T>): ObserverDisposer; getValueAtPath<T>(state: S, keyPath: Array<string | number>): T; private observable; [Symbol.observable]: () => Observable<S>; }