Files
smartstate/ts/smartstate.classes.stateaction.ts

29 lines
1022 B
TypeScript
Raw Permalink Normal View History

2022-03-25 13:31:21 +01:00
import { StatePart } from './smartstate.classes.statepart.js';
2020-11-29 23:51:05 +00:00
/**
* Context object passed to action definitions, enabling safe nested dispatch.
* Use `context.dispatch()` to dispatch sub-actions inline (bypasses the mutation queue).
* Direct `statePart.dispatchAction()` from within an action will deadlock.
*/
export interface IActionContext<TStateType> {
dispatch<T>(action: StateAction<TStateType, T>, payload: T): Promise<TStateType>;
}
2020-11-29 23:51:05 +00:00
export interface IActionDef<TStateType, TActionPayloadType> {
(stateArg: StatePart<any, TStateType>, actionPayload: TActionPayloadType, context?: IActionContext<TStateType>): Promise<TStateType>;
2020-11-29 23:51:05 +00:00
}
/**
* an actionmodifier for the state
*/
export class StateAction<TStateType, TActionPayloadType> {
constructor(
public statePartRef: StatePart<any, any>,
public actionDef: IActionDef<TStateType, TActionPayloadType>
) {}
public trigger(payload: TActionPayloadType): Promise<TStateType> {
return this.statePartRef.dispatchAction(this, payload);
2020-11-29 23:51:05 +00:00
}
}