Runtime · Observation

Observer lifecycle

Register observers, deliver changes, and release resources predictably.

Overview

An observer receives a snapshot whenever the selected resource changes. Registration returns a disposable handle. Keep that handle near the code that owns the subscription so teardown remains explicit.

Observers begin after the current transaction commits. They never inspect an incomplete update, and each callback receives the revision that caused its notification.

Note: Delivery is ordered within one resource. Observers attached to different resources can run independently.

Creating an observer

Pass the resource key and a callback to observe(). The callback should complete quickly. Move expensive work to the queue when it does not need to block later notifications.

const subscription = registry.observe("deployment", snapshot => {
  renderStatus(snapshot.state);
});

window.addEventListener("pagehide", () => subscription.dispose());

Handling changes

Compare the supplied revision when a consumer can receive work from another source. Ignore an older revision instead of allowing delayed work to overwrite a newer result.

A callback can request its current snapshot again, but it should not assume that the registry remains unchanged after the callback returns. Treat every delivered value as an immutable record.

Releasing the subscription

Call dispose() when the owning component leaves the page. Disposal is idempotent. A callback that is already running may finish, but the registry will not schedule another callback for that observer.