Create a new finite state machine
the initial state that this FSM should be in
the set of possible states to transition between
The currently allowed transitions
Emitter for notifying of state changes
The current state
The states and their allowed transitions
Change the state. Will return whether the transition was allowed or not.
the new state
true is the change of state was successful
the reason for closing the stream. This can be of any type. The type of the close reason should be documented in the class extending StreamImpl.
Remove a listener from a specified event.
Example:
// Bind a single listener to the 'foo' event and then deregister it
var listener = function() {};
stream.on('foo', listener);
stream.off('foo', listener);
Example:
// Bind a listener to the 'foo' event and deregister all listeners
var listener = function() {};
stream.on('foo', listener);
stream.off('foo');
the listener to remove. All listeners for the event are removed if this is not specified. This argument is ignored if the first argument is a CallbackMap.
this stream.
Register listeners against events.
A single listener may be bound to an event by passing the event name and listener function.
Multiple listeners may be bound by passing in a CallbackMap, mapping event names to listener functions.
Example:
// Bind a single listener to the 'foo' event
stream.on('foo', function(arg1, arg2) {
console.log("Called for 'foo' event", arg1, arg2);
});
Example:
// Bind multiple listeners
stream.on({
foo : function() { ... },
bar : function() { ... },
baz : function() { ... }
});
It is possible to bind multiple listeners to the same event. In this case, the listeners will be called in the order they were bound.
the event name or CallbackMap mapping event names to listeners
the listener to bind to the event, if passed as string. This argument is ignored if the first argument is a CallbackMap.
this stream.
Copyright © 2025 DiffusionData Ltd. All Rights Reserved.
Minimal finite state machine implementation. Allows directed transitions between states.
States are provided as an object, with each key being single distinct state. Each state's value within the object should be an array of strings, which dictates the available states that are legal to transition to.
If a state may transition to any other state, the string
'*'may be provided instead of an array.If a state is terminal, it should simply provide an empty array.
Example:
// Create a state machine with three states: // 'a' may transition to 'b' or 'c' // 'b' is a terminal state // 'c' may transition to any state var states = { a : ['b', 'c'], b : [], c : '*' }; var fsm = new FSM('a', states); states.change('c'); // => true states.change('a'); // => true states.change('b'); // => true states.change('c'); // => false states.change('a'); // => false