@blinkk/degu
    Preparing search index...

    Function runOnceOnChange

    • Runs a function ONLY when the parameters have changed.

      Running the below, you will see only four console.logs executed, each when the params have changed.


      let expensiveOperation = func.runOnceOnChange(
      (name) => {
      // Do expensive stuff here.
      console.log(name);
      }
      );

      expensiveOperation('Scott'); // Scott
      expensiveOperation('Scott');
      expensiveOperation('Scott');
      expensiveOperation('Scott');
      expensiveOperation('Scott');
      expensiveOperation('John'); // John
      expensiveOperation('John');
      expensiveOperation('Aya'); // Aya
      expensiveOperation('Aya');
      expensiveOperation('John'); // John
      expensiveOperation('John');

      More in practice. Here we want to do mutate only when the window.innerWidth or windowHeight has changed.

      let updateCanvasSize = func.runOnceOnChange(
      (windowWidth, windowHeight)=> {
      // Do something expensive.
      console.log('Change the canvas size');
      })

      new Raf(()=> {
      updateCanvasSize(window.innnerWidth, window.innerHeight);
      });

      You can implement this for a class method as follows

      class MyClass {

      constructor() {
      this.element = document.getElementById('hello');
      this.render = func.runOnceOnChange(this.render.bind(this));

      this.render(800);
      this.render(800); // Won't be called a second time.
      }

      render(height) {
      this.element.style.height = height + 'px';
      }
      }

      Parameters

      • callback: Function

        The callback to execute. Doesn't require the callback to return a value.

      Returns (...args: any[]) => any