Minimal state machine implementation in JS.