fsm-as-promised
Version:
A minimalistic finite state machine library using promises
105 lines (104 loc) • 2.32 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from 'events';
interface StateMachine extends EventEmitter {
/**
* The current state
*/
current: string;
/**
* Determines is the event can be triggered in the current state
*/
can(event: string): boolean;
/**
* Determines is the event can not be triggered in the current state
*/
cannot(event: string): boolean;
is(state: string): boolean;
/**
* Determines if the provided state is final
*/
isFinal(state: string): boolean;
/**
* Determines if the provided state exists in the current state machine
*/
hasState(state: string): boolean;
/**
* The instance id of the state machine
*/
instanceId(): string;
[k: string]: any;
}
interface EventSpecification {
/**
* The event name
*/
name: string;
/**
* The state in which the event is triggered
*/
from: string | string[];
/**
* The state in which the state machine transitiones when the event completes
*/
to?: string | string[];
/**
* The condition function that determines the state for conditional events
*/
condition?: {
(args: any[]): string | number | Promise<string | number>;
};
}
interface CallbackOptions {
/**
* Event name
*/
name: string;
from: string;
to: string;
/**
* Event arguments
*/
args: any[];
/**
* Event returned value
*/
res?: any;
}
interface StateMachineConfiguration {
/**
* The initial state
*
* @default 'none'
*/
initial?: string;
/**
* The final states
*/
final?: string | string[];
/**
* The events
*/
events: EventSpecification[];
/**
* The callbacks
*/
callbacks?: {
[k: string]: {
(options: CallbackOptions): void | Promise<void>;
};
};
/**
* Custom error handler
*/
error?: {
(message: string, options: {
name: string;
from: string;
}): Error;
};
}
interface StateMachineFactory {
(configuration: StateMachineConfiguration, target?: object): StateMachine;
}
declare const StateMachine: StateMachineFactory;
export default StateMachine;