Aberdeen - v1.0.3
    Preparing search index...

    Function clean

    • 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 observe, mount, $ (when given a render function), and internally by constructs like onEach.

      Parameters

      • cleaner: () => void

        The function to execute during cleanup.

      Returns void

      const myArray = proxy([3, 5, 10]);
      let sum = proxy(0);

      // Show the array items and maintain the sum
      onEach(myArray, (item, index) => {
      $(`code:${index}${item}`);
      // We'll update sum.value using peek, as += first does a read, but
      // we don't want to subscribe.
      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
      // observe scope.
      clean(() => sum.value -= item);
      })

      // Show the sum
      $('h1', {text: sum});

      // Make random changes to the array
      const rnd = () => 0|(Math.random()*20);
      setInterval(() => myArray[rnd()] = rnd(), 1000);