flush method
void
flush()
Runs every queued effect, then rethrows the first error, if any.
Replaces alien_signals' flush, which stops at the first throwing effect
and drops the effects queued after it (see issue #182).
Only one flush runs at a time. A write inside an effect only queues effects, and the running flush runs them after that effect ends.
When the queue is empty and no Computed evaluation runs, the pending dependencies of disposed effects and computeds get auto-disposal. A parent that reruns has then subscribed its new children, so the dependencies they read again stay alive (see issue #202).
Implementation
void flush() {
if (_flushing) return;
_flushing = true;
Object? error;
StackTrace? stackTrace;
try {
do {
while (alien.queuedEffects != null) {
final effect = alien.queuedEffects! as alien.EffectNode;
alien.queuedEffects = effect.nextEffect;
effect.nextEffect = null;
if (alien.queuedEffects == null) alien.queuedEffectsTail = null;
try {
_AlienEffect.runQueued(effect);
} catch (e, s) {
if (error == null) {
error = e;
stackTrace = s;
}
} finally {
// An upstream run can restore an effect that its callback stopped.
_clearDisposedActiveEffect();
}
}
try {
if (_evaluationDepth == 0) _disposePending();
} catch (e, s) {
if (error == null) {
error = e;
stackTrace = s;
} else {
Zone.current.handleUncaughtError(e, s);
}
}
// An onDispose callback can write a signal and queue effects.
} while (alien.queuedEffects != null);
} finally {
_flushing = false;
}
if (error != null) Error.throwWithStackTrace(error, stackTrace!);
}