call<T> method

Watcher call<T>(
  1. SourcesFn<T> sourcesFn,
  2. WatcherFn<T> fn, {
  3. WhenFn<T>? when,
  4. bool immediately = false,
  5. JoltDebugFn? onDebug,
})

Creates a watcher hook that observes specific reactive sources.

Watchers provide more control than effects by explicitly defining what to watch and comparing old vs new values before executing the callback.

Parameters:

  • sourcesFn: Function that returns the values to watch
  • fn: Callback function executed when sources change
  • when: Optional condition function for custom trigger logic
  • immediately: Whether to execute the callback immediately (default is false)
  • onDebug: Optional debug callback for reactive system debugging

Returns: A Watcher with fine-grained change detection

Example:

setup(context, props) {
  final count = useSignal(0);
  final name = useSignal('Alice');

  useWatcher(
    () => [count.value, name.value],
    (newValues, oldValues) {
      print('Changed from $oldValues to $newValues');
    },
  );

  return () => Text('${name.value}: ${count.value}');
}

Implementation

Watcher call<T>(
  SourcesFn<T> sourcesFn,
  WatcherFn<T> fn, {
  WhenFn<T>? when,
  bool immediately = false,
  JoltDebugFn? onDebug,
}) {
  return useAutoDispose(() => Watcher<T>(sourcesFn, fn,
      when: when, immediately: immediately, onDebug: onDebug));
}