limitChunkedStream<T> function
Create a chunked stream limited to the first maxSize
items from input
.
Throws MaximumSizeExceeded if input
contains more than maxSize
items.
Implementation
Stream<List<T>> limitChunkedStream<T>(
Stream<List<T>> input, {
int? maxSize,
}) async* {
if (maxSize != null && maxSize < 0) {
throw ArgumentError.value(maxSize, 'maxSize must be positive, if given');
}
var count = 0;
await for (final chunk in input) {
if (maxSize != null && maxSize - count < chunk.length) {
yield chunk.sublist(0, maxSize - count);
throw MaximumSizeExceeded(maxSize);
}
count += chunk.length;
yield chunk;
}
}