call method

Future call(
  1. T payload
)

Dispatch this ActionV2 to all listeners. The payload will be passed to each listener's callback.

Implementation

Future call(T payload) {
  // Invoke all listeners in a microtask to enable waiting on futures. The
  // microtask queue is emptied before the event loop continues. This ensures
  // synchronous listeners are invoked in the current tick of the event loop
  // without being scheduled at the back of the event queue. A Dart [Stream]
  // behaves in a similar fashion.
  //
  // Performance benchmarks over 10,000 samples show no performance
  // degradation when dispatching actions using this action implementation vs
  // a [Stream]-based action implementation. At smaller sample sizes this
  // implementation slows down in comparison, yielding average times of 0.1 ms
  // for stream-based actions vs. 0.14 ms for this action implementation.
  Future callListenerInMicrotask(_ActionListener<T> l) =>
      Future.microtask(() => l(payload));
  return Future.wait(_listeners.map(callListenerInMicrotask));
}