Reconciler¶
Diffs successive virtual trees and applies the smallest set of native
mutations that bring the on-screen view tree in line with the new
description. The reconciler is platform-agnostic; it talks to native
widgets exclusively through the
NativeViewRegistry.
Virtual-tree reconciler.
Maintains a tree of VNode objects
(each wrapping a native view) and diffs incoming
Element trees to apply the minimal set of
native mutations.
Supports:
- Native elements (
typeis a string like"Text"). - Function components (
typeis a callable decorated withcomponent). Their hook state is preserved across renders. - Provider elements (
type == "__Provider__"), which push and pop context values during tree traversal. - Error boundary elements (
type == "__ErrorBoundary__"), which catch exceptions in child subtrees and render a fallback. - Key-based child reconciliation for stable identity across re-renders.
- Post-render effect flushing. After each mount or reconcile pass, all queued effects are executed so they see the committed native tree.
- Layout pass: after every commit, a parallel
LayoutNodetree is built from the committed VNodes and fed throughcalculate_layout; the resulting per-node frames are applied via the backend'sset_frame. The viewport size is supplied by the screen host viaset_viewport_size.
Classes:
| Name | Description |
|---|---|
VNode |
A mounted |
Reconciler |
Create, diff, and patch native view trees from |
VNode
¶
A mounted Element plus its native view.
The reconciler walks parallel trees of VNode and incoming
Element to compute the minimal set of native mutations.
Attributes:
| Name | Type | Description |
|---|---|---|
element |
The |
|
native_view |
The platform-native view (e.g., an Android |
|
children |
Ordered list of child |
|
parent |
Optional[VNode]
|
The owning |
hook_state |
Any
|
The component's
|
mounted |
bool
|
|
Reconciler
¶
Reconciler(backend: Any)
Create, diff, and patch native view trees from Element descriptors.
After each mount or
reconcile call the
reconciler walks the committed tree and flushes all pending effects
so effect callbacks run after native mutations are applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Any
|
An object implementing the native-view protocol
( |
required |
Methods:
| Name | Description |
|---|---|
mount |
Build native views from |
reconcile |
Diff |
root_view |
Return the current root native view, or |
mark_dirty |
Queue |
flush_dirty |
Re-render only the component subtrees marked dirty since the last pass. |
set_viewport_size |
Update the viewport size and re-run layout if it changed. |
compute_layout_for_test |
Build and compute a layout tree without touching the backend. |
mount
¶
reconcile
¶
mark_dirty
¶
mark_dirty(vnode: VNode) -> None
Queue vnode (a function component) for a local re-render.
Called by a component's use_state / use_reducer setter
when its own state changes. The node is re-rendered on the next
flush_dirty
pass, which the screen host schedules. Marking is idempotent and
cheap; the actual render is deferred so several setters (e.g.
inside batch_updates) coalesce
into a single pass.
flush_dirty
¶
flush_dirty() -> Any
Re-render only the component subtrees marked dirty since the last pass.
This is the hot path for state-driven updates: instead of
re-running the whole app from the root, each dirty function
component re-runs its own body (reusing its
HookState) and reconciles just
its subtree. Nodes are processed shallowest-first so that when a
dirty ancestor's re-render already covers a dirty descendant, the
descendant is skipped (its _dirty flag is cleared by the
ancestor pass).
Returns:
| Type | Description |
|---|---|
Any
|
The (possibly replaced) root native view, so the host can |
Any
|
re-attach it if the root changed. |
set_viewport_size
¶
Update the viewport size and re-run layout if it changed.
Called by the screen host whenever the platform reports a new
container size (Android: onLayoutChange; iOS:
viewDidLayoutSubviews). The first call after mount
triggers the initial layout pass; subsequent identical
sizes are no-ops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
float
|
Viewport width in points. |
required |
height
|
float
|
Viewport height in points. |
required |
compute_layout_for_test
¶
compute_layout_for_test(viewport_width: float, viewport_height: float) -> Optional[LayoutNode]
Build and compute a layout tree without touching the backend.
Test helper that returns the synthetic viewport LayoutNode
with all descendants positioned. Returns None if no tree
has been mounted yet.
Next steps¶
- Read the high-level walkthrough in Reconciliation.
- See how native widgets are registered in Native views.