2022-03-25 13:31:21 +01:00
|
|
|
import { StatePart } from './smartstate.classes.statepart.js';
|
2020-11-29 23:51:05 +00:00
|
|
|
|
2026-03-02 19:11:44 +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> {
|
2026-03-02 19:11:44 +00:00
|
|
|
(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>
|
|
|
|
|
) {}
|
|
|
|
|
|
2025-07-19 07:18:53 +00:00
|
|
|
public trigger(payload: TActionPayloadType): Promise<TStateType> {
|
|
|
|
|
return this.statePartRef.dispatchAction(this, payload);
|
2020-11-29 23:51:05 +00:00
|
|
|
}
|
|
|
|
|
}
|