Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

fxConflate

One async run at a time; whatever arrives meanwhile is collapsed to its newest value, which runs next.

Conflated<T> fxConflate<T>(FutureOr<void> Function(T value) run, {void Function(Object error, StackTrace stackTrace)? onError}) class Conflated<T> { void call(T value); // run now, or hold as the next value bool get isRunning; bool get isPending; // a value is waiting for the run in flight Future<void> get idle; // nothing running, nothing waiting void cancel(); // drop the waiting value }

Lecture

A video scrubber calls seekTo on every drag tick, but a seek takes longer than a tick. Awaiting each one in turn builds a queue of stale seeks; firing them all at once lets an old one finish last and win. What the scrubber means is "go to wherever the thumb is now", and fxConflate(run) is that: the first call starts at once; a call that arrives while a run is in flight is held, and a newer call replaces it; when the run finishes, the held value — the newest — runs. Everything in between is dropped.

It never waits for quiet (that is fxDebounce) and never abandons the run in flight (that is switchLatest on the events layer) — a Future cannot be cancelled, so the honest behaviour is to let it finish and move on. .isRunning and .isPending describe the state, .idle completes once nothing runs and nothing waits, and .cancel() drops the held value.

An error thrown by run goes to onError, or to the current zone when there is none, and does not stop the next run — a failed sync should not block the one after it. fxdart extension (not part of FxTS), after Kotlin Flow's conflate.

Demo 1 · A scrubber that outruns its seek

Demo 2 · State, errors and idle

Try it yourself

Exercise: render only the first and the newest slider value.

Related: fxDebounce — wait until calls stop · fxThrottle — at most once per window · switchLatest — the stream form, which abandons the old run · concurrentPool — many runs, bounded