trigger function

void trigger(
  1. void fn()
)

Notifies subscribers of the signals read synchronously by fn.

Use this after mutating a list or map in place. Setting the same object does not notify readers because values are compared with identical.

final items = signal(<String>[]);
final lengths = <int>[];
final stop = effect(() => lengths.add(items().length));

items().add('new item');
items.set(items()); // The same object does not notify readers.
print(lengths); // [0]

trigger(() => items()); // Explicitly notifies readers of this signal.
print(lengths); // [0, 1]
stop();

Implementation

@pragma('vm:align-loops')
void trigger(void Function() fn) {
  final sub = ReactiveNode(flags: .watching), prevSub = setActiveSub(sub);
  try {
    fn();
  } finally {
    activeSub = prevSub;
    sub.flags = .none;
    Link? link = sub.deps;
    while (link != null) {
      final dep = link.dep;
      link = unlink(link, sub);

      final subs = dep.subs;
      if (subs != null) {
        propagate(subs, runDepth > 0);
        shallowPropagate(subs);
      }
    }
    if (batchDepth == 0) flush();
  }
}