Notification Center

← Notifications
STRVCT

Asynchronous event dispatching, dual WeakMap observation indexing, and decoupled change propagation in SvNotificationCenter.

Overview

In a framework where model, view, and storage layers must stay independent, objects announce state changes without holding direct references to observers. Direct method calls create tight coupling—the sender must hold a reference to every observer, and adding a new listener requires modifying sender code.

SvNotificationCenter solves this: senders post named notifications into a shared center, observers register interest by name and sender, and the two sides never reference each other directly.

Sender Objects (SvNode) this.postNoteNamed("didChange") newNote().setInfo({...}).post() SvNotificationCenter _senderToObservations WeakMap index for O(1) lookup _nameToObservations WeakMap index by name Observer Objects (SvNodeView) View A: didChange(note) View B: customHandler(note) WeakRef observer storage prevents retain cycles; unmounted views are reclaimed automatically without manual unwatching.

Posting Notifications

Senders post notifications when their state mutates:

// Convenience method on SvNode:
this.postNoteNamed("didUpdateNode");

// Advanced notification with info dictionary:
SvNotificationCenter.shared().newNote()
    .setSender(this)
    .setName("didUpdateNode")
    .setInfo({ fieldName: "title", oldValue: "Draft", newValue: "Final" })
    .post();

Observing Notifications & Convention Handlers

Objects register observations using newObservation():

// Register observation
SvNotificationCenter.shared().newObservation()
    .setSender(targetNode)
    .setName("didUpdateNode")
    .setObserver(this)
    .startWatching();

Handler Method Resolution:

  • Default Convention: When targetNode posts "didUpdateNode", the notification center automatically calls this.didUpdateNode(note) on the observer if defined.
  • Explicit Override: To direct dispatch to a different method name, call setSendName():
obs.setSendName("handleNodeUpdate");

Dual WeakMap Indexing & Memory Safety

To minimize dispatch overhead as domain graphs grow, SvNotificationCenter maintains two internal index maps:

  1. _senderToObservations: A WeakMap mapping senders to their registered observations.
  2. _nameToObservations: A WeakMap mapping notification names to observations.

During a post operation, the notification center intersects the two maps to retrieve target observations in $O(\text{observations on sender})$ time, avoiding full-table scans.

Memory Leak Protection:

Observers are held in WeakRef wrappers. When an observing view is removed from the DOM and has no remaining strong references, JavaScript's garbage collector automatically reclaims it. Dead observations are cleaned up during dispatch passes without throwing errors.