{"id":13467376,"url":"https://github.com/jakesgordon/javascript-state-machine","last_synced_at":"2025-05-12T15:24:22.801Z","repository":{"id":1536210,"uuid":"1834386","full_name":"jakesgordon/javascript-state-machine","owner":"jakesgordon","description":"A javascript finite state machine library","archived":false,"fork":false,"pushed_at":"2022-04-02T21:30:00.000Z","size":1616,"stargazers_count":8727,"open_issues_count":74,"forks_count":967,"subscribers_count":259,"default_branch":"master","last_synced_at":"2025-04-23T17:13:37.020Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jakesgordon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"docs/contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-06-01T22:02:01.000Z","updated_at":"2025-04-21T05:36:01.000Z","dependencies_parsed_at":"2022-07-12T14:55:35.844Z","dependency_job_id":null,"html_url":"https://github.com/jakesgordon/javascript-state-machine","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakesgordon%2Fjavascript-state-machine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakesgordon%2Fjavascript-state-machine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakesgordon%2Fjavascript-state-machine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakesgordon%2Fjavascript-state-machine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jakesgordon","download_url":"https://codeload.github.com/jakesgordon/javascript-state-machine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253764217,"owners_count":21960538,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T15:00:55.556Z","updated_at":"2025-05-12T15:24:22.772Z","avatar_url":"https://github.com/jakesgordon.png","language":"JavaScript","readme":"# Javascript State Machine\n\n[![NPM version](https://badge.fury.io/js/javascript-state-machine.svg)](https://badge.fury.io/js/javascript-state-machine)\n[![Build Status](https://travis-ci.org/jakesgordon/javascript-state-machine.svg?branch=master)](https://travis-ci.org/jakesgordon/javascript-state-machine)\n\nA library for finite state machines.\n\n![matter state machine](examples/matter.png)\n\n\u003cbr\u003e\n\n### NOTE for existing users\n\n\u003e **VERSION 3.0** Is a significant rewrite from earlier versions.\n  Existing 2.x users should be sure to read the [Upgrade Guide](docs/upgrading-from-v2.md).\n\n\u003cbr\u003e\n\n# Installation\n\nIn a browser:\n\n```html\n  \u003cscript src='state-machine.js'\u003e\u003c/script\u003e\n```\n\n\u003e after downloading the [source](dist/state-machine.js) or the [minified version](dist/state-machine.min.js)\n\nUsing npm:\n\n```shell\n  npm install --save-dev javascript-state-machine\n```\n\nIn Node.js:\n\n```javascript\n  var StateMachine = require('javascript-state-machine');\n```\n\n# Usage\n\nA state machine can be constructed using:\n\n```javascript\n  var fsm = new StateMachine({\n    init: 'solid',\n    transitions: [\n      { name: 'melt',     from: 'solid',  to: 'liquid' },\n      { name: 'freeze',   from: 'liquid', to: 'solid'  },\n      { name: 'vaporize', from: 'liquid', to: 'gas'    },\n      { name: 'condense', from: 'gas',    to: 'liquid' }\n    ],\n    methods: {\n      onMelt:     function() { console.log('I melted')    },\n      onFreeze:   function() { console.log('I froze')     },\n      onVaporize: function() { console.log('I vaporized') },\n      onCondense: function() { console.log('I condensed') }\n    }\n  });\n```\n\n... which creates an object with a current state property:\n\n  * `fsm.state`\n\n... methods to transition to a different state:\n\n  * `fsm.melt()`\n  * `fsm.freeze()`\n  * `fsm.vaporize()`\n  * `fsm.condense()`\n\n... observer methods called automatically during the lifecycle of a transition:\n\n  * `onMelt()`\n  * `onFreeze()`\n  * `onVaporize()`\n  * `onCondense()`\n\n... along with the following helper methods:\n\n  * `fsm.is(s)`            - return true if state `s` is the current state\n  * `fsm.can(t)`           - return true if transition `t` can occur from the current state\n  * `fsm.cannot(t)`        - return true if transition `t` cannot occur from the current state\n  * `fsm.transitions()`    - return list of transitions that are allowed from the current state\n  * `fsm.allTransitions()` - return list of all possible transitions\n  * `fsm.allStates()`      - return list of all possible states\n\n# Terminology\n\nA state machine consists of a set of [**States**](docs/states-and-transitions.md)\n\n  * solid\n  * liquid\n  * gas\n\nA state machine changes state by using [**Transitions**](docs/states-and-transitions.md)\n\n  * melt\n  * freeze\n  * vaporize\n  * condense\n\nA state machine can perform actions during a transition by observing [**Lifecycle Events**](docs/lifecycle-events.md)\n\n  * onBeforeMelt\n  * onAfterMelt\n  * onLeaveSolid\n  * onEnterLiquid\n  * ...\n\nA state machine can also have arbitrary [**Data and Methods**](docs/data-and-methods.md).\n\nMultiple instances of a state machine can be created using a [**State Machine Factory**](docs/state-machine-factory.md).\n\n# Documentation\n\nRead more about\n\n  * [States and Transitions](docs/states-and-transitions.md)\n  * [Data and Methods](docs/data-and-methods.md)\n  * [Lifecycle Events](docs/lifecycle-events.md)\n  * [Asynchronous Transitions](docs/async-transitions.md)\n  * [Initialization](docs/initialization.md)\n  * [Error Handling](docs/error-handling.md)\n  * [State History](docs/state-history.md)\n  * [Visualization](docs/visualization.md)\n  * [State Machine Factory](docs/state-machine-factory.md)\n  * [Upgrading from 2.x](docs/upgrading-from-v2.md)\n \n# Contributing\n\nYou can [Contribute](docs/contributing.md) to this project with issues or pull requests.\n\n# Release Notes\n\nSee [RELEASE NOTES](RELEASE_NOTES.md) file.\n\n# License\n\nSee [MIT LICENSE](https://github.com/jakesgordon/javascript-state-machine/blob/master/LICENSE) file.\n\n# Contact\n\nIf you have any ideas, feedback, requests or bug reports, you can reach me at\n[jake@codeincomplete.com](mailto:jake@codeincomplete.com), or via\nmy website: [Code inComplete](http://codeincomplete.com/)\n","funding_links":[],"categories":["JavaScript","🔧 Utilities \u0026 Miscellaneous","目录","Libraries"],"sub_categories":["JavaScript"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakesgordon%2Fjavascript-state-machine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjakesgordon%2Fjavascript-state-machine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakesgordon%2Fjavascript-state-machine/lists"}