import type { Action } from "redux" import type { Reducer } from "../reducers" import type { ObservableStore } from "../store/ObservableStore" import type { State, StateConstructor, StateKey } from "../reducers" import type { Dispatch } from "redux" // Internal type definition for // function that gets the store state export type GetStoreState = () => S export type DispatchState = any> = Dispatch export type ActionFactoryDecorator = (factoryCtor:ActionFactoryConstructor) => T export type ActionFn = (...args: Args) => any export interface ActionRegistration = ActionFactory, Fn extends ActionFn = ActionFn> { type:string fullName:string leaf: string | F["leaf"] action:Fn options:ActionOptions> actionFactoryCtor: ActionFactoryConstructor } export type ActionOptions = ActionFactoryConstructor> = { isPromise?:boolean isReducer?:boolean factoryCtor?: A reducers?:Reducer[] mapped?:string[] } /** * Action factory provider */ export type ActionFnProvider = (...args:any[]) => (dispatch, getState) => T /** * Action reducer provider */ export type ActionReducerProvider = (...args:any[]) => (state:S) => S /** * Action provider */ export type ActionProvider = ActionFnProvider | ActionReducerProvider export interface ActionFactory< S extends State = any, M extends ActionMessage = ActionMessage, K extends StateKey = StateKey > { /** * Currently assigned store, fallback to global * if set */ readonly getStore: () => ObservableStore /** * Get the state constructor */ readonly stateType: StateConstructor /** * Get the leaf key */ readonly leaf: () => string | K /** * Get all currently registered actions */ readonly actions: ActionRegistration[] /** * Get action by name * * @param fullName - full name or type */ getAction(fullName: string) /** * Get action by parts * * @param leaf * @param type */ getAction(leaf: string, type: string) readonly dispatcher: Dispatch> readonly state: S /** * Register an action. Registrations * are used for store binding and * message generation, should not be * used directly * * @param reg */ readonly registerAction: (reg: ActionRegistration) => ActionRegistration readonly withStore: (newStore: ObservableStore) => this readonly setStore: (newStore: ObservableStore) => this readonly newMessage: ( id: string, leaf: string, type: string, reducers?: Reducer>[], args?: any[], data?: any ) => M } export interface ActionFactoryConstructor< Clazz extends ActionFactory, S extends Clazz["state"] = Clazz["state"] > { setStore: (store: ObservableStore) => void new (store?: ObservableStore): Clazz } export interface ActionMessage extends Action { id?: string leaf?: string | S["type"] type: string stateType: StateConstructor args?: any[] reducers?: Reducer>[] error?: Error } export enum ActionStatus { Started = 1, Finished = 2 } export interface PendingAction { id: string leaf: string name: string status: ActionStatus }