Model listeners¶
A concept can declare a listeners() aspect: handlers that run after a committed edit changed a node of that
concept (a child added or removed, a primitive property changed, a reference name changed). This is the analogue of
MPS-extensions' mpsutil.modellisteners. Source: src/model-listeners.ts (plain TS, no Solid, no Editor import),
wired into Editor.fire() in src/editor/lifecycle.ts.
API¶
class Release extends BaseConcept {
listeners() {
return {
onChildAdded(ev, ctx) { /* ev.node = this Release, ev.child, ev.index, ev.slot */ },
onChildRemoved(ev, ctx) { /* ev.child is the detached node */ },
onProperty(ev, ctx) { /* ev.index, ev.slot, ev.oldValue, ev.newValue */ },
onReference(ev, ctx) { /* same payload, only for IReference concepts */ },
};
}
}
| Event | Fires when | Payload (besides type, node) |
|---|---|---|
onChildAdded |
a node child appears in node.children (by id) |
child, index, slot |
onChildRemoved |
a node child disappears from node.children |
child (the detached object), index (old), slot |
onProperty |
a primitive child at the same index changed value | index, slot, oldValue, newValue |
onReference |
like onProperty, on an IReference concept (fires in addition to onProperty) |
same |
ctx is { lang, root, editor, pass }. event.node is always the owner whose concept declared the handler, so a
handler is "about my own children". A handler may mutate the tree (any node it can reach) and call the editor's
public API.
Not built: onRootAdded / onRootRemoved. The Editor shows one root at a time and roots live in the Project, not in
fire(); they need a project-level hook.
How events are derived¶
No write path emits anything. The Editor keeps a ModelListenerRunner holding a snapshot (head plus a copy of
children) of every node whose head declares a listener. fire(), the one commit point every edit funnels through,
takes a fresh snapshot and diffs the two by node id. A node that exists only in the new snapshot is new and reports
through its (existing) parent, never for its own children. A move between parents is a remove plus an add; reordering
inside one parent is silent.
Language.listenerHeads() (memoised with the other flag sets) is the fast path: with no listening concept in any
loaded language, fire() costs one Set.size read and no runner is created.
Guardrails¶
- Batched, after the edit. Handlers run once per committed edit inside
fire(), after the tree is final. Insidetransaction()they are deferred to the closingfire()and see the whole transaction as one diff. - One undo step. Listener writes land before
_pushHistory()/onChange(), so the edit and everything the listeners wrote are one history entry (and, in a Live Model Session, one Yjs transaction, hence oneY.UndoManagerstep). Undo and redo restore a snapshot that already contains the writes and never re-run listeners. - Re-entrancy guard. While handlers run,
runner.runningis true and anyfire()they cause (directly, or through the editor's public API) returns early: no nested dispatch, no extra history entry; the outer pass picks the writes up. - Cascades settle or are cut off. After a pass of handlers the tree is re-normalized and re-diffed; writes that
trigger further events run in the next pass (
ctx.pass1, 2, ...), at mostMAX_LISTENER_PASSES(8). A handler that keeps rewriting its own trigger is stopped and reported once throughreportError("did not settle"). The writes made so far stay; one Undo reverts the edit together with them. - Errors. A throwing handler is reported via
reportError(console when no document state) and the batch continues. - Slow listeners. A handler call above
SLOW_LISTENER_MS(50 ms) is warned about once perhead.eventwithconsole.warn. A listener on a widely inherited base concept fires for every subconcept's edits, so keep those cheap.
Remote (collab) changes: listeners do not run¶
Decision: a change applied from a remote collaborator does not dispatch listeners on the receiving side. The peer
that made the edit already ran its listeners, and their writes travel in the same Yjs transaction. Running them again
on every peer would duplicate each write and echo it back through Yjs. So resyncFromExternalMutation(), setRoot(),
adoptRoot(), setLanguage(), undo/redo restore and the constructor all re-baseline the snapshot instead of
dispatching (_rebaselineListeners()), so the next local edit is diffed against the current tree and is not blamed for
the remote change.
Consequences for authors: handlers must be idempotent. Two peers editing concurrently may each run a listener on their own edit, and Yjs merges both results. Writes a listener makes on one peer's edit are not "owned" by anyone else.
Inheritance and persistence¶
conceptMetaFromClass harvests listeners() into RuntimeConceptData.listeners, a table of arrays per event. With a
language-level extends, mergeInheritance concatenates parent then child handlers per event, so a listener on an
abstract base concept keeps firing for subconcepts. A concept class that extends another class overrides listeners()
like any method (call super.listeners() to keep the base's). Handlers must not use this: they are harvested from a
throwaway probe instance, like computed().
Listeners are code-only, like linkTargets and referenceScopes: they are not written to .lang.js / .lang.kdl,
and applyJsOverlays re-attaches them to a persisted copy (a field missing there is dead in the real app, see the
persisted-copy bug class). They do not appear in an aspect tab of the Language Editor; they sit under "Other".
Demo and tests¶
plugins/lang-changelog Release: a blank change inserted after a "Fixed" entry adopts "Fixed" (onChildAdded).
Tests: tests/test-model-listeners.mjs (diff, batching, undo folding, re-entrancy, pass cap, slow warning, remote
patches, zero cost, overlays), tests/test-model-listeners-collab.mjs (a real Editor under a hosted session),
tests/test-ui-model-listeners.spec.js (real browser, including a second boot with the persisted language copy).
Not covered¶
- Edits that bypass the Editor (the Model Console /
model-api.ts, generators writing models) do not run listeners. - The snapshot walk is O(nodes) per
fire()and pass when any listener exists; fine for editor-sized models. - A language swapped onto an Editor via
setLanguage()re-baselines; the very first edit after a listener-less language gained listeners some other way is baseline-only.