Skip to content
Playground Tests llms.txt
Docs in progress TKO docs are in progress. Examples, API details, and migration notes are still being revised.

Lifecycle

LifeCycle helps an object keep track of temporary computeds, subscriptions, event handlers, and other disposables.

You can use it as a base class or mix it into an existing constructor or object:

class OtherClass extends LifeCycle {}
LifeCycle.mixInto(thing)

Call dispose directly, or anchor the lifecycle to a DOM node with anchorTo(node) so disposal happens when that node leaves the document.

Function or methodPurpose
this.computed(fn)Create a computed tied to the lifecycle
this.subscribe(obs, fn)Create a subscription tied to the lifecycle
this.addEventListener(node, ...)Register a DOM listener that is removed automatically
this.anchorTo(node)Dispose the lifecycle when the node is removed
this.addDisposable(obj)Dispose a custom object during cleanup

When a lifecycle ends, its computeds, subscriptions, event handlers, and added disposables are cleaned up. If the lifecycle is anchored to a DOM node, cleanup runs when that node is removed.

Prefer pureComputed where possible, and explicitly dispose anything that outlives the object itself.