A simple finite state machine implementation in javascript