Yet another Javascript State Machine