{"_id":"@ap-ent/state-machine","name":"@ap-ent/state-machine","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@ap-ent/state-machine","version":"1.0.0","description":"Zero-dependency typed finite state machine for TypeScript","license":"MIT","author":{"name":"Gordon Schauer"},"type":"module","main":"./dist/cjs/index.cjs","module":"./dist/esm/index.js","types":"./dist/esm/index.d.ts","exports":{".":{"types":"./dist/esm/index.d.ts","import":"./dist/esm/index.js","require":"./dist/cjs/index.cjs"}},"scripts":{"build":"tsup src/index.ts --format esm --dts --out-dir dist/esm && tsup src/index.ts --format cjs --out-dir dist/cjs","test":"vitest run","test:watch":"vitest","test:coverage":"vitest run --coverage","lint":"eslint src __tests__","typecheck":"tsc --noEmit"},"devDependencies":{"@types/node":"^20.0.0","eslint":"^8.0.0","@typescript-eslint/parser":"^7.0.0","@typescript-eslint/eslint-plugin":"^7.0.0","tsup":"^8.0.0","typescript":"^5.4.0","vitest":"^1.6.0"},"keywords":["state-machine","fsm","finite-state-machine","typescript","zero-dependency"],"publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/AloysiusProductions/ap-state-machine.git"},"homepage":"https://github.com/AloysiusProductions/ap-state-machine#readme","bugs":{"url":"https://github.com/AloysiusProductions/ap-state-machine/issues"},"gitHead":"194572c3c2173e4117c5e2ffc4ae332a60a2790b","_id":"@ap-ent/state-machine@1.0.0","_nodeVersion":"25.8.0","_npmVersion":"11.7.0","dist":{"integrity":"sha512-q9IU2nr+QEYiKapM9vEZ4rL7PUXBcs8rVsJF78I0D5/1Mi21Xa19hfnxnkb+xd3pDRRFBJcBbzd7rgRQjGDZEw==","shasum":"37bc713b8956de829c0b4bab0e1c9ec8167a2a1e","tarball":"https://registry.npmjs.org/@ap-ent/state-machine/-/state-machine-1.0.0.tgz","fileCount":6,"unpackedSize":11428,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCZILi98WgoNDGjI4qdBmJ7OEA8o+qb+PIPAweM1EBNAAIgWLYlkKDPsV079NunhJCHwStd/zPatuIRYl+5HEw8c3Y="}]},"_npmUser":{"name":"irishrocker1125","email":"irishrocker1125@gmail.com"},"directories":{},"maintainers":[{"name":"irishrocker1125","email":"irishrocker1125@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/state-machine_1.0.0_1775331582971_0.9770942609028599"},"_hasShrinkwrap":false}},"time":{"created":"2026-04-04T19:39:42.851Z","1.0.0":"2026-04-04T19:39:43.170Z","modified":"2026-04-04T19:39:43.443Z"},"maintainers":[{"name":"irishrocker1125","email":"irishrocker1125@gmail.com"}],"description":"Zero-dependency typed finite state machine for TypeScript","homepage":"https://github.com/AloysiusProductions/ap-state-machine#readme","keywords":["state-machine","fsm","finite-state-machine","typescript","zero-dependency"],"repository":{"type":"git","url":"git+https://github.com/AloysiusProductions/ap-state-machine.git"},"author":{"name":"Gordon Schauer"},"bugs":{"url":"https://github.com/AloysiusProductions/ap-state-machine/issues"},"license":"MIT","readme":"# @ap/state-machine\n\n> Zero-dependency typed finite state machine for TypeScript.\n\n## Features\n\n- ✅ Fully typed states and events via TypeScript generics\n- ✅ Guard functions — conditionally block transitions\n- ✅ Lifecycle hooks: `onEntry`, `onExit`, transition `action`\n- ✅ Shallow history with `goBack()`\n- ✅ Reactive subscriptions: `subscribe(fn)` returns unsubscribe\n- ✅ Serialization: `toJSON()` / `fromJSON()`\n- ✅ Dual ESM + CJS build\n- ✅ Zero runtime dependencies\n\n## Install\n\n```bash\nnpm install @ap/state-machine\n```\n\n## Quick Start\n\n```typescript\nimport { createMachine } from '@ap/state-machine'\n\ntype State = 'IDLE' | 'LOADING' | 'DONE' | 'ERROR'\ntype Event = 'FETCH' | 'RESOLVE' | 'REJECT' | 'RESET'\n\nconst machine = createMachine<State, Event>({\n  initial: 'IDLE',\n  states: {\n    IDLE: { on: { FETCH: { target: 'LOADING' } } },\n    LOADING: {\n      on: {\n        RESOLVE: { target: 'DONE' },\n        REJECT: { target: 'ERROR' },\n      },\n    },\n    DONE: { on: { RESET: { target: 'IDLE' } } },\n    ERROR: { on: { RESET: { target: 'IDLE' } } },\n  },\n})\n\nmachine.subscribe((state) => {\n  console.log('State changed to:', state)\n})\n\nmachine.send('FETCH')   // → 'LOADING'\nmachine.send('RESOLVE') // → 'DONE'\nmachine.send('RESET')   // → 'IDLE'\n```\n\n## Traffic-Light Example\n\n```typescript\ntype Light = 'RED' | 'GREEN' | 'YELLOW'\ntype Tick = 'TICK'\n\nconst traffic = createMachine<Light, Tick>({\n  initial: 'RED',\n  history: true,\n  states: {\n    RED:    { on: { TICK: { target: 'GREEN'  } }, onEntry: () => console.log('STOP')  },\n    GREEN:  { on: { TICK: { target: 'YELLOW' } }, onEntry: () => console.log('GO')    },\n    YELLOW: { on: { TICK: { target: 'RED'    } }, onEntry: () => console.log('SLOW')  },\n  },\n})\n\ntraffic.send('TICK') // GO\ntraffic.send('TICK') // SLOW\ntraffic.goBack()     // back to GREEN\n```\n\n## API\n\n### `createMachine(config, initialContext?)`\n\nCreates a new machine instance. Config is immutable after creation.\n\n| Option | Type | Description |\n|---|---|---|\n| `initial` | `TState` | Starting state |\n| `states` | Record | Per-state config (transitions, hooks) |\n| `history` | boolean | Enable shallow history stack |\n\n### `machine.send(event)`\n\nTriggers a transition. Returns the new state (unchanged if blocked).\n\n### `machine.subscribe(fn)`\n\nRegisters a listener called on every state change. Returns an unsubscribe function.\n\n### `machine.goBack()`\n\nPops the history stack and restores the previous state. Returns `undefined` if unavailable.\n\n### `machine.toJSON() / fromJSON(snapshot)`\n\nSerialize and restore machine state including history stack.\n\n## Guards\n\n```typescript\nconst vault = createMachine<'LOCKED' | 'OPEN', 'UNLOCK'>(\n  {\n    initial: 'LOCKED',\n    states: {\n      LOCKED: {\n        on: {\n          UNLOCK: {\n            target: 'OPEN',\n            guard: (ctx) => ctx['pin'] === 9876,\n          },\n        },\n      },\n      OPEN: {},\n    },\n  },\n  { pin: 9876 },\n)\n```\n\n## License\n\nMIT © Gordon Schauer\n","readmeFilename":"README.md","_rev":"1-35eace9c8b629b34dbd05f2c6c72ae00"}