Modular and Safe Event-driven Programming