Replace a tracker that calls every screen by name with an order that announces status changes to listeners, and make the announcing safe: unsubscribe, snapshot, failures.
A delivery app tells three screens when an order's status changes, and the code in the editor does it by calling each screen by name. It works. It just can't grow: every new recipient edits the tracker, and one screen that fails stops the ones after it. Refactor it into the Observer pattern, and make the announcing safe.
Build this API (the StatusEvent class is given):
Order(order_id) with status (starts as "placed") and order_id.order.subscribe(name, listener) adds a listener (any function taking one StatusEvent) and returns a subscription with cancel(). cancel() may be called twice, and from inside a notification.order.set_status(new_status) builds StatusEvent(order_id, old_status, new_status), sets the status first (so a listener that reads order.status sees the new one), then calls every listener, and returns the list of failures as "name: message" strings (an empty list if none).order.subscribers() returns the names currently subscribed, in order.JavaScript and TypeScript use camelCase: setStatus(newStatus), orderId on the order, and the event fields orderId, oldStatus and newStatus. A listener that throws an Error is reported as "name: " + error.message.
The tests check the event, the order, unsubscribing, failures, edits during a round, a new listener written inside a test and plugged in, and that no recipient is called by name any more.
Write your code, then run 9 example tests and 5 hidden tests. Each run is limited to 8 seconds: an infinite loop gets stopped and the runtime restarts, your code stays.