Recursive AI

Change sets

All oneui docs

The seam between components and whatever shows them. A UiContext collects the mutations that components make and hands them to an IChangeSink as one ChangeSet per batch; the sink is a document: the reference document in this package, and the native and browser backends in RecursiveAI.OneApp. Version ChangeSetProtocol.Version = 2 (see Version 2).

Nodes and ids

Ids are integers allocated by the context, starting at 2; id ChangeSetProtocol.RootNode (1) is the container the sink owns, and top-level components are inserted into it. A node is created detached (CreateElement, CreateText) before anything else mentions its id. Ids are never reused, so a sink can keep a plain array or dictionary indexed by id, and a bridge can send ids over a boundary without tracking lifetimes.

Changes

Change(Kind, Node, Other, Before, Name, Value):

KindFields
CreateElementNode new id, Name lower-case tag
CreateTextNode new id, Value data
InsertNode parent, Other detached child, Before sibling or 0 to append
Moveas Insert, for a node already in the tree (identity and listeners kept)
RemoveNode; the subtree is destroyed, its listeners with it
SetAttribute / RemoveAttributeNode, Name, Value. class replaces the whole class list
SetTextNode, Value
AddClass / RemoveClassNode, Name
SetStyle / RemoveStyleNode, Name property (or --custom), Value
SetPropertyNode, Name (value, checked), Value as text ("true"/"false" for checked)
AddListener / RemoveListenerNode, Name DOM event type
AddStyleSheetName key (added once), Value CSS text for the whole document
CreateElementNS (2)Node new id, Name local name with its case kept, Value namespace URI
Focus (2)Node the element to focus

A ChangeSet carries Version, a Sequence (1, 2, 3, … per context) and the ordered changes. The sink applies a set atomically and may throw ChangeSetException for a change that names a missing node, a cycle, or an element where text is expected. Within a set, an Insert of a subtree always comes after the creation of every node in it.

Version 2: SVG elements and focus

Version 2 adds two kinds, CreateElementNS (numeric value 16) and Focus (17), and changes nothing else; every version 1 change keeps its meaning and encoding. A sink that implements version 2 also accepts version 1 sets, and refuses any version above 2.

Namespaces. CreateElement creates an HTML element, as before. CreateElementNS creates a detached element in the namespace Value; the producer uses it for every element inside an <svg> template, with Value = ChangeSetProtocol.SvgNamespace (http://www.w3.org/2000/svg) and Name = the local name exactly as written (svg, path, linearGradient). A DOM backend calls document.createElementNS(Value, Name). The namespace is carried only by this change; there is no prefix in names and no namespaced attribute change. Everything after creation uses the ordinary kinds: SetAttribute / RemoveAttribute with the name as given (viewBox, stroke-width; a DOM backend calls setAttribute(name, value), which keeps the case on an SVG element), AddClass / RemoveClass and SetAttribute("class") (a backend must not assume className is a string on SVG; use classList or setAttribute), SetStyle (element.style), AddListener and Insert into HTML or SVG parents alike. References between SVG parts use href, never xlink:href.

Focus. Focus asks the document to do what the DOM's element.focus() does to Node, once the whole set has been applied (so a node created and inserted in the same set can be focused). If the node is not a focusable area, is disabled, hidden or detached, nothing happens and nothing is reported; there is no failure to handle. A DOM backend calls element.focus(); a native backend follows the HTML focusing steps. Focus moves produce blur, focusout, focus and focusin for the elements involved, which reach the producer through the listeners the producer added (AddListener) like any other event. A backend must deliver events that the browser raises during element.focus() after the set has finished applying, never from inside Apply, because a sink is not re-entered. focus and blur do not bubble: deliver them to the target node only; focusin and focusout bubble, so deliver them to each node on the path that listens. Removing the focused element leaves no element focused and raises no event. There is no Blur change; focus leaves an element when another gets it or when it is removed.

In markup, ref="{Member}" binds an ElementRef to an element and ElementRef.Focus() records the change (UiContext.Focus(node) is the low-level call).

Layout and focus queries, and timers

None of these travel through the change-set protocol, which stays at version 2.

Queries. A sink that can answer questions about the document it holds also implements IDocumentQueries; UiContext.Queries defaults to the sink (assign it to supply them separately, and check HasQueries). Component code asks the context, or an ElementRef:

CallAnswer (CSS pixels, CSSOM View names)
GetBoundingClientRect(node) / ElementRef.GetBoundingClientRect()the border box in viewport coordinates (DomRect: X Y Width Height Left Top Right Bottom), or null for an element with no box (unknown, detached, display: none)
GetViewportSize()DomSize (innerWidth, innerHeight)
GetScrollMetrics(node)ScrollMetrics: ScrollLeft ScrollTop ScrollWidth ScrollHeight ClientWidth ClientHeight, or null
GetTabbableNodes(root)the elements inside root that Tab reaches, in sequential focus order (positive tabindex ascending, then tree order); no negative tabindex, disabled, hidden, display: none or inert elements
FocusFirstTabbable(root)records a Focus for the first of those; false when there is none
GetActiveElement(), Contains(ancestor, node), IsInert(node)document.activeElement (0 for none), Node.contains (inclusive), and whether the element or an ancestor has inert

Rule: every query first commits what has been recorded (recomputing dirty bindings), exactly like a forced synchronous layout, so an element created or moved earlier in the same batch or handler has its box. UiContext.Commit() does the same without asking anything, for code that needs a fresh ElementRef bound before it uses it. Queries throw InvalidOperationException from inside a binding expression or while a sink is applying a commit (including events a sink delivers from inside Apply), and when the backend answers none. A sink implements the interface by answering from its document as of the last commit, running layout first if it is stale; an unknown node id gets the "none" answer, never an exception.

The reference document answers from boxes a test chooses: SetBox(selector, rect), BoxResolver (a function for tests that move things), Viewport, SetScrollMetrics. An element with neither has no box. Focus answers use the same HTML rules as Press/Focus. Scroll offsets are read-only; setting them is not part of the interface yet.

Timers. The host sets UiContext.Scheduler (IUiScheduler.Schedule(delay, callback): run once on the UI thread, return a handle that cancels). UiContext.SetTimeout and SetInterval run their callback as one batch, like an event handler, and return an IDisposable that cancels (also from inside the callback). A component's protected SetTimeout/SetInterval do the same and cancel when the component is removed. Intervals are re-armed when they fire, so a slow callback never queues extra runs. Tests use ManualScheduler: Advance(span) runs what is due, in order, each at its own due time; Now and PendingCount show the state.

Events back

The sink reports events through UiContext.Dispatch(nodeId, DomEvent) for each node on the path that has a listener (the listeners it was told about by AddListener). DomEvent has Type, Target, StopPropagation() and PreventDefault(); the subclass for the type is DomEvents.PayloadOf(type): MouseEvent, KeyboardEvent, InputEvent, FocusEvent, DomEvent. An event with the wrong payload class or no listener is dropped and reported to UiContext.Diagnostics. A dispatch runs as one batch: the state changes its handlers make are one commit.

Batches

Outside a batch every state change commits immediately. UiContext.Batch(action) (and every Dispatch) defers: the dirty bindings run once when the outermost batch ends, and the result is one ChangeSet. Lifecycle callbacks (OnMounted) run after the commit.

For sinks

  • The reference document (OneUI.Components.Reference.ReferenceDocument) is the model: it validates, keeps the commits, answers selector queries (Query, QueryAll, Text, ToHtml) and drives events for tests (Click, Input, Press, Focus, Blur, Dispatch). It also follows the HTML rules for focus and default actions, so component tests need no helpers:
    • ActiveElement and IsFocused(selector) mirror document.activeElement; ReferenceDocument.IsFocusable(node) is the focusable-area test (controls, links with href, contenteditable, any integer tabindex; not disabled, hidden, inert or display: none inline).
    • Focus(selector) and Blur(selector) move focus and raise blur, focusout, focus, focusin (with RelatedTarget) to listeners.
    • Click(selector) focuses the nearest focusable ancestor-or-self, then clicks. A checkbox toggles and a radio button selects (unless a handler called PreventDefault); a click on a label (or inside one, outside other interactive content) focuses its control (for or first labelable descendant) and clicks it; disabled controls ignore clicks.
    • Press(key) presses on the focused element, Press(selector, key) on a chosen one. Unless keydown is prevented: Tab moves focus along the tab order (positive tabindex first, then document order, Shift+Tab backwards, wrapping, tabindex="-1" skipped); Enter clicks a button, button-type input or link with href; Space (" ") clicks a button, button-type input, checkbox or radio button when released.
    • Elements are DomNodes with Namespace and IsSvg; selectors match type names case-sensitively on SVG elements.
  • A backend that keeps its own tree needs (version 2) CreateElementNS and Focus, and Insert/Move with a Before sibling, Remove of a subtree, and per-node attribute, class, inline-style, text and property setters. Nothing else is required to apply commits; queries are a separate, optional interface (below).
  • Moves are minimal: a keyed list update moves only the rows outside the longest run that is already in order.