UNPKG

node-state-machine

Version:
72 lines (60 loc) 2.43 kB
// @flow /** * Class representing a transition that has a certain trigger (function) and a * destination node (string). It also has a list of middlewares which can be * executed when the transition is executed. * @author Jensen Bernard */ class Transition { _checker: (v: string) => boolean; _destination: string; _middlewares: Array<(v: string) => void>; /** * Creates a new instance of a transition with the given checker/trigger, * destination node and list of middlewares. * @param checker {string} The trigger of the transition. * @param destination {string} The destination of the transition. * @param middlewares {array} The middlewares executed. */ constructor(checker: (v: string) => boolean, destination: string, middlewares: Array<(v: string) => void> = []) { this._checker = checker; this._destination = destination; this._middlewares = []; for(var middleware of middlewares) this._middlewares.push(middleware); } /** * Checks whether or not the transition has middleswares. Returns true if * and only if this._middlewares.length > 0. * @return {boolean} Whether or not the transition has middlewares. */ hasMiddlewares(): boolean { return this._middlewares.length > 0; } /** * Returns the list of middlewares of the current transition. This list will * be the same one that is passed as an argument in the constructor. * @return {array} List of middlewares. */ middlewares(): Array<(v: string) => void> { return this._middlewares; } /** * Checks whether or not the current transition fires based on the checker/ * trigger that is provided when this transition was created. * @return {boolean} Whether or not this transition should fire. */ fires(v: string): boolean { return this._checker(v); } /** * Returns the destination of the current transition. This is the one that * is provided as an argument when this transition is created. * @return {string} The destination state of this transition. */ destination(): string { return this._destination; } /** * Returns a string representation of this transition. * @return {string} A string representation of this transition. */ toString(): string { return `(f() > ${this._destination} (${this._middlewares.length}M))`; } } module.exports.Transition = Transition;