State Machines for javascript