add<T> method
Adds a new function
to the queue. The function
will be executed
immediately after all previously added functions have completed.
If the buffer
parameter is provided, the function is executed alongside
a buffer duration. This ensures that the total execution time of the
function is at least as long as the buffer
duration, which can be useful
for throttling operations and ensuring a minimum duration for each task.
The method returns a FutureOr that completes when both the function and all preceding functions in the queue have completed. This means, when awaiting, the returned FutureOr will resolve only after all prev functions, including any added with a buffer, have finished executing.
Implementation
FutureOr<T> add<T>(
TSyncOrAsyncMapper<dynamic, T> function, {
Duration? buffer,
}) {
final executable = _Executable<T>(
buffer == null
? function
: (prev) async => (await Future.wait<T>([
Future.value(function(prev)),
Future.delayed(buffer),
]))
.first,
);
_queue.add(executable);
final result =
mapSyncOrAsync(_execute(), (_) => executable.completer.futureOr);
return result;
}