call method

  1. @override
Stream<TState> call(
  1. Context<TState> context,
  2. Event event,
  3. NextMiddleware<TState> next
)
override

Implementation

@override
Stream<TState> call(
  Context<TState> context,
  Event event,
  NextMiddleware<TState> next,
) async* {
  if (eventTypes.contains(event.runtimeType)) {
    final existing =
        processedEvents.cast<EventProcessing<TState>?>().firstWhere(
              (x) => x!.event.runtimeType == event.runtimeType,
              orElse: () => null,
            );
    if (existing != null) {
      await existing.updates.toList();
    } else {
      final controller = StreamController<TState>();
      final processing = EventProcessing(event, controller.stream);
      processedEvents.add(processing);
      await for (final state in next(context, event)) {
        yield state;
        controller.add(state);
      }
      processedEvents.remove(processing);
      controller.close();
    }
  }
}