Skip to content

Add GraphMutationObserver to store-graph - #6096

Open
William Wong (compulim) with Copilot wants to merge 2 commits into
mainfrom
copilot/add-graph-mutation-observer
Open

William Wong (compulim) with Copilot wants to merge 2 commits into
mainfrom
copilot/add-graph-mutation-observer

Conversation

Copilot AI commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Adds a GraphMutationObserver to store-graph, modelled after the HTML MutationObserver. It observes graph changes synchronously and can filter them by a list of @id or alternate identifier values. takeRecords() is not included.

const observer = new GraphMutationObserver((affectedNodeIds, observer) => {
  // Contains only the entries from identifierList that were affected.
});

observer.observe(graph, { identifierList: new Set(['_:m-1', 'urn:x-message:1']) });
observer.observe(otherGraph); // no filter: receives every affected ID

observer.disconnect();

Graph (Graph.ts)

  • Adds a private #observers set. A static {} block sets the module-level observeGraph(graph, observer) => unobserve, so the observer can reach it without a public API. It is exported from Graph.ts only, not from index.ts.
  • #notifyAll() calls subscribe() listeners first, then observers, all in the same synchronous job.
  • Observers added or removed during a notification are skipped for that notification. The code iterates over a snapshot of the set and checks has() before each call.
  • The try/catch that re-throws listener errors on a microtask moves into a shared callListener() helper, used by both listeners and observers.

GraphMutationObserver (GraphMutationObserver.ts)

  • Filtering: identifierList is intersected with the graph's affected-ID set, which already includes alternate identifiers from before and after the change. So @id and identifier matching both work, including when an identifier is added or removed. If nothing in the list was affected, the callback is not called.
  • Behaviour copied from MutationObserver:
    • The callback receives the observer as its second argument.
    • One observer can watch several graphs.
    • Calling observe() again on the same graph replaces its options.
    • identifierList is copied when observe() is called.
  • Input validation: the callback must be a function, otherwise the constructor throws TypeError. observe() uses valibot to check that graph is a Graph and that identifierList is a Set of valid identifiers.
  • The IDs passed to the callback are read-only (FrozenSet).
  • Exported from index.ts together with the GraphMutationCallback and GraphMutationObserverInit types.

Review focus

  • When identifierList is not set, the callback gets the same set as subscribe() listeners. That set includes alternate identifiers, e.g. ['_:m-1', 'urn:x-message:1'].
  • Entries in identifierList must be valid identifiers, so arbitrary strings are rejected rather than silently never matching.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An observer registered from a subscribe() listener can incorrectly receive a notification for a commit that preceded its registration, violating the promised skip rule.

1 open issue
What changed in this PR

Adds synchronous, identifier-filtered graph observation to store-graph, modeled after MutationObserver.

Changes:

  • Introduces validated observer registration, filtering, and disconnection.
  • Indexes observations by identifier and shares listener error handling.
  • Adds behavioral tests and an observer-scaling benchmark.
File Description
packages/​store-graph/​src/​private/​ObservationIndex.ts Indexes and dispatches matching observations.
packages/​store-graph/​src/​private/​callListener.ts Shares callback error isolation.
packages/​store-graph/​src/​index.ts Exports the observer and its types.
packages/​store-graph/​src/​GraphMutationObserver.ts Implements observer lifecycle and validation.
packages/​store-graph/​src/​GraphMutationObserver.test.ts Tests filtering, lifecycle, and notification behavior.
packages/​store-graph/​src/​Graph.ts Integrates observer dispatch after subscription listeners.
packages/​store-graph/​src/​benchmark.ts Measures updates with per-node observers.

Review effort: Balanced

callListener(listener, affectedNodeIds);
}

this.#observationIndex.notify(affectedNodeIds);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants