createTrigger

createTrigger provides a way to manually initiate signal re-computations by referencing the trigger signal it creates. Its functionality is similar to RxJS Subject. With each event, you can notify the subject using its next method. Using createTrigger is convenient for manually triggering a "refresh" and responding to it in a signal-oriented manner.

createTrigger() returns an object with two properties: next and value. The value property indicates how many times you have triggered the signal update. The next method is used to trigger the signal update. Consequently, you can prompt the re-computation of any computed or effect (including computedAsync from ngx-lift) by accessing the signal returned when listening.

import {createTrigger} from 'ngx-lift';

export class CreateTriggerComponent {
  private refreshTrigger = createTrigger();

  constructor() {
    effect(() => {
      this.refreshTrigger.value();

      // whatever code you want to run whenever refreshTrigger.next() is called
      console.log('trigger effect');
    });

    effect(() => {
      if (this.refreshTrigger.value()) {
        // Will NOT run on init
        // whatever code you want to run whenever refreshTrigger.next() is called
        console.log('You click the button!');
      }
    });
  }

  // when button clicks
  refresh() {
    this.refreshTrigger.next();
  }
}