or simply a state machine, is a behavioral model used to design computer programs. It is composed of a finite number of states associated to transitions. A transition is a set of actions that starts from one state and ends in another (or the same) state. A transition is started by a trigger, and a trigger can be an event or a condition. Finite-state machines can model a large number of problems, among which are electronic design automation, communication protocol design, parsing and other engineering applications.... [1] [1] http://en.wikipedia.org/wiki/Finite-state_machine
Javascript & node.js offer a ‘run to completion’ environment node.js has three common event sources: Callback ▪ Usually function (err, ...) ▪ 2 events – error or success EventEmitters ▪ Lots of possible events (unfortunately unknown) Timers (one off and interval) Code gets complex quickly: Multiple callbacks, EventEmitters, timeouts etc Needs a design metaphor to make manageable
to JavaScript/node.js Explore best ways to define FSMs in JS Provide tools to help design and implementation process Small core, but make extensible Environment first, speed later More than just another async framework: • Designed for node.js and its standard patterns • Built from ground-up to support hierarchical state machines • Small, simple core with plug-ins and add-ons to extend capabilities • Draw your code as State Machine Diagrams • Linting: Check your code makes sense
function: make sure the state is legal] [Entry function: ‘start’ the state] Wait New event: [Action function: decide how to deal with event: ▪ Stay in state ▪ Re-enter state ▪ Go to another state] [Exit function: tidy-up] Guard A state Entry Exit Action
you can draw them on a whiteboard and discuss 2. Layer complexity by using hierarchical machines 3. Re-use standard design patterns 4. As formal as you want to be Debugging: 5. States and events have names 6. Single SM: ‘stack’ of recent state/event history 7. Multi SM: can view recent interactions with states and events 8. (Unexpected) errors are attached to the machine Implementation: 9. Flatten embedded callbacks 10. Share states, actions and machines across multiple SMs 11. Lots of plugins to ease async pain (whilst providing parallel operations) 12. Multi-process
object-oriented design’ ‘In asynchronous programming behavioural design is as important as structural design’ ‘In asynchronous programming state machines are as important as classes and objects’