Goal: connect clicks, reservations, DOM changes, and painting
Extend the earlier Rust click handler with deferred work. A click schedules a timer, the timer changes DOM text, and a microtask changes it again. Investigate where the intermediate value one exists and which value is adopted at a rendering opportunity, rather than checking only that the final text becomes two.
The Rust document is distinct from the host page’s DOM. Its getElementById resolves within that document, while the player displays returned state. Treating the program as direct host DOM manipulation would obscure replay and native/WASM comparison. Follow event dispatch, scheduling, evaluation, mutation, layout, and drawing inside the same interpreter.
Separate intermediate DOM from explicitly painted output
Debug frames can recompute current layout and draw commands after a mutation. That does not count each frame as a presentation to the user. The interpreter updates paintedDraw and increments paintCount at explicit rendering opportunities after checkpoints. A computed intermediate state and an educational paint boundary therefore have separate representations.
The first successful script checkpoint creates an initial rendering opportunity. A later mutation to one can coexist with the previous paintedDraw. A microtask then changes the DOM to two before the drained checkpoint updates paintedDraw. These fields model inspectable boundaries; they do not measure physical display refresh or host-browser frame rate.
Relayout the same document
Track both the HTML id count and the Rust node identifier across changes. A text mutation should preserve the target element and relevant parent relationships. Updating a presentation panel while leaving the document consumed by the next operation unchanged creates an inconsistency between what is shown and what later callbacks read.
Follow mutate_text into recomputation and check that style, boxes, and draw commands use the changed string. Replace a short value with longer text and inspect layout as well as content. The project implements a limited layout subset, so explain differences through its supported properties and calculation rules rather than assuming full CSS compatibility.
Worked trace: the DOM passes through one before painting two
Use <button id="button">go</button><p id="count">0</p> as HTML with this program. Clicking registers a timer without executing task. A following runNext changes the DOM to one, queues repaint, and then changes it to two during the same checkpoint before recording the rendering opportunity.
Use operations [{"type":"click","target":"button"},{"type":"runNext"}]. Seek a frame where the DOM holds one while paintedDraw still represents the earlier state. Final output alone cannot establish that one never existed; an intermediate DOM frame cannot establish that one appeared on a physical display. Use both internal mutations and explicit painting boundaries as evidence.
function repaint() {
document.getElementById("count").textContent = "2";
}
function task() {
document.getElementById("count").textContent = "1";
queueMicrotask(repaint);
}
function clicked() { setTimeout(task, 0); }
document.getElementById("button").addEventListener("click", clicked);| Stage | DOM count | Explicit painted output |
|---|---|---|
| After initial script | 0 | 0 |
| After click | 0 | 0 |
| Timer mutation | 1 | Previous output |
| Microtask mutation | 2 | Previous output |
| Checkpoint ends | 2 | Updated to2 |
Compare updates, boundaries, and failures
The normal case reaches two after one click and one timer task. Compare click-only input with click-plus-runNext input as a boundary. Add cancellation to produce a delivered click whose deferred DOM update never occurs. Inspect dispatch, registration, and selection before attempting to repair rendering alone.
Use a missing element id as a failure and locate whether dispatch, timer evaluation, or DOM lookup stopped execution. Contrast it with a microtask registration budget failure that prevents reaching the next paint opportunity. Do not hide a diagnostic by silently restoring initial values; that would erase evidence about the failing boundary.
Runnable Rust: distinguish DOM and painted state
This standalone program stores dom and painted separately. It asserts that changing the DOM to one does not immediately change the painted field, then drains a microtask before adopting two. Compile with rustc. It is a contract for the distinction, not an implementation of the real DOM tree, CSS cascade, or box calculations.
Locate the corresponding nodes, paintedDraw, and paintCount in the actual project. The count two in this small program assumes initialization and one task. A real click operation has another checkpoint, so do not copy that count into a different operation sequence. Specify which boundaries were counted rather than comparing a number without its execution context.
use std::collections::VecDeque;
#[derive(Debug)]
struct View { dom: String, painted: String, paints: u32 }
fn main() {
let mut view = View { dom:"0".into(), painted:"0".into(), paints:1 };
view.dom = "1".into();
assert_eq!(view.painted,"0");
let mut micro = VecDeque::from(["2"]);
while let Some(text) = micro.pop_front() { view.dom = text.into(); }
view.painted = view.dom.clone(); view.paints += 1;
assert_eq!((view.dom.as_str(),view.painted.as_str(),view.paints),("2","2",2));
println!("dom={} painted={} paints={}",view.dom,view.painted,view.paints);
}Preserve operation meaning across resume
Replay requires initial HTML, CSS, JavaScript, and the sequence of clicks and runNext operations. It does not serialize a suspended host function. Re-evaluating the same input and operations reconstructs pending work and DOM state. Moving a frame cursor must not overwrite initial input with values taken from that intermediate frame.
Compare intermediate nodes, timers, microtasks, and paintedDraw in native and WASM runs, not only final text. Match the distributed source and WASM versions and retain input plus diagnostics with the results. Local implementation completion is a self-reported milestone, separate from a task result graded by the Web interface.
Make two saved cases: one immediately after the click has registered its timer, and another after that timer has been consumed. A further runNext must update the first case but find no new timer in the second. Both cases can eventually show the same text two, yet they represent different pending work. Verify one operation beyond the saved point rather than relying on the restored screenshot. Also check that viewing an earlier mutation frame changes neither the saved operations nor the pending state used by that next operation.
Capstone: display pending, completed, and cancelled states
Make a click immediately display pending, let a timer update a number, and let a microtask display complete. Predict which text survives at each rendering opportunity before executing. Add cancellation and verify agreement between the cancelled display and the disappearance of its timer. Preserve the identity of the target document nodes.
Submit the operation sequence, DOM state, pending queues, and explicit painted output at each stage. Distinguish failure to refresh paintedDraw after mutation from incorrectly counting a paint before draining microtasks. Explain the complete click-to-display path by assigning responsibility to dispatch, scheduling, task selection, checkpointing, layout, and drawing.