bind method

Stream<String> bind(
  1. Stream<String> tokens
)

Applies this filter to tokens.

Implementation

Stream<String> bind(Stream<String> tokens) async* {
  final stops = stopSequences.where((s) => s.isNotEmpty).toList();
  if (stops.isEmpty) {
    yield* tokens;
    return;
  }

  final holdback =
      stops.map((s) => s.length).reduce((a, b) => a > b ? a : b) - 1;
  var buffer = '';

  await for (final token in tokens) {
    buffer += token;
    final stopAt = _firstStop(buffer, stops);
    if (stopAt != null) {
      onStop?.call();
      final emit = buffer.substring(0, stopAt);
      if (emit.isNotEmpty) yield emit;
      return;
    }

    if (buffer.length > holdback) {
      final emit = buffer.substring(0, buffer.length - holdback);
      if (emit.isNotEmpty) yield emit;
      buffer = buffer.substring(buffer.length - holdback);
    }
  }

  if (buffer.isNotEmpty) yield buffer;
}