jakesgordon / javascript-state-machine

A javascript finite state machine library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ES6 Classes Support

gtg092x opened this issue · comments

Fantastic library!

I was interested in using this with an es6 class of mine like so:

class Matter extends StateMachine.es6 {
  static initialState = 'solid'
  static stateTransitions = [
    { name: 'melt',     from: 'solid',  to: 'liquid' },
    { name: 'freeze',   from: 'liquid', to: 'solid'  },
    { name: 'vaporize', from: 'liquid', to: 'gas'    },
    { name: 'condense', from: 'gas',    to: 'liquid' }
  ]
}

I went ahead and created a PR that supports this.

It adds a function that can be extended like above and adds additional functionality around setting the state property directly.

class Matter extends StateMachine.es6 {
  static initialState = 'solid'
  static stateTransitions = [
    { name: 'melt',     from: 'solid',  to: 'liquid' },
    { name: 'freeze',   from: 'liquid', to: 'solid'  }
  ]
  onFreeze() {
    console.log('It\'s cold!');
  }
}

const h20 = new Matter();
h20.state = 'solid';
// log - It's cold!