Finite state machine design pattern in javascript