accumulateBy method
Implementation
Stream<List<T>> accumulateBy(
int limit,
int Function(T) weigher, {
int? maxAmount,
}) async* {
List<T> buffer = [];
int size = 0;
await for (T chunk in this) {
if (maxAmount != null && buffer.length >= maxAmount) {
yield buffer;
buffer = [];
size = 0;
}
int weight = weigher(chunk);
if (size + weight > limit) {
if (buffer.isNotEmpty) {
yield buffer;
buffer = [];
size = 0;
} else {
yield [chunk];
buffer = [];
size = 0;
}
}
buffer.add(chunk);
size += weight;
}
if (buffer.isNotEmpty) {
yield buffer;
}
}