Event delegation
Open live editorDiscover → Predict → Understand
What evidence shows that event delegation keeps state and application behavior understandable?
Delegation resolves an action from the clicked descendant and a stable parent handler. This data kernel models nearest actionable ancestor; actual events are visible in the studio.
- Explain the operation and its responsibility.
- Predict actual values before execution.
- Identify local calculation, actual DOM and external execution boundaries.
Conceptual application flow
- User intent
- Action / handler
- Business logic
- State
- Render / persistence
Five source examples
Working contract
Delegation resolves an action from the clicked descendant and a stable parent handler. This data kernel models nearest actionable ancestor; actual events are visible in the studio.
async function main(input) {
return input.ancestors.find(x=>x.action)?.action??null;
}Input
{
"ancestors": [
{
"tag": "span"
},
{
"tag": "button",
"action": "remove"
}
]
}Fixed expected result
remove
Observe input preservation
The wrapper records the original input and compares it after the operation. Some lessons intentionally demonstrate mutation in a local copy.
async function calculate(input){
return input.ancestors.find(x=>x.action)?.action??null;
}
async function main(input){const before=structuredClone(input);const result=await calculate(input);return {result,inputUnchanged:JSON.stringify(input)===JSON.stringify(before)};}Input
{
"ancestors": [
{
"tag": "span"
},
{
"tag": "button",
"action": "remove"
}
]
}Fixed expected result
{
"result": "remove",
"inputUnchanged": true
}Investigate the discarded result
An early return bypasses the intended operation. Repair the first incorrect boundary rather than changing the expected result.
async function calculate(input){
return input.ancestors.find(x=>x.action)?.action??null;
}
async function main(input){return null; // Deliberate early return discards calculation
return await calculate(input);}Input
{
"ancestors": [
{
"tag": "span"
},
{
"tag": "button",
"action": "remove"
}
]
}Fixed expected result
remove
Repeat the deterministic calculation
Invoke the same declared pure/model operation twice and compare both results. This is not an external idempotency guarantee.
async function calculate(input){
return input.ancestors.find(x=>x.action)?.action??null;
}
async function main(input){return [await calculate(structuredClone(input)),await calculate(structuredClone(input))];}Input
{
"ancestors": [
{
"tag": "span"
},
{
"tag": "button",
"action": "remove"
}
]
}Fixed expected result
[ "remove", "remove" ]
Frozen regression contract
Execute the operation and compare with a fixed fixture result. Changing the calculation must not silently rewrite the expectation.
async function calculate(input){
return input.ancestors.find(x=>x.action)?.action??null;
}
async function main(input){const actual=await calculate(input);const expected="remove";if(JSON.stringify(actual)!==JSON.stringify(expected))throw Error("Regression contract mismatch");return {pass:true,actual};}Input
{
"ancestors": [
{
"tag": "span"
},
{
"tag": "button",
"action": "remove"
}
]
}Fixed expected result
{
"pass": true,
"actual": "remove"
}Important operations
[
{
"line": 1,
"source": "async function main(input) {",
"explanation": "Declare the named operation or local value and follow its scope in the source."
},
{
"line": 2,
"source": "return input.ancestors.find(x=>x.action)?.action??null;",
"explanation": "Return the observable result for the caller; rendering and external effects belong to explicit boundaries."
},
{
"line": 3,
"source": "}",
"explanation": "Declare the named operation or local value and follow its scope in the source."
}
]Guided mission
- Discover the problem and inspect source/input.
- Write a prediction. Run the actual source and inspect the returned value.
- Change one requirement and state the new expected result independently.
- Load the repair exercise, reproduce the failure and use hints.
- Restore the authored fixture and Check, then explain and extend.
Three hints
- Locate the input, transition and returned result before editing.
- Compare the first differing field and trace which expression produced it.
- Remove the deliberate early return and restore the named responsibility; compare with Finished Code only after trying.
Complete solution
async function main(input) {
return input.ancestors.find(x=>x.action)?.action??null;
}Common mistake: Changing the expected result to match a defect instead of repairing the behavior.
Invalid JSON is rejected before execution. Syntax and thrown errors are shown. Model kernels for DOM, HTTP and Node teach contracts; use the actual browser and exported server projects for those execution boundaries.
Reflect and extend
Which returned value or actual DOM observation supports your explanation? Which side effect was outside this check?
Create a second normal, boundary and invalid case. Write exact expected values before execution and explain what the provided fixture does not prove.
AI prompt goals
- Explain the current source, input, actual output and architecture boundary.
- Help refactor one responsibility while preserving independently expected behavior.
- Investigate the current error and propose normal, boundary and invalid tests.