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';
exportclassCreateTriggerComponent {
private refreshTrigger = createTrigger();
constructor() {
effect(() => {
this.refreshTrigger.value();
// whatever code you want to run whenever refreshTrigger.next() is calledconsole.log('trigger effect');
});
}
// when button clicksrefresh() {
this.refreshTrigger.next();
}
}
Initially, createTrigger's value() returns 0. Whenever next is explicitly called, the value adds 1.
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';
exportclassCreateTriggerComponent {
private refreshTrigger = createTrigger();
constructor() {
effect(() => {
if (this.refreshTrigger.value()) {
// Will NOT run on init// whatever code you want to run whenever refreshTrigger.next() is calledconsole.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.