request method

  1. @override
Future<TransportMessage> request(
  1. String subject,
  2. Uint8List data, {
  3. Map<String, String>? arguments,
  4. String? originId,
  5. Duration? timeout,
})
override

Sends a request and waits for the first response.

Throws TransportServerUnavailableException when the connection is not established and TimeoutException when no response arrives within timeout. A null timeout waits until a response arrives.

Implementation

@override
Future<TransportMessage> request(
  String subject,
  Uint8List data, {
  Map<String, String>? arguments,
  String? originId,
  Duration? timeout,
}) async {
  if (!isConnected) {
    throw TransportServerUnavailableException(subject);
  }

  final replyTo = '_INBOX.${_nextInbox++}';
  requests.add(
    FakeTransportRequest(subject, data, arguments, originId, replyTo),
  );
  final completer = Completer<TransportMessage>();
  final subscription = subscribe(replyTo, (message) {
    if (!completer.isCompleted) completer.complete(message);
  });

  try {
    final response = completer.future;
    return timeout == null ? await response : await response.timeout(timeout);
  } finally {
    await subscription.cancel();
  }
}