Installation
Get started with @jewel998/state-machine by installing it in your project.
Package Manager Installation
Choose your preferred package manager:
- npm
- pnpm
- yarn
npm install @jewel998/state-machine
pnpm add @jewel998/state-machine
yarn add @jewel998/state-machine
Requirements
- Node.js: >= 16.0.0
- TypeScript: >= 4.3.5 (if using TypeScript)
Import Methods
- ES Module
- CommonJS
- TypeScript
import { StateMachine } from '@jewel998/state-machine';
const { StateMachine } = require('@jewel998/state-machine');
import {
StateMachine,
IStateMachineDefinition,
InvalidTransitionError,
GuardConditionError,
} from '@jewel998/state-machine';
Bundle Information
The library is optimized for modern bundlers:
- Bundle Size: ~45KB minified
- Tree Shaking: Fully supported
- Side Effects: None
- Dependencies: Zero runtime dependencies
Browser Support
The library supports all modern browsers:
- Chrome >= 60
- Firefox >= 60
- Safari >= 12
- Edge >= 79
For older browser support, you may need to include polyfills for:
MapandSetObject.assignArray.from
CDN Usage
For quick prototyping, you can use the library via CDN:
<script type="module">
import { StateMachine } from 'https://unpkg.com/@jewel998/state-machine/index.mjs';
// Your code here
</script>
Verification
Verify your installation by creating a simple state machine:
import { StateMachine } from '@jewel998/state-machine';
// Create a stateless definition
const definition = StateMachine.definitionBuilder()
.initialState('idle')
.state('idle')
.state('active')
.transition('idle', 'active', 'start')
.buildDefinition();
// Create a simple wrapper to manage state
class SimpleStateMachine {
constructor(definition) {
this.definition = definition;
this.currentState = definition.getInitialState();
}
processEvent(event) {
const result = this.definition.processEvent(this.currentState, event, {});
if (result.success) {
this.currentState = result.newState;
return true;
}
return false;
}
getCurrentState() {
return this.currentState;
}
}
// Test the state machine
const machine = new SimpleStateMachine(definition);
console.log(machine.getCurrentState()); // 'idle'
machine.processEvent('start');
console.log(machine.getCurrentState()); // 'active'
console.log('✅ Installation successful!');
Next Steps
Now that you have the library installed, continue to the Quick Start guide to learn the basics.