Socket
Socket
Sign inDemoInstall

xstate

Package Overview
Dependencies
Maintainers
3
Versions
248
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xstate

Finite State Machines and Statecharts for the Modern Web.


Version published
Weekly downloads
1.5M
decreased by-5.69%
Maintainers
3
Weekly downloads
Ā 
Created

What is xstate?

The xstate npm package is a library for creating, interpreting, and executing finite state machines and statecharts, as well as managing invocations of those machines as actors. It provides a robust framework for modeling and analyzing application logic in a composable and declarative way, which can help manage complex state logic in UIs, robotics, and other systems.

What are xstate's main functionalities?

Finite State Machines

This feature allows you to create simple finite state machines. The code sample demonstrates a toggle machine with two states: 'active' and 'inactive'.

{"import { Machine } from 'xstate';\n\nconst toggleMachine = Machine({\n  id: 'toggle',\n  initial: 'inactive',\n  states: {\n    inactive: { on: { TOGGLE: 'active' } },\n    active: { on: { TOGGLE: 'inactive' } }\n  }\n});"}

Statecharts

This feature allows you to create statecharts, which are an extension of state machines that can have hierarchical and parallel states. The code sample shows a traffic light machine with three states: 'green', 'yellow', and 'red'.

{"import { Machine } from 'xstate';\n\nconst lightMachine = Machine({\n  id: 'light',\n  initial: 'green',\n  states: {\n    green: { on: { TIMER: 'yellow' } },\n    yellow: { on: { TIMER: 'red' } },\n    red: { on: { TIMER: 'green' } }\n  }\n});"}

Interpreters

This feature allows you to interpret and execute the state machines and statecharts. The code sample demonstrates how to start a service that interprets the toggleMachine and logs state transitions.

{"import { interpret } from 'xstate';\n\nconst service = interpret(toggleMachine).onTransition((state) => console.log(state.value));\n\nservice.start();\nservice.send('TOGGLE');\nservice.send('TOGGLE');"}

Actors

This feature allows you to manage machine invocations as actors, which can send and receive events from other machines. The code sample shows how to spawn a child machine within a parent machine's context.

{"import { spawn, Machine } from 'xstate';\n\nconst parentMachine = Machine({\n  context: {\n    child: null\n  },\n  entry: ['spawnChild']\n}, {\n  actions: {\n    spawnChild: assign({\n      child: () => spawn(someMachine)\n    })\n  }\n});"}

Other packages similar to xstate

Keywords

FAQs

Package last updated on 23 Oct 2023

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