A tiny JavaScript state machine