You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.7 KiB
TypeScript

export type Event_T = [name: string, payload: any];
export interface Machine_T {
states: Array<State_T>;
}
export interface State_T {
name: string;
eventReactionCouplings: Array<EventReactionCouplings_T>;
}
export interface EventReactionCouplings_T {
eventName: string;
reactions: Array<Reaction_T>;
}
export type Reaction_T = SideEffect_T | ContextMutation_T | Goto_T;
export interface SideEffect_T {
type: 'SideEffect';
fn: SideEffectFunction_T;
}
export type SideEffectFunction_T = (ctx: any, e: Event_T, self: Interpreter_T) => void;
export interface ContextMutation_T {
type: 'ContextMutation';
fn: ContextMutationFunction_T;
}
export type ContextMutationFunction_T = (ctx: any, e: Event_T, self: Interpreter_T) => any;
export interface Goto_T {
type: 'Goto';
targetStateName: string;
}
export declare const Machine: (...states: Array<State_T>) => Machine_T;
export declare const State: (name: string, ...eventReactionCouplings: Array<EventReactionCouplings_T>) => State_T;
export declare const On: (eventName: string, ...reactions: Array<Reaction_T>) => EventReactionCouplings_T;
export declare const SideEffect: (fn: SideEffectFunction_T) => SideEffect_T;
export declare const Goto: (targetStateName: string) => Goto_T;
export declare const Context: (fn: ContextMutationFunction_T) => ContextMutation_T;
export interface Interpreter_T {
machine: Machine_T;
state: string;
context: any;
eventQueue: Array<Event_T>;
isTransitioning: boolean;
subscriptions: Record<string, SubscriptionCallbackFunction_T>;
}
export declare function interpret(machine: Machine_T, options: {
state?: string;
context: any;
}): Interpreter_T;
/** Inject an Event into the Interpreter's "tick queue".
*
* An event can be signify something "new" happening, such that its reactions should run on the next Tick;
* or it can signify a milestone "within" the current Tick, such that a Tick can be thought of as having
* "sub-Ticks".
*
* This distinction is significant for proper ordering of reaction execution, and also for determining
* whether to run a reaction at all. If an Event is received, and is specified to be applied on a past
* Tick, it is discarded.
*/
export declare function send(interpreter: Interpreter_T, event: Event_T): void;
export declare const enqueue: typeof send;
export type SubscriptionCallbackFunction_T = (self: Interpreter_T) => void;
export declare function subscribe(interpreter: Interpreter_T, callback: SubscriptionCallbackFunction_T): number;
export declare function unsubscribe(interpreter: Interpreter_T, subscriptionId: string): void;
export declare const Spawn: () => void;
export declare const Unspawn: () => void;