
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
This hierarchical state machine implementation has been inspired by the QP active object framework, see http://www.state-machine.com/.
State machine is defined as an object with initialState
and states
properties. The former defines
the first state we want our machine to enter. The latter is an object with states, events and actions:
var lightSwitch = _.extend({
initialState: "Out",
states: {
'Out': {
'on': { target: 'On' },
'out': { target: 'Out' }
},
'On': {
'on': { target: 'On' },
'out': { target: 'Out' }
}
}
}, Statechart);
// Initialize the state machine and make the initial transition (to the `Out` state).
lightSwitch.run();
// Dispatch the `on` event to the machine which causes it to transit to the `On` state.
lightSwitch.dispatch('on');
The state machine dispatched three reserved events: init
, entry
and exit
. These are special
events that you might react on when an initial transition to a state takes place, when a state is entered or exited.
Assume we use the same machine as defined in the above example and run it like this:
lightSwtich.run();
lightSwitch.dispatch('out');
lightSwitch.dispatch('on');
The resulting order of transitions would then be:
out
event was dispatchedon
event was dispatchedCustom events are named events that we dispatch to the machine (like the on
and out
events in the above example).
As a reaction on these events, we might want to either transit to another state, execute an action while doing that or
guard the transition if there is a certain condition that must be met in order for the transition to take place.
'MyState': {
'myEvent': {
guard: function() { return this.mySlot === true; },
action: function() { console.log('Hooray, transition takes place.'),
target: 'AnotherState'
}
},
'AnotherState': {
}
States can be nested to an arbitrary level. State nesting leads to behavioral inheritance [Samek+ 00, 02]. This allows new states to be specified by difference rather then created from scratch each time.
State nesting can simply be done by nesting objects.
'MyState': {
'init': 'MyChildState',
'eventA': { ... },
'MyChildState': {
'entry': function() { console.log('MyChildState being entered.'); },
'eventB': { ... }
}
}
See https://github.com/DavidDurman/statechart/blob/master/test/samek.js for a complete example of a non-trivial state machine.
Copyright (c) 2010 David Durman
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://opensource.org/licenses/MIT.
FAQs
StateChart implementation.
We found that statechart 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.