https://github.com/ovaar/ts-fence
The descriptive statemachine for typescript
https://github.com/ovaar/ts-fence
javascript statemachine typescript
Last synced: about 2 months ago
JSON representation
The descriptive statemachine for typescript
- Host: GitHub
- URL: https://github.com/ovaar/ts-fence
- Owner: ovaar
- License: mit
- Created: 2018-06-01T15:30:11.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-12-23T14:37:03.000Z (over 6 years ago)
- Last Synced: 2025-12-07T09:40:54.097Z (7 months ago)
- Topics: javascript, statemachine, typescript
- Language: TypeScript
- Homepage: https://github.com/ovaar/ts-fence
- Size: 208 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/ovaar/ts-fence)
[](https://coveralls.io/github/ovaar/ts-fence?branch=master)
[](https://github.com/semantic-release/semantic-release)
[](https://www.npmjs.com/package/ts-fence 'View this project on npm')
[](https://paypal.me/ThomasReynders)
> The descriptive statemachine for typescript
```sh
$ npm i -D ts-fence
```
```typescript
import { StateMachine, StateTransition, IStateMachineDescription } from 'ts-fence'
interface IPlayer {
health: number
animate(name: string): void
reset(): void
}
interface GameStateMachineDescription extends IStateMachineDescription {
player: IPlayer
}
const player: IPlayer = {
health: 100,
animate(name: string) {},
reset() {}
}
const description: GameStateMachineDescription = {
player,
[StateMachine.STARTING_STATE]: 'idle',
[StateMachine.STATES]: {
// describe states here
idle: {
// describe state triggers and actions here
hit(
{ scope, stateMachine }: { scope: GameStateMachineDescription; stateMachine: StateMachine },
damage: number
) {
scope.player.health = scope.player.health - damage < 0 ? 0 : scope.player.health - damage
if (scope.player.health === 0) {
stateMachine.die()
}
},
die: new StateTransition('game-over', (): any => undefined)
},
'game-over': {
[StateMachine.ON_ENTER]: ({ scope }: { scope: GameStateMachineDescription }) => {
scope.player.animate('death')
},
[StateMachine.ON_EXIT]: ({ scope }: { scope: GameStateMachineDescription }) => {
scope.player.reset()
}
}
}
}
```