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.

94 lines
5.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> | Peer_T<C, unknown> | 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 Peer_T<C, C_Peer> {
type: 'SetPeer' | 'SetPeers' | 'AddPeers';
name: string;
peerCreationFunction: PeerCreationFunction_T<C, C_Peer>;
}
export type PeerCreationFunction_T<C, C_Peer> = (ctx: C, e: Event_T, self: Interpreter_T<C>) => Interpreter_T<C_Peer> | Array<Interpreter_T<C_Peer>>;
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 declare const Peer: <C, C_Peer>(name: string, peerCreationFunction: PeerCreationFunction_T<C, C_Peer>) => Peer_T<C, C_Peer>;
export declare const Peers: <C, C_Peer>(name: string, peerCreationFunction: PeerCreationFunction_T<C, C_Peer>) => Peer_T<C, C_Peer>;
export declare const AddPeers: <C, C_Peer>(name: string, peerCreationFunction: PeerCreationFunction_T<C, C_Peer>) => Peer_T<C, C_Peer>;
export interface Interpreter_T<C> {
machine: Machine_T<C>;
state: string;
context: C;
peers: Record<string, Interpreter_T<unknown> | Array<Interpreter_T<unknown>>>;
peerSubscriptionIds: Map<Interpreter_T<unknown>, string>;
eventQueue: Array<Event_T>;
subscriptionsToEvents: Record<string, EventsSubscriptionCallbackFunction_T<C>>;
subscriptionsToState: Record<string, StateSubscriptionCallbackFunction_T<C>>;
subscriptionsToSettledState: Record<string, SettledStateSubscriptionCallbackFunction_T<C>>;
isTransitioning: boolean;
isPaused: boolean;
start: () => Interpreter_T<C>;
Peer: (name: string, peer: Interpreter_T<any>) => Interpreter_T<C>;
Peers: (name: string, peers: Array<Interpreter_T<any>>) => 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 EventsSubscriptionCallbackFunction_T<C> = (e: Event_T, self: Interpreter_T<C>) => void;
export type StateSubscriptionCallbackFunction_T<C> = (e: Event_T, self: Interpreter_T<C>) => void;
export type SettledStateSubscriptionCallbackFunction_T<C> = (self: Interpreter_T<C>) => void;
export declare function subscribe<C>(interpreter: Interpreter_T<C>, callback: SettledStateSubscriptionCallbackFunction_T<C>): string;
export declare const subscribeToSettledState: typeof subscribe;
export declare function subscribeToState<C>(interpreter: Interpreter_T<C>, callback: StateSubscriptionCallbackFunction_T<C>): string;
export declare function subscribeToEvents<C>(interpreter: Interpreter_T<C>, callback: StateSubscriptionCallbackFunction_T<C>): string;
export declare function unsubscribe<C>(interpreter: Interpreter_T<C>, subscriptionId: string): void;
export declare function setPeer<C, C_Peer>(self: Interpreter_T<C>, name: string, peer: Interpreter_T<C_Peer>): void;
export declare function setPeers<C, C_Peer>(self: Interpreter_T<C>, name: string, peers: Array<Interpreter_T<C_Peer>>): void;
export declare function addPeers<C, C_Peer>(self: Interpreter_T<C>, name: string, peers: Array<Interpreter_T<C_Peer>>): void;