createTrigger

The createTrigger function 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.

Examples

Example 1: Basic Usage

Create a trigger and use it to manually trigger signal re-computations:

import {createTrigger} from 'ngx-lift';
import {Component, effect} from '@angular/core';

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');
    });
  }

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

Example 2: Conditional Effect

Use the trigger value conditionally to prevent running on initialization:

import {createTrigger} from 'ngx-lift';
import {Component, effect} from '@angular/core';

export class CreateTriggerComponent {
  private refreshTrigger = createTrigger();

  constructor() {
    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!');
      }
    });
  }

  refresh() {
    this.refreshTrigger.next();
  }
}

API Reference

createTrigger

Creates a trigger signal that can be used to manually initiate signal re-computations.

Signature

createTrigger(): {value: Signal<number>; next: () => void}

Returns

An object with two properties:

  • value: A signal that returns the number of times the trigger has been called (starts at 0)
  • next: A method to trigger the signal update