{"id":20471423,"url":"https://github.com/statewalker/statewalker-fsm","last_synced_at":"2026-04-20T01:03:10.321Z","repository":{"id":61392986,"uuid":"533520748","full_name":"statewalker/statewalker-fsm","owner":"statewalker","description":null,"archived":false,"fork":false,"pushed_at":"2026-04-04T20:22:31.000Z","size":204,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-04T22:39:22.928Z","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/statewalker.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-09-06T22:26:47.000Z","updated_at":"2026-04-04T20:22:34.000Z","dependencies_parsed_at":"2024-04-14T20:39:26.081Z","dependency_job_id":"d47b5b7c-999d-4d9e-b223-51d8026fb8d0","html_url":"https://github.com/statewalker/statewalker-fsm","commit_stats":{"total_commits":20,"total_committers":1,"mean_commits":20.0,"dds":0.0,"last_synced_commit":"0bed53f0e235b1cd75ded6cf56252c9182f4c766"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/statewalker/statewalker-fsm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-fsm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-fsm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-fsm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-fsm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/statewalker","download_url":"https://codeload.github.com/statewalker/statewalker-fsm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-fsm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32028547,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T00:18:06.643Z","status":"ssl_error","status_checked_at":"2026-04-20T00:17:31.068Z","response_time":55,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2024-11-15T14:16:04.670Z","updated_at":"2026-04-20T01:03:10.311Z","avatar_url":"https://github.com/statewalker.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @statewalker/fsm: Hierarchical Finite State Machine\n\nClass-based HFSM with nested states, event-driven transitions, lifecycle hooks, and dump/restore serialization.\n\n## Core Classes\n\n### FsmStateConfig\n\nDeclarative state tree definition:\n\n```typescript\nconst config: FsmStateConfig = {\n  key: \"Main\",\n  transitions: [\n    [\"\", \"start\", \"Active\"],     // initial → Active\n    [\"Active\", \"done\", \"\"],      // Active → final\n    [\"*\", \"reset\", \"Active\"],    // any → Active\n  ],\n  states: [\n    { key: \"Active\", states: [\n      { key: \"Step1\" },\n      { key: \"Step2\" },\n    ], transitions: [\n      [\"\", \"\", \"Step1\"],         // initial → Step1 (eventless)\n      [\"Step1\", \"next\", \"Step2\"],\n    ]},\n  ],\n};\n```\n\n### FsmProcess\n\nRuntime state machine. Maintains a state stack (root → ... → leaf), dispatches events, manages lifecycle.\n\n- `dispatch(event)` — trigger a transition\n- `shutdown()` — exit all states gracefully\n- `state` — current leaf state\n- `status` — bitmask tracking enter/exit cycle\n- `onStateCreate(handler)` — called for every new state (primary extension point)\n- `dump()` / `restore(data)` — serialization hooks\n\n### FsmState\n\nIndividual node in the state hierarchy.\n\n- `key` — state name\n- `parent` — parent state\n- `onEnter(handler)` — run when entering\n- `onExit(handler)` — run when exiting (reverse order)\n- `onStateError(handler)` — error handling\n- `dump()` / `restore(data)` — per-state serialization\n\n## Orchestrator\n\n### startProcess(context, config, load, startEvent?)\n\nHigh-level entry point: creates FsmProcess, wires handlers, binds FSM into context.\n\nContext keys bound:\n- `fsm:dispatch` — dispatch function\n- `fsm:terminate` — shutdown function\n- `fsm:states` — current state stack\n- `fsm:event` — last event\n\n`load(stateKey)` returns handler(s) for each state. Handlers can return:\n- `void` — no cleanup\n- `Function` — registered as onExit cleanup\n- `AsyncGenerator` — yielded events are dispatched to FSM\n\n### HandlerRegistry\n\nConvention-based handler discovery via `createHandlerRegistry()`:\n\n```typescript\nconst registry = createHandlerRegistry();\nregistry.addConfig(\"MyProcess\", config);\nregistry.addHandlers(\"MyProcess\", { \"Active\": activeHandler, \"Step1\": step1Handler });\nconst load = registry.getLoader(\"MyProcess\");\n```\n\n### launcher(config)\n\nMulti-process launcher with strict types:\n\n```typescript\ninterface LauncherConfig {\n  processes: ProcessDef[];\n  start?: string[];\n  context?: (parent: Record\u003cstring, unknown\u003e) =\u003e Record\u003cstring, unknown\u003e;\n}\n\ninterface ProcessDef {\n  name: string;\n  config: FsmStateConfig;\n  handlers?: (StageHandler | Record\u003cstring, StageHandler | StageHandler[]\u003e)[];\n  start?: boolean;\n}\n```\n\n## Utilities\n\n- `printer(process)` — log state transitions to console\n- `tracer(process)` — collect transition trace for testing\n\n## Migration from pre-0.35\n\nRemoved in 0.35:\n- `FsmBaseClass.data`, `.setData()`, `.getData()` — use closures or context instead\n- `FsmState.getData(key, recursive)`, `.useData(key)` — use closures\n- `FsmBaseClass._runHandlerParallel()` — handlers run sequentially now\n- `newFsmProcess()` — use `startProcess()` from orchestrator\n- `utils/handlers.ts` (`addSubstateHandlers`, `callStateHandlers`) — use HandlerRegistry\n- `utils/process.ts` — use `startProcess()` directly\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatewalker%2Fstatewalker-fsm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstatewalker%2Fstatewalker-fsm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatewalker%2Fstatewalker-fsm/lists"}