listenRequest<TRequestPayload> method

void listenRequest<TRequestPayload>(
  1. FutureOr onRequest(
    1. TRequestPayload
    )
)

Implementation

void listenRequest<TRequestPayload>(FutureOr<dynamic> Function(TRequestPayload) onRequest) {
  final sendPort = _sendPort;
  if (sendPort == null) {
    throw StateError('Messenger is not attached to an host');
  }

  if (_requestSubscription != null) {
    throw StateError('Messenger is already listening');
  }

  _requestSubscription = _requestStream.listen((request) async {
    try {
      final response = await onRequest(request.payload as TRequestPayload);
      sendPort.send(AudioIsolateWorkerSuccessResponse(request.id, response));
    } catch (e, stack) {
      sendPort.send(AudioIsolateWorkerFailedResponse(request.id, e, stack));
      _shutdownCompleter.completeError(e, stack);
    }
  });
}