WatcherImpl<T> constructor
WatcherImpl<T> (})
Creates a new watcher with the given sources and callback.
Parameters:
sourcesFn: Function that returns the values to watchfn: Callback function executed when sources changeimmediately: Whether to execute the callback immediatelywhen: Optional condition function for custom trigger logicdetach: Whether to detach this watcher from the current effect scope. If true, the watcher will not be automatically disposed when its parent scope is disposed.debug: Optional debug options
Example:
final signal = Signal(0);
final watcher = Watcher(
() => signal.value,
(newValue, oldValue) => print('Changed: $oldValue -> $newValue'),
immediately: true,
when: (newValue, oldValue) => newValue > oldValue, // Only when increasing
);
Implementation
WatcherImpl(this.sourcesFn, this.fn,
{bool immediately = false, this.when, bool? detach, JoltDebugOption? debug})
: super(flags: ReactiveFlags.watching) {
assert(() {
JoltDebug.create(this, debug);
return true;
}());
final prevSub = setActiveSub(this);
if (!(detach ?? false) && prevSub != null) {
link(this, prevSub, 0);
}
try {
prevSources = sourcesFn();
if (immediately) {
untracked(() {
final prevWatcher = Watcher.activeWatcher;
Watcher.activeWatcher = this;
try {
fn(prevSources, null);
} finally {
Watcher.activeWatcher = prevWatcher;
}
});
assert(() {
JoltDebug.effect(this);
return true;
}());
}
} finally {
setActiveSub(prevSub);
}
}