addCleanup
Registers cleanup on an explicit owner.
Registers cleanup on an explicit owner. It runs when the owner is disposed or, when the owner is a
computation, before its next run. Cleanups run newest first, untracked and with no current owner;
one that throws goes to handleError for that owner and does not stop the others. Use it
where onCleanup cannot be called: the owner is not the current one, or the code must not warn.
Unlike onCleanup, nothing checks that the owner is still alive: a cleanup added to a disposed
owner never runs.
Import#
ts
import { addCleanup } from "@shadow-js/core/extend";
Signature#
ts
function addCleanup(owner: Owner, cleanup: Cleanup): void
Parameters#
| Name | Description |
|---|---|
owner |
The owner whose disposal runs the cleanup. |
cleanup |
The function to run. |
Examples#
ts
import { addCleanup, currentOwner } from "@shadow-js/core/extend";
export function useInterval(tick: () => void, ms: number): void {
const owner = currentOwner();
const timer = setInterval(tick, ms);
if (owner !== null) addCleanup(owner, () => clearInterval(timer));
}