trigger function
void
trigger(
- void fn()
Manually triggers reactive updates within a function.
Creates a temporary reactive context and executes the given function. Any signals accessed during execution will be tracked, and their subscribers will be notified of changes.
Useful for imperatively triggering updates in the reactive system without creating a permanent effect.
Example:
final count = signal(0);
trigger(() {
count(); // Access triggers propagation to subscribers
});
Implementation
@pragma('vm:align-loops')
void trigger(void Function() fn) {
final sub = ReactiveNode(flags: ReactiveFlags.watching),
prevSub = setActiveSub(sub);
try {
fn();
} finally {
activeSub = prevSub;
Link? link = sub.deps;
while (link != null) {
final dep = link.dep;
link = unlink(link, sub);
final subs = dep.subs;
if (subs != null) {
sub.flags = ReactiveFlags.none;
propagate(subs);
shallowPropagate(subs);
}
}
if (batchDepth == 0) flush();
}
}