parseLine static method

MapEntry<String, dynamic> parseLine(
  1. String line
)

Parses each separated line of an SSE event.

Implementation

//TODO(nvkantonio): Rework this to properly utilize
// SSE documentation practices
static MapEntry<String, dynamic> parseLine(String line) {
  try {
    final separatorIndex = line.indexOf(': ');
    if (separatorIndex < 2) {
      throw SseParseException(message: 'Invalid line: $line');
    }

    final key = line.substring(0, separatorIndex);
    final value = line.substring(separatorIndex + 2, line.length);

    if (key.isEmpty || value.isEmpty) {
      throw SseParseException(message: 'Invalid line: $line');
    }

    dynamic decodedValue;
    if (value.startsWith('{') && value.endsWith('}')) {
      decodedValue = jsonDecode(value);
    }

    return MapEntry<String, dynamic>(key, decodedValue ?? value);
  } on SseParseException {
    rethrow;
  } catch (e) {
    throw SseParseException(
      message: 'Failed to parse SSE line: $line',
      originalExeption: e,
    );
  }
}