WatcherImpl<T>.immediately constructor

WatcherImpl<T>.immediately(
  1. SourcesFn<T> sourcesFn,
  2. WatcherFn<T> fn, {
  3. WhenFn<T>? when,
  4. JoltDebugFn? onDebug,
})

Creates a watcher that executes the callback immediately upon creation.

This factory method is a convenience constructor for creating a watcher with immediately set to true. The callback will be executed once immediately with the current source values, and then whenever the sources change and the condition (if provided) is met.

Parameters:

  • sourcesFn: Function that returns the values to watch
  • fn: Callback function executed when sources change
  • when: Optional condition function for custom trigger logic
  • onDebug: Optional debug callback for reactive system debugging

Returns: A new Watcher instance that executes immediately

Example:

final signal = Signal(10);
final values = <int>[];

Watcher.immediately(
  () => signal.value,
  (newValue, oldValue) {
    values.add(newValue);
  },
);

// Callback executed immediately with value 10
expect(values, equals([10]));

signal.value = 20; // Triggers callback again
expect(values, equals([10, 20]));

Implementation

factory WatcherImpl.immediately(SourcesFn<T> sourcesFn, WatcherFn<T> fn,
    {WhenFn<T>? when, JoltDebugFn? onDebug}) {
  return WatcherImpl(sourcesFn, fn,
      immediately: true, when: when, onDebug: onDebug);
}