Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

finity

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

finity

A finite state machine library for Node.js and the browser with a friendly configuration DSL

  • 0.5.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

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

Keywords

FAQs

Package last updated on 07 Mar 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc