Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including ts-fence with all npm packages installed. Try it out:

var tsFence = require("ts-fence")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ts-fence v1.2.1

typescript statemachine

Build Status Coverage Status semantic-release npm version Donate

The descriptive statemachine for typescript

$ npm i -D ts-fence
import { StateMachine, StateTransition, IStateMachineDescription } from 'ts-fence'

interface IPlayer {
  health: number
}

interface PlayerStateMachineDescription extends IStateMachineDescription {
  player: IPlayer
}

const player: IPlayer = {
  health: 100
}

const _description: PlayerStateMachineDescription = {
  player,
  [StateMachine.STARTING_STATE]: 'idle',
  [StateMachine.STATES]: {
    // describe states here
    idle: {
      [StateMachine.ON_ENTER]: () => {},
      // describe state triggers and actions here
      stabbed({ scope, stateMachine }: { scope: PlayerStateMachineDescription, 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),
      [StateMachine.ON_EXIT]: () => {},
    },
    'game-over': {
      ...
    }
  }
}

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free