Skip to content

clean

clean(cleaner): void

Defined in: aberdeen.ts:3105

Registers a cleanup function to be executed just before the current reactive scope is destroyed or redraws.

This is useful for releasing resources, removing manual event listeners, or cleaning up side effects associated with the scope. Cleaners are run in reverse order of registration.

Scopes are created by functions like derive, mount, A (when given a render function), and internally by constructs like onEach.

() => void

The function to execute during cleanup.

void

Maintaing a sum for a changing array

const $numbers = A.proxy([3, 5, 10]);
let $sum = A.proxy(0);
// Show the array items and maintain the sum
A.onEach($numbers, (item, index) => {
A(`code#${index}${item}`);
// We'll update $sum.value using peek, as += first does a read, but
// we don't want to subscribe.
A.peek(() => $sum.value += item);
// Clean gets called before each rerun for a certain item index
// No need for peek here, as the clean code doesn't run in an
// observer scope.
A.clean(() => $sum.value -= item);
})
// Show the sum
A('h1 text=', $sum);
// Make random changes to the array
const rnd = () => 0|(Math.random()*20);
setInterval(() => $numbers[rnd()] = rnd(), 1000);