send method

Future<Event?> send(
  1. Event event, {
  2. bool? waitResult,
  3. Duration? timeout,
  4. bool syncing = true,
})

Implementation

Future<Event?> send(
  Event event, {
  bool? waitResult,
  Duration? timeout,
  bool syncing = true,
}) async {
  if (!valid(syncing: syncing)) {
    return null;
  }
  if (!(waitResult ?? false)) {
    await _protocol!.send(event);
    return null;
  }
  var completer = Completer();
  var requestId = DateTime.now().toUtc().millisecondsSinceEpoch.toString();
  event.withMeta("request-id", requestId);
  Event? result;
  Timer? timer;
  if (timeout != null) {
    timer = Timer(timeout, () {
      completer.complete();
    });
  }
  var handler = RequestIdEventHandler((e) {
    if (e.hasMeta("request-id", requestId)) {
      result = e;
      if (timer != null) {
        timer.cancel();
      }
      completer.complete();
    }
  }, requestId: requestId);
  _protocol!.addEventListner(handler);
  if (!await _protocol!.send(event)) {
    return null;
  }
  await completer.future;
  _protocol!.removeEventListner(handler);
  return result;
}