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.
74 lines
3.2 KiB
TypeScript
74 lines
3.2 KiB
TypeScript
export type Event_T = [name: string, payload: any];
|
|
export interface Machine_T<C> {
|
|
states: Array<State_T<C>>;
|
|
}
|
|
export interface State_T<C> {
|
|
name: string;
|
|
eventReactionCouplings: Array<EventReactionCouplings_T<C>>;
|
|
}
|
|
export interface EventReactionCouplings_T<C> {
|
|
eventName: string;
|
|
reactions: Array<Reaction_T<C>>;
|
|
}
|
|
export type Reaction_T<C> = SideEffect_T<C> | ContextMutation_T<C> | Goto_T;
|
|
export interface SideEffect_T<C> {
|
|
type: 'SideEffect';
|
|
fn: SideEffectFunction_T<C>;
|
|
}
|
|
export type SideEffectFunction_T<C> = (ctx: C, e: Event_T, self: Interpreter_T<C>, originalContext: C) => void;
|
|
export interface ContextMutation_T<C> {
|
|
type: 'ContextMutation';
|
|
fn: ContextMutationFunction_T<C>;
|
|
}
|
|
export type ContextMutationFunction_T<C> = (ctx: C, e: Event_T, self: Interpreter_T<C>) => C;
|
|
export interface Goto_T {
|
|
type: 'Goto';
|
|
targetStateName: string;
|
|
}
|
|
export declare const Machine: <C>(...states: State_T<C>[]) => Machine_T<C>;
|
|
export declare const State: <C>(name: string, ...eventReactionCouplings: EventReactionCouplings_T<C>[]) => State_T<C>;
|
|
export declare const On: <C>(eventName: string, ...reactions: Reaction_T<C>[]) => EventReactionCouplings_T<C>;
|
|
export declare const SideEffect: <C>(fn: SideEffectFunction_T<C>) => SideEffect_T<C>;
|
|
export declare const Goto: (targetStateName: string) => Goto_T;
|
|
export declare const Context: <C>(fn: ContextMutationFunction_T<C>) => ContextMutation_T<C>;
|
|
export interface Interpreter_T<C> {
|
|
machine: Machine_T<C>;
|
|
state: string;
|
|
context: C;
|
|
eventQueue: Array<Event_T>;
|
|
subscriptions: Record<string, SubscriptionCallbackFunction_T<C>>;
|
|
isTransitioning: boolean;
|
|
isPaused: boolean;
|
|
start: () => Interpreter_T<C>;
|
|
subscribe: (callback: SubscriptionCallbackFunction_T<C>) => Interpreter_T<C>;
|
|
}
|
|
/**
|
|
* Description placeholder
|
|
*
|
|
* @export
|
|
* @param {Machine_T} machine
|
|
* @param {any} initialContext
|
|
* @param {?string} [initialStateName]
|
|
* @returns {Interpreter_T}
|
|
*/
|
|
export declare function Interpreter<C>(machine: Machine_T<C>, initialContext: any, initialStateName?: string): Interpreter_T<C>;
|
|
export declare function start<C>(interpreter: Interpreter_T<C>): void;
|
|
export declare function pause<C>(interpreter: Interpreter_T<C>): 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<C>(interpreter: Interpreter_T<C>, event: Event_T): void;
|
|
export declare const enqueue: typeof send;
|
|
export type SubscriptionCallbackFunction_T<C> = (self: Interpreter_T<C>) => void;
|
|
export declare function subscribe<C>(interpreter: Interpreter_T<C>, callback: SubscriptionCallbackFunction_T<C>): string;
|
|
export declare function unsubscribe<C>(interpreter: Interpreter_T<C>, subscriptionId: string): void;
|
|
export declare const Spawn: () => void;
|
|
export declare const Unspawn: () => void;
|