ignoreUpdates<U> method

  1. @override
U ignoreUpdates<U>(
  1. U fn()
)
override

Temporarily ignores updates from the reactive sources during function execution.

This method executes the given function while preventing the watcher's callback from being triggered by any changes that occur during execution. The reactive sources will still update normally, but the watcher's callback will not be executed for changes during the ignored period.

Behavior:

  • Only prevents callback execution; ref changes and listener updates still occur
  • Does not update prevValue during the ignored period
  • Changes during ignore are treated as "never happened" for oldValue purposes, but newValue will always reflect the latest state
  • Works correctly even when nested inside batches

Implementation note: This method uses batch to delay side effects and restores flags to prevent new changes during ignore from triggering callbacks. If the previous flags required execution (e.g., had dirty), it will still execute after restore (preserves existing pending tasks).

Parameters:

  • fn: The function to execute while ignoring updates

Returns: The result of executing fn

Type parameter:

  • U: The return type of fn

Example:

final signal = Signal(1);
final values = <int>[];
final watcher = Watcher(
  () => signal.value,
  (newValue, _) => values.add(newValue),
);

signal.value = 2; // Triggers
expect(values, equals([2]));

watcher.ignoreUpdates(() {
  signal.value = 3; // Does not trigger callback
});
expect(values, equals([2]));
expect(signal.value, equals(3)); // Value still updated

signal.value = 4; // Triggers again
expect(values, equals([2, 4]));

Example with nested batch:

batch(() {
  signal.value = 5;
  watcher.ignoreUpdates(() {
    signal.value = 6;
  });
  signal.value = 7;
});
// Only the final value (7) triggers the callback

Implementation

@override
U ignoreUpdates<U>(U Function() fn) {
  assert(!isDisposed, "Watcher is disposed");

  return batch(() {
    int prevFlags = flags;
    try {
      return fn();
    } finally {
      flags = prevFlags;
    }
  });
}