Introduce on_state_change for building persistent states