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.

72 lines
3.2 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>;
subscriptions: Record<string, SubscriptionCallbackFunction_T>;
isTransitioning: boolean;
isPaused: boolean;
}
/**
* Description placeholder
*
* @export
* @param {Machine_T} machine
* @param {InitialContextFunction_T} initialContextFunction - in the form of a function rather than a direct value, so as to facilitate co-initialization of peer interpreters. Otherwise, the "parent" interpreter will start, but without a reference to a running child interpreter which it might expect to exist.
* @param {?string} [initialStateName]
* @returns {Interpreter_T}
*/
export declare function Interpreter(machine: Machine_T, initialContext: any, initialStateName?: string): Interpreter_T;
export declare function start(interpreter: Interpreter_T): void;
export declare function pause(interpreter: Interpreter_T): void;
/** 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): string;
export declare function unsubscribe(interpreter: Interpreter_T, subscriptionId: string): void;
export declare const Spawn: () => void;
export declare const Unspawn: () => void;