on<E extends R> method

void on<E extends R>(
  1. RequestHandler<E, RestSuccessResponse<Object>> handler
)
inherited

Implementation

void on<E extends R>(
  RequestHandler<E, S> handler,
) {
  _requestController.stream.where((e) => e.request is E).listen(
    (e) async {
      final request = e.request as E;

      try {
        if (e is _StreamRequestCompleter) {
          final event = e as _StreamRequestCompleter<R, S>;

          final handlerCall = handler(request, (response) {
            if (!event.isCompleted) event.complete(response);
            event.emitSuccess(response);
          });
          if (handlerCall is Future) {
            unawaited(
              handlerCall.catchError(
                (Object error, StackTrace stackTrace) {
                  e.completeFailure(_onError(error, stackTrace, request));
                },
              ),
            );
          }
        } else {
          await handler(request, e.complete);
        }
      } catch (error, stackTrace) {
        e.completeFailure(_onError(error, stackTrace, request));
      }
    },
  );
}