Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@xstate/fsm
Advanced tools
@xstate/fsm is a lightweight finite state machine library for JavaScript and TypeScript. It allows you to model the behavior of your application in a predictable way by defining states and transitions. This can help manage complex state logic in a more maintainable and understandable manner.
Define a Finite State Machine
This feature allows you to define a finite state machine with states and transitions. In this example, a simple toggle machine is created with two states: 'inactive' and 'active'. The machine transitions between these states on the 'TOGGLE' event.
const { createMachine } = require('@xstate/fsm');
const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } }
}
});
console.log(toggleMachine.initialState); // { value: 'inactive' }
Transition Between States
This feature allows you to transition between states using the 'send' method. The example demonstrates how to start the machine and transition from 'inactive' to 'active' state by sending the 'TOGGLE' event.
const { createMachine, interpret } = require('@xstate/fsm');
const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } }
}
});
const toggleService = interpret(toggleMachine).start();
console.log(toggleService.state.value); // 'inactive'
toggleService.send('TOGGLE');
console.log(toggleService.state.value); // 'active'
State Actions
This feature allows you to define actions that should be executed when entering or exiting a state. The example shows how to log messages when entering and exiting the 'active' state.
const { createMachine, interpret } = require('@xstate/fsm');
const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: {
on: { TOGGLE: 'inactive' },
entry: () => console.log('Entering active state'),
exit: () => console.log('Exiting active state')
}
}
});
const toggleService = interpret(toggleMachine).start();
toggleService.send('TOGGLE'); // Logs: 'Entering active state'
toggleService.send('TOGGLE'); // Logs: 'Exiting active state'
robot3 is a finite state machine library for JavaScript that provides a simple API for defining states and transitions. It is similar to @xstate/fsm in that it allows you to model state logic in a predictable way, but it offers a more minimalistic approach.
javascript-state-machine is a library for creating finite state machines in JavaScript. It provides a straightforward API for defining states and transitions, similar to @xstate/fsm. However, it is more focused on simplicity and ease of use, making it a good choice for smaller projects.
redux-saga is a library that aims to make application side effects (e.g., asynchronous actions) easier to manage, more efficient to execute, and better at handling failures. While it is not a finite state machine library per se, it can be used to manage complex state logic in a Redux application, similar to how @xstate/fsm can be used.
This package contains a minimal implementation of XState for finite state machines.
Features:
state.changed
If you want to use statechart features such as nested states, parallel states, history states, activities, invoked services, delayed transitions, transient transitions, etc. please use XState
.
import { FSM, assign } from '@xstate/fsm';
const lightFSM = FSM({
id: 'light',
initial: 'green',
context: { redLights: 0 },
states: {
green: {
on: {
TIMER: 'yellow'
}
},
yellow: {
on: {
TIMER: {
target: 'red',
actions: () => console.log('Going to red!')
}
}
},
red: {
entry: assign({ redLights: ctx => ctx.redLights + 1 }),
on: {
TIMER: 'green'
}
}
}
});
const { initialState } = lightFSM;
// {
// value: 'green',
// context: { redLights: 0 },
// actions: [],
// changed: undefined
// }
const yellowState = lightFSM.transition(initialState, 'TIMER');
// {
// value: 'yellow',
// context: { redLights: 0 },
// actions: [
// { type: undefined, exec: () => console.log('Going to red!') }
// ],
// changed: true
// }
const redState = lightFSM.transition(yellowState, 'TIMER');
// {
// value: 'red',
// context: { redLights: 1 },
// actions: [],
// changed: true
// }
FAQs
XState for finite state machines
The npm package @xstate/fsm receives a total of 0 weekly downloads. As such, @xstate/fsm popularity was classified as not popular.
We found that @xstate/fsm demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.