
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
async-state-machine
Advanced tools
An Asynchronous State Machine Implementation written in Javascript
This is a package that uses the state machine pattern in an asynchronous manner.
This allows us to do some cool things in our code and extends the capabilities of state machines to not have to rely on synchronous steps.
State's are your big
To set new states-
const OffState = Object.create(State)
.setName('off')
.onExit(() => console.log('Leaving off State'))
.onEntry(() => console.log('Switching Off'))
.onSuccess(() => console.log('State is now off'));
const OnState = Object.create(State).setName('on');
The lifecycles methods onEntry
, onExit
, and onSuccess
are all optional.
name | params | returns | description |
---|---|---|---|
setName | string | self | Set name of current state object |
setOnEntry | function (async optional) | self | Set function to be fired when state is entered |
setOnExit | function (async optional) | self | Set function to be fired when state is left |
setOnSuccess | function (async optional) | self | Set function to be fired when state has changed |
A transition is a simple object which only contains it's name and conditions
If we wanted to create a transition which only fires when the sky is blue we can say
const switch_off = Object.create(Transition)
.setName('switch_off')
.addCondition((context, params) => params.heat === 100);
but typically your transition might be as simple as
const switch_on = Object.create(Transition).setName('switch_on');
I've opted to prefer to use Object.create(Transition)
for the more simple objects versus
the class syntax new Transition()
as I don't believe a Transition needs to be a class.
This is a personaly preferance and if there is a case made for Transition's being classes we can go that route.
name | params | returns | description |
---|---|---|---|
setName | string | self | Set the name for this state |
addCondition | condition: () => boolean | self | Adds a new condition for this transition |
An edge is defined as:
Edge.new()
.from([LIST, OF, FROM, STATES])
.to(SINGLE_TO_STATE)
.with(name_of_transition);
Edges are used to define your state machine steps, where each step is a new edge- for example given a simple state graph:
OFF_STATE
(switch_on)-> ON_STATE
(switch_off)-> OFF_STATE
We can define this with the two edges being:
const OFF_TO_ON = Edge.new().from([OFF_STATE]).to(ON_STATE).with(switch_on)
const ON_TO_OFF = Edge.new().from([ON_STATE]).to(OFF_STATE).with(switch_off)
name | params | returns | description |
---|---|---|---|
new | - | new Edge object | Return a new Edge Object |
from | fromStates: Array | self | Set from states |
to | toState: StateObject | self | Set to state |
transition | transition: StateObject | self | Set transition applicable to this edge |
with | transition: StateObject | self | Set transition applicable to this edge |
From the Machine class we can add our edges
const MyMachine = new Machine();
MyMachine.registerEdge(OFF_TO_ON)
.registerEdge(ON_TO_OFF)
.setInitialState(OFF);
To then trigger a transition we simple use
MyMachine.triggerTransition(switch_off);
name | params | returns | description |
---|---|---|---|
getTransitions | none | Array | Returns available transitions from this state |
getState | none | string | Returns current state |
setDataGetter | function | self | passed in function is bound to context of Machine and is called whene data is returned |
setInitialState | state: StateObject, params: object, delayMachineStart: boolean | self | set's state which machine should start at, optionally do not start machine here |
triggerTransition | transition: TransitionObject | Promise | Transitions to next state |
registerEdge | edge: EdgeObject | self | Register a new Edge onto the Machine |
carry | none | self | Useful when dot chaining |
start | params?: Object | Promise | Transitions to initial state |
import Machine from 'async-state-machine';
import redux from '../store';
class ReduxMachine extends Machine {
reduxEnabled: boolean;
machineKey: string;
constructor(reduxEntry: string | void, enableLog: boolean = false) {
super(enableLog);
this.reduxEnabled = false;
if (reduxEntry !== null) {
// add entry as soon as class is created
this.redux(reduxEntry);
}
}
redux(name: string) {
// add entry as soon as class is created
this.machineKey = name;
redux.dispatch({
type: 'REGISTER_REDUX_MACHINE',
payload: { machine: this.machineKey, newState: '' },
});
this.reduxEnabled = true;
}
storeRedux(newState) {
if (this.reduxEnabled) {
redux.dispatch({
type: 'UPDATE_REDUX_MACHINE',
payload: { machine: this.machineKey, newState },
});
}
}
setState(nextState: string) {
super.setState(nextState);
this.storeRedux(nextState.toString());
}
}
export default ReduxMachine;
FAQs
An Asynchronous State Machine Implementation written in Javascript
The npm package async-state-machine receives a total of 0 weekly downloads. As such, async-state-machine popularity was classified as not popular.
We found that async-state-machine demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.