{"id":18809907,"url":"https://github.com/bytebit-org/roblox-finitestatemachine","last_synced_at":"2025-04-13T20:29:20.205Z","repository":{"id":57138696,"uuid":"422006005","full_name":"Bytebit-Org/roblox-FiniteStateMachine","owner":"Bytebit-Org","description":"A simple Finite State Machine implementation for Roblox development written using roblox-ts","archived":false,"fork":false,"pushed_at":"2023-11-03T23:08:32.000Z","size":227,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-08-09T02:23:16.339Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/Bytebit-Org.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-27T23:45:37.000Z","updated_at":"2022-09-13T13:02:22.000Z","dependencies_parsed_at":"2022-09-03T10:40:31.892Z","dependency_job_id":null,"html_url":"https://github.com/Bytebit-Org/roblox-FiniteStateMachine","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bytebit-Org%2Froblox-FiniteStateMachine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bytebit-Org%2Froblox-FiniteStateMachine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bytebit-Org%2Froblox-FiniteStateMachine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bytebit-Org%2Froblox-FiniteStateMachine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bytebit-Org","download_url":"https://codeload.github.com/Bytebit-Org/roblox-FiniteStateMachine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223602832,"owners_count":17171996,"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-11-07T23:18:09.225Z","updated_at":"2024-11-07T23:18:11.254Z","avatar_url":"https://github.com/Bytebit-Org.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Finite State Machine\n\u003cp align=\"center\"\u003e\n\t\u003ca href=\"https://github.com/Bytebit-Org/roblox-FiniteStateMachine/actions\"\u003e\n        \u003cimg src=\"https://github.com/Bytebit-Org/roblox-FiniteStateMachine/workflows/CI/badge.svg\" alt=\"CI status\" /\u003e\n    \u003c/a\u003e\n\t\u003ca href=\"http://makeapullrequest.com\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/badge/PRs-welcome-blue.svg\" alt=\"PRs Welcome\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/badge/License-MIT-blue.svg\" alt=\"License: MIT\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://discord.gg/QEz3v8y\"\u003e\n\t\t\u003cimg src=\"https://img.shields.io/badge/discord-join-7289DA.svg?logo=discord\u0026longCache=true\u0026style=flat\" alt=\"Discord server\" /\u003e\n\t\u003c/a\u003e\n\u003c/p\u003e\n\nA simple Finite State Machine implementation for Roblox development written using roblox-ts.\n\n## Installation\n### roblox-ts\nSimply install to your [roblox-ts](https://roblox-ts.com/) project as follows:\n```\nnpm i @rbxts/finite-state-machine\n```\n\n### Wally\n[Wally](https://github.com/UpliftGames/wally/) users can install this package by adding the following line to their `Wally.toml` under `[dependencies]`:\n```\nFiniteStateMachine = \"bytebit/finite-state-machine@1.0.11\"\n```\n\nThen just run `wally install`.\n\n### From model file\nModel files are uploaded to every release as `.rbxmx` files. You can download the file from the [Releases page](https://github.com/Bytebit-Org/roblox-FiniteStateMachine/releases) and load it into your project however you see fit.\n\n### From model asset\nNew versions of the asset are uploaded with every release. The asset can be added to your Roblox Inventory and then inserted into your Place via Toolbox by getting it [here.](https://www.roblox.com/library/7872552904/Finite-State-Machine-Package)\n\n## Documentation\n\n### IReadonlyFiniteStateMachine interface\nThis interface is meant to be used when there is a consumer that merely cares about the state of the machine but not to do anything to affect that state.\n\nIt requires two generic inputs - a `StateType` and an `EventType`. Both must simply fit the type `defined`.\n\n#### stateChanged Signal\n`readonly stateChanged: IReadOnlySignal\u003c(newState: StateType, oldState: StateType, event: EventType) =\u003e void\u003e;`\n\nFired when the current state is changed by an event.\n\n#### getCurrentState Method\n`getCurrentState(): StateType;`\n\nUsed to get the current state of the machine.\n\n### FiniteStateMachine class\n\n_Implements everything from `IReadonlyFiniteStateMachine`._\n\nThis is the root of this package.\n\n#### Construction\nIn order to construct a `FiniteStateMachine` instance, you will need to define your states, events, and the transition relationships between them. The transition relationships are represented using a nested tuple of `[[fromState, event], toState]`.\n\nTake this on/off switch as an example:\n```ts\ntype StateType = \"On\" | \"Off\";\ntype EventType = \"Toggle\";\nconst stateTransitions = new Map\u003c[StateType, EventType], StateType\u003e([\n\t[[\"Off\", \"Toggle\"], \"On\"],\n\t[[\"On\", \"Toggle\"], \"Off\"]\n]);\nconst fsm = FiniteStateMachine.create(\"Off\", stateTransitions);\n```\n\n#### destroy Method\n`destroy(): void`\n\nThis method is used to destroy an instance. That instance will throw exceptions if any other methods are used after this is invoked.\n\n#### handleEvent Method\n`handleEvent(event: EventType): void`\n\nThis method is used to effect a state transition though an event. If the given event has a valid state to transition to from the current state as per the state transitions at the time of the instance's construction, the state will be changed accordingly; otherwise, an error will be raised.\n\nExample using the on/off switch from above:\n```ts\nfunction toggleSwitch() {\n\tfsm.handleEvent(\"Toggle\");\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebit-org%2Froblox-finitestatemachine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbytebit-org%2Froblox-finitestatemachine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebit-org%2Froblox-finitestatemachine/lists"}