What is finity?
The finity npm package is a lightweight and flexible state machine library for JavaScript. It allows you to define and manage states and transitions in a clear and concise manner, making it easier to handle complex state logic in your applications.
What are finity's main functionalities?
Define a State Machine
This code sample demonstrates how to define a simple state machine with three states: 'idle', 'running', and 'paused'. It shows how to configure transitions between these states based on events such as 'start', 'pause', 'resume', and 'stop'.
const finity = require('finity');
const stateMachine = finity
.configure()
.initialState('idle')
.state('idle')
.on('start').transitionTo('running')
.state('running')
.on('pause').transitionTo('paused')
.on('stop').transitionTo('idle')
.state('paused')
.on('resume').transitionTo('running')
.on('stop').transitionTo('idle')
.start();
stateMachine.handle('start'); // Transition to 'running'
stateMachine.handle('pause'); // Transition to 'paused'
stateMachine.handle('resume'); // Transition to 'running'
stateMachine.handle('stop'); // Transition to 'idle'
Handle State Transitions
This code sample shows how to handle state transitions and retrieve the current state of the state machine. It demonstrates transitioning from 'idle' to 'running' and then to 'paused', and how to check the current state after each transition.
const finity = require('finity');
const stateMachine = finity
.configure()
.initialState('idle')
.state('idle')
.on('start').transitionTo('running')
.state('running')
.on('pause').transitionTo('paused')
.on('stop').transitionTo('idle')
.state('paused')
.on('resume').transitionTo('running')
.on('stop').transitionTo('idle')
.start();
stateMachine.handle('start'); // Transition to 'running'
console.log(stateMachine.getCurrentState()); // Output: 'running'
stateMachine.handle('pause'); // Transition to 'paused'
console.log(stateMachine.getCurrentState()); // Output: 'paused'
Add Entry and Exit Actions
This code sample demonstrates how to add entry and exit actions to states. It shows how to log messages when entering and exiting states, providing a way to execute custom logic during state transitions.
const finity = require('finity');
const stateMachine = finity
.configure()
.initialState('idle')
.state('idle')
.onEntry(() => console.log('Entering idle state'))
.on('start').transitionTo('running')
.state('running')
.onEntry(() => console.log('Entering running state'))
.onExit(() => console.log('Exiting running state'))
.on('pause').transitionTo('paused')
.on('stop').transitionTo('idle')
.state('paused')
.onEntry(() => console.log('Entering paused state'))
.on('resume').transitionTo('running')
.on('stop').transitionTo('idle')
.start();
stateMachine.handle('start'); // Logs 'Entering running state'
stateMachine.handle('pause'); // Logs 'Exiting running state' and 'Entering paused state'
stateMachine.handle('resume'); // Logs 'Exiting paused state' and 'Entering running state'
Other packages similar to finity
xstate
XState is a robust and feature-rich state machine library for JavaScript and TypeScript. It provides a comprehensive set of tools for defining, visualizing, and interpreting state machines and statecharts. Compared to finity, XState offers more advanced features such as hierarchical states, parallel states, and built-in support for statechart visualization.
javascript-state-machine
The javascript-state-machine package is a simple and lightweight state machine library for JavaScript. It allows you to define states and transitions in a straightforward manner. While it is similar to finity in terms of simplicity, it lacks some of the advanced features and flexibility that finity offers.
machina
Machina is a powerful state machine library for JavaScript that supports hierarchical states, event emitters, and more. It is designed for complex state management scenarios and provides a rich API for defining and managing states. Compared to finity, machina offers more advanced features and is suitable for more complex use cases.