Coordinate application lifecycle and cleanup
Quick answer
A feature owns the resources it creates: subscriptions, pending work, timers and DOM listeners. An explicit lifecycle makes setup and cleanup predictable when a feature mounts, reloads or is disposed.
How it works
At setup, record cleanup functions and expose one dispose operation. Release resources in reverse acquisition order if dependencies require it. Make disposal idempotent: calling it twice must not repeat effects. A robust implementation should attempt remaining cleanups even if one fails, report failures, and handle partial setup failure. Cancellation of an operation is cooperative; use a freshness guard as well if completion can arrive after disposal.
Local examples
Run in Node.js v22.16.0 as a CommonJS script. Actual ByteMind editor support is unverified.
(async () => {const seen=[],cleanups=[()=>seen.push("A"),()=>{seen.push("B");throw new Error("B");},()=>seen.push("C")];let disposed=false;function dispose(){if(disposed)return [];disposed=true;const errors=[];while(cleanups.length){try{cleanups.pop()();}catch(e){errors.push(e.message);}}return errors;}console.log(seen.length,JSON.stringify(dispose()),seen.join(","),JSON.stringify(dispose()));})()Expected console output:
0 ["B"] C,B,A []Cleanup starts only on disposal, attempts every resource and does not repeat.
Run in Node.js v22.16.0 as a CommonJS script. Actual ByteMind editor support is unverified.
(async () => {let active=true,publish=[];let resolve;const pending=new Promise(r=>resolve=r);const done=pending.then(x=>{if(active)publish.push(x);});active=false;resolve("late");await done;console.log(JSON.stringify(publish));})()Expected console output:
[]The late result is ignored because the owner is no longer active.
Common mistakes
Do not leave an event subscription or stale async completion active after its owner is disposed. Do not rely on garbage collection for semantic cleanup.
Practice
Create a cleanup registry with two resources and a throwing cleanup between them. Verify that all are attempted once in reverse order and that the failure is reported.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Coordinate application lifecycle and cleanup — Knowledge Check
Apply the concept and reason about local example output.
What should happen when one cleanup throws?
Why make disposal idempotent?
What handles a completion arriving after a disposed feature?
What is the exact console output in Coordinate application lifecycle and cleanup example 1?
(async () => {const seen=[],cleanups=[()=>seen.push("A"),()=>{seen.push("B");throw new Error("B");},()=>seen.push("C")];let disposed=false;function dispose(){if(disposed)return [];disposed=true;const errors=[];while(cleanups.length){try{cleanups.pop()();}catch(e){errors.push(e.message);}}return errors;}console.log(seen.length,JSON.stringify(dispose()),seen.join(","),JSON.stringify(dispose()));})()What is the exact console output in Coordinate application lifecycle and cleanup example 2?
(async () => {let active=true,publish=[];let resolve;const pending=new Promise(r=>resolve=r);const done=pending.then(x=>{if(active)publish.push(x);});active=false;resolve("late");await done;console.log(JSON.stringify(publish));})()