{"id":50318271,"url":"https://github.com/catinrage/miuz","last_synced_at":"2026-05-29T01:30:36.318Z","repository":{"id":228440119,"uuid":"774036685","full_name":"catinrage/miuz","owner":"catinrage","description":"A TypeScript state machine library with type safety for defining and managing state transitions with entry, exit, and event actions.","archived":false,"fork":false,"pushed_at":"2024-03-19T08:45:49.000Z","size":38,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-03-19T21:14:26.836Z","etag":null,"topics":["finite-state-machine","fsm","state","state-machine","state-management","statechart"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/catinrage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-03-18T20:43:08.000Z","updated_at":"2024-03-19T20:22:58.000Z","dependencies_parsed_at":"2024-03-18T21:14:26.635Z","dependency_job_id":"aa17606a-c71b-4504-8f29-30474dc00cd5","html_url":"https://github.com/catinrage/miuz","commit_stats":null,"previous_names":["catinrage/muz"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/catinrage/miuz","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catinrage%2Fmiuz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catinrage%2Fmiuz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catinrage%2Fmiuz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catinrage%2Fmiuz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/catinrage","download_url":"https://codeload.github.com/catinrage/miuz/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/catinrage%2Fmiuz/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33633468,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-28T02:00:06.440Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["finite-state-machine","fsm","state","state-machine","state-management","statechart"],"created_at":"2026-05-29T01:30:35.173Z","updated_at":"2026-05-29T01:30:36.288Z","avatar_url":"https://github.com/catinrage.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TypeScript State Machine\n\nThis TypeScript state machine library provides a robust framework for defining and managing state transitions in your applications. It ensures type safety throughout the process, allowing you to define states, events, and actions with precision.\n\n## Features\n\n- **Type Safety:** Enjoy the benefits of TypeScript's static typing to catch errors at compile-time.\n- **State Transitions:** Define states and easily manage transitions between them.\n- **Entry and Exit Actions:** Execute custom actions when entering or exiting states.\n- **Event Handling:** Handle events within states, with support for custom actions and target states.\n- **After Events:** Trigger actions after a specified delay.\n- **Context Management:** Manage context data associated with states and events.\n- **Subscription:** Subscribe to state changes and context updates.\n\n## Installation\n\nYou can install the package via npm:\n\n```bash\nnpm install miuz\n# or\nyarn add miuz\n# or\nbun add miuz\n# ...\n```\n\n## Documentation\n\n### MachineConfiguration\n\nThe `MachineConfiguration` class is used to define the state machine configuration. It provides a fluent API to declare states, events, actions, and context.\n\n#### Constructor\n\n```typescript\nnew MachineConfiguration(config: {\n  declare: string[];\n  states: {\n    [state: string]: {\n      on: {\n        [event: string]: {\n          target?: string;\n          action?: (context: any, data?: any) =\u003e void;\n          after?: number;\n        };\n      };\n    };\n  };\n  initial: string;\n  context?: any;\n});\n```\n\n- `declare`: An array of state names to declare.\n- `states`: An object containing state definitions. Each state definition contains an `on` object, which maps event names to event handlers.\n- `initial`: The initial state of the state machine.\n- `context`: An optional object to define the initial context of the state machine.\n\n#### Methods\n\n- `create(): StateMachine`: Creates a new instance of the state machine.\n\n### StateMachine\n\nThe `StateMachine` class represents an instance of the state machine. It provides methods to start, and send events to the state machine, as well as subscribe to state changes and context updates.\n\n#### Methods\n\n- `start(): StateMachine`: Starts the state machine.\n- `send\u003cT extends string\u003e(): (event: string, data?: any) =\u003e void`: Sends an event to the state machine. The generic type `T` is used to specify the current state of the state machine.\n- `subscribe(listener: (state: string, context: any, type: 'State-Change' | 'Context-Change') =\u003e void): void`: Subscribes to state changes and context updates.\n\n## Usage Example\n\nHere's a simple example to demonstrate the usage of the library:\n\n```typescript\nimport { MachineConfiguration } from 'miuz';\n\n// Define your state machine configuration\nconst config = new MachineConfiguration({\n  declare: ['reading', 'editing'],\n  states: {\n    reading: {\n      on: {\n        edit: {\n          target: 'editing',\n          action(context, data) {}\n        }\n      }\n    },\n    editing: {\n      on: {\n        cancel: {\n          target: 'reading'\n        },\n        save: {\n          target: 'reading',\n          action: (context) =\u003e {\n            context.committedValue = context.value;\n          }\n        },\n        edit: {\n          action: (context, data: 'A' | 'B') =\u003e {\n            context.value = data;\n          }\n        }\n      }\n    }\n  },\n  initial: 'reading',\n  context: {\n    committedValue: '',\n    value: ''\n  }\n});\n\n// Create an instance of the state machine and start it\nconst instance = config.create().start();\n\n// Subscribe to state changes\ninstance.subscribe(({ state, context }, type) =\u003e {\n  if (type === 'Context-Change') return;\n  console.log(context.committedValue);\n});\n\n// Send events to trigger state transitions\ninstance.send\u003c'reading'\u003e()('edit');\ninstance.send\u003c'editing'\u003e()('edit', 'A');\ninstance.send\u003c'editing'\u003e()('save');\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a pull request or open an issue if you encounter any problems or have any suggestions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcatinrage%2Fmiuz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcatinrage%2Fmiuz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcatinrage%2Fmiuz/lists"}