add method

List<DVCloudEvent> add(
  1. String chunk
)

The events chunk completes.

Implementation

List<DVCloudEvent> add(String chunk) {
  _pending += chunk;
  final List<DVCloudEvent> events = <DVCloudEvent>[];
  int newline;
  while ((newline = _pending.indexOf('\n')) >= 0) {
    String line = _pending.substring(0, newline);
    _pending = _pending.substring(newline + 1);
    if (line.endsWith('\r')) line = line.substring(0, line.length - 1);
    if (line.isEmpty) {
      final DVCloudEvent? event = _dispatch();
      if (event != null) events.add(event);
      continue;
    }
    if (line.startsWith(':')) continue;
    final int colon = line.indexOf(':');
    final String field = colon < 0 ? line : line.substring(0, colon);
    String value = colon < 0 ? '' : line.substring(colon + 1);
    if (value.startsWith(' ')) value = value.substring(1);
    switch (field) {
      case 'id':
        _id = int.tryParse(value);
      case 'event':
        _event = value;
      case 'data':
        _data.add(value);
    }
  }
  return events;
}