Observer pattern 00:00:00 Make a prediction You will show how subscription and unsubscription affect notifications and identify a delivery guarantee that the observer pattern leaves open. Two displays subscribe to a station. One closes, then a new reading arrives. If the station still holds its listener, how many callbacks will it try? Two. Closing the window does not automatically remove its subscription. Return an unsubscribe mechanism and demonstrate that the closed display stops receiving callbacks. 00:00:33 The hen and the strings A hen's nesting box has strings tied to three bells. An egg tugs each string. Lee removes one string after the first egg; the next egg should ring two bells. An abandoned string can still ring an empty house. A string is a live subscription. Removing a listener is part of the lifecycle, not a side effect you can infer from the subscriber's user interface. Observer is a behavioral design pattern for a one-to-many dependency. One object, the subject, has state or produces events. Others, the observers, register with it and are called when something happens. When one part changes its state, several others often need to react. If the part calls each of them directly, it depends on all of them, must be edited for each new one, and can not be tested alone. 00:01:26 Remove a subscriber cleanly Two displays subscribe to the same weather station. A new reading should update both without the station knowing their screen types. Keep a subscription token or explicit unsubscribe operation. Close the kitchen display and remove its listener. Publish another reading. Only the remaining display should update. If the old display remains reachable through the subscriber list, closing its window may still leak memory. Dispatch one score update to a snapshot containing A and B. A removes itself during its callback. Under snapshot semantics B still receives this update and only B receives the next. Mutating the live list while iterating can skip B, an unrelated subscriber. 00:02:12 Separate publication from delivery policy A store emits an order event to accounting and email. If the email handler is slow or fails, decide whether accounting waits or the publisher returns earlier. The observer relationship identifies who is interested; ordering, retries and isolation need separate explicit decisions. Let subscriber A succeed while subscriber B throws. Decide whether B retries, the publisher reports an error, or failures are isolated. Replaying the whole event can notify A twice. The observer relationship tells you who is interested; an explicit delivery policy supplies these additional guarantees. If one subscriber throws and the publisher stops iterating, the remaining subscribers may never see the event. Decide and test failure isolation. For independent durable delivery and retries across processes, use a message system with explicit delivery semantics rather than relying on an in-process observer list. DOM events. addEventListener registers a function on an element. The DOM standard goes through a clone of the list, skips listeners that were removed meanwhile, and reports an exception without stopping the others: the rules of our round. Node EventEmitter. Listeners are called synchronously in the order of registration. An error event without a listener is thrown and ends the process, and too many listeners for one event cause a warning about a possible leak. 00:03:43 Try an interview follow-up How do you make it safe for threads? Protect the list with copy on write: writers replace it under a lock, a round reads one reference. Never call observers under a lock, or two locks can wait for each other. State openly that a detached observer may get one last call from a round on another thread, or wait for running rounds. If order matters across threads, one delivering thread. A scoreboard notifies two listeners. The first unsubscribes itself during the update. Decide whether the second still receives this update, and whether the first receives the next one. A snapshot of listeners at dispatch time gives one clear answer. State delivery order only if the contract requires it. 00:04:31 What to take with you One subject, many observers, one small interface. Store the new state, go through a copy of the list, skip who left, catch and report errors. Push an immutable message unless observers need very different parts. A subscription is a resource: attach returns a handle, an owner closes it. If observers may publish, queue events so that all see one order.