WatcherImpl<T>.once constructor
WatcherImpl<T>.once (
- SourcesFn<
T> sourcesFn, - WatcherFn<
T> fn, { - WhenFn<
T> ? when, - JoltDebugFn? onDebug,
Creates a watcher that executes once and then automatically disposes itself.
This factory method creates a watcher that will execute its callback on the first change after creation, and then automatically dispose itself. The watcher will not respond to changes before the first trigger, and will not respond to any changes after disposal.
Parameters:
sourcesFn: Function that returns the values to watchfn: Callback function executed on first changewhen: Optional condition function for custom trigger logiconDebug: Optional debug callback for reactive system debugging
Returns: A new Watcher instance that auto-disposes after first execution
Example:
final signal = Signal(1);
final values = <int>[];
final watcher = Watcher.once(
() => signal.value,
(newValue, _) {
values.add(newValue);
},
);
expect(values, isEmpty);
expect(watcher.isDisposed, isFalse);
signal.value = 2; // Triggers and disposes
expect(values, equals([2]));
expect(watcher.isDisposed, isTrue);
signal.value = 3; // No longer responds
expect(values, equals([2]));
Implementation
factory WatcherImpl.once(SourcesFn<T> sourcesFn, WatcherFn<T> fn,
{WhenFn<T>? when, JoltDebugFn? onDebug}) {
late WatcherImpl<T> watcher;
watcher = WatcherImpl(sourcesFn, (newValue, oldValue) {
fn(newValue, oldValue);
watcher.dispose();
}, when: when, immediately: false, onDebug: onDebug);
return watcher;
}