UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

106 lines (105 loc) 3.15 kB
import type { ObservableStore } from "../store"; import type { ActionMessage, ActionFactory, ActionRegistration } from "./ActionTypes"; import type { Reducer, State, StateConstructor, StateKey, RootState } from "../reducers"; import type { Dispatch } from "redux"; /** * Base class for action implementations for a given state * */ declare abstract class BaseActionFactory<S extends State = State, M extends ActionMessage<S> = ActionMessage<S>, K extends StateKey<S> = StateKey<S>, RS extends RootState & { [key in K]: S; } = RootState & { [key in K]: S; }> implements ActionFactory<S, M, K> { protected actionMap: Map<string, ActionRegistration<S, any>>; protected store: ObservableStore<RS>; readonly stateType: StateConstructor<S, K>; private pushStoreActions; /** * @inheritDoc */ get actions(): ActionRegistration<S, any, import("./ActionTypes").ActionFn<any>>[]; /** * @inheritDoc */ getAction(fullName: string): any; /** * @inheritDoc */ getAction(leaf: string, type: string): any; /** * @inheritDoc */ registerAction(reg: ActionRegistration): ActionRegistration; /** * Create a new action factory that consumes and produces a specific * state type * * @param stateType * @param withStore */ protected constructor(stateType: StateConstructor<S, K>, withStore?: ObservableStore); getStore(): ObservableStore<any>; /** * The leaf served by this implementation */ abstract leaf(): K; /** * Get the current dispatcher * * Implemented for the purpose of thunks * etc where the dispatcher can be augmented * * @returns {Function|(function(any): any)} */ get dispatcher(): Dispatch<ActionMessage<S>>; /** * Retrieve the current state using the global * getState or the augmented one * * directly applicable to @see dispatcher * * @returns instance of the state supported by this factory */ get state(): S; /** * withDispatcher creates a new instance of * this action implementation with a * new dispatcher and optionally a new * getState * * @returns {any} * @param newStore */ withStore(newStore: ObservableStore<any>): this; /** * Set the store for action factory * * @param newStore * @return {this<S, M>} */ setStore(newStore: ObservableStore<any>): this; /** * Create a new action message object that * fits the shape defined by the generic M * * @param id * @param type * @param reducers * @param data * @param args * @param leaf */ newMessage(id: string, leaf: string, type: string, reducers?: Reducer<S, ActionMessage<S>>[], args?: any[], data?: {}): M; /** * setError action applies to all states * * @param error */ setError(error: Error): void; } declare namespace BaseActionFactory { let clazzStore: ObservableStore<any>; function setStore(newClazzStore: ObservableStore<any>): void; } export { BaseActionFactory };