Waiter<T> class
Manages a collection of operations for deferred, batched execution.
Unlike Future.wait, which requires a list of already-running Future
instances, a Waiter collects WaiterOperation descriptors that have
not yet been executed. This lets you build up a queue of tasks from
different parts of your application and then run them all at once by
calling wait.
Per-isolate model. A Waiter instance is owned by exactly one
isolate. The internal queue is a mutable List, so two isolates must
each construct their own Waiter — passing a populated Waiter across
a SendPort is rejected at runtime unless every operation's run is a
top-level/static function and the receiving isolate is willing to
reconstruct the wrapper. Use operations to extract a sendable snapshot
when you need to hand work off to a worker isolate.
Web compatibility. Everything here is pure Dart with no
dart:isolate or dart:io dependencies, so it works unchanged on
Flutter web, dart2js, dartdevc, and the VM.
{@tool snippet}
final waiter = Waiter<String>();
// Define tasks without running them.
waiter.addFn(() => 'Sync task complete');
waiter.addFn(() async {
await Future.delayed(Duration(milliseconds: 20));
return 'Async task complete';
});
// Execute the entire batch.
final results = await waiter.wait();
print(results); // (Sync task complete, Async task complete)
{@end-tool}
Constructors
-
Waiter({_TOnErrorCallback? onError, Iterable<
WaiterOperation< operations = const <Never>[]})T> > - Creates a waiter to queue and manage deferred operations.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
-
operations
→ List<
WaiterOperation< T> > -
A read-only snapshot of the pending operations.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
add(
WaiterOperation< T> operation) → void - Adds a deferred operation to the queue.
-
addAll(
Iterable< WaiterOperation< operations) → voidT> > - Adds multiple deferred operations to the queue.
-
addFn(
FutureOr< T> fn(), {String? id}) → void -
Adds a bare function as a deferred operation. Convenience shortcut for
add(WaiterOperation(fn, id: id)); behaves identically except the caller does not have to construct the wrapper. -
clear(
) → void - Removes all pending operations from the queue.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
remove(
WaiterOperation< T> operation) → void - Removes a specific operation from the queue.
-
removeWhere(
bool test(WaiterOperation< T> op)) → void -
Removes operations matching
test. -
toString(
) → String -
A string representation of this object.
inherited
-
wait(
{_TOnErrorCallback? onError, bool eagerError = true}) → FutureOr< Iterable< T> > - Executes all queued operations and returns their results.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited