outputProcessor method

void outputProcessor(
  1. String line,
  2. NotificationProcessor? notificationProcessor
)
inherited

Handle a (possibly) json encoded object, completing the Completer in _pendingCommands corresponding to the response. Reports problems in decoding or message synchronization using listener, and replicates raw data to stdout as appropriate.

Implementation

void outputProcessor(
    String line, NotificationProcessor? notificationProcessor) {
  if (_stdioPassthrough) stdout.writeln(line);
  var trimmedLine = line.trim();

  // Guard against lines like:
  //   {"event":"server.connected","params":{...}}The Dart VM service is listening on ...
  const dartVMServiceMessage = 'The Dart VM service is listening on ';
  if (trimmedLine.contains(dartVMServiceMessage)) {
    trimmedLine = trimmedLine
        .substring(0, trimmedLine.indexOf(dartVMServiceMessage))
        .trim();
  }
  if (trimmedLine.isEmpty) {
    return;
  }

  listener?.messageReceived(trimmedLine);
  Map<String, Object?> message;
  try {
    message = json.decoder.convert(trimmedLine);
  } catch (exception) {
    listener?.badMessage(trimmedLine, exception);
    return;
  }

  // Handle response.
  final id = message[Response.ID];
  if (id != null) {
    final completer = _pendingCommands.remove(id);
    if (completer == null) {
      listener?.unexpectedResponse(message, id);
      return;
    }

    final errorJson = message[Response.ERROR];
    if (errorJson != null) {
      completer.completeError(
          RequestError.fromJson(ResponseDecoder(null), '.error', errorJson));
      return;
    }

    final resultJson = message[Response.RESULT];
    if (resultJson is Map<String, Object?>?) {
      completer.complete(resultJson);
      return;
    }

    listener?.unexpectedResponseFormat(message);
    return;
  }

  // Handle notification.
  final event = message[Notification.EVENT];
  if (event is String) {
    final paramsJson = message[Notification.PARAMS];
    if (paramsJson is Map<String, Object?>) {
      if (notificationProcessor != null) {
        notificationProcessor(Notification(event, paramsJson));
      }
    } else {
      listener?.unexpectedNotificationFormat(message);
    }
    return;
  }

  listener?.unexpectedMessage(message);
}