A simple event-driven state machine