You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
763 B
Dart

typedef CancelCheck = bool Function();
Iterable<List<T>> _chunked<T>(List<T> list, int size) sync* {
if (size <= 0) size = 1;
for (var i = 0; i < list.length; i += size) {
final end = i + size;
yield list.sublist(i, end > list.length ? list.length : end);
}
}
Future<void> runChunked<T>(
Iterable<T> items,
int chunkSize,
Future<void> Function(T item) task, {
CancelCheck? isCancelled,
}) async {
final list = items.toList();
for (final chunk in _chunked(list, chunkSize)) {
if (isCancelled?.call() == true) break;
await Future.wait(
chunk.map((e) async {
if (isCancelled?.call() == true) return;
await task(e);
}),
eagerError: false,
);
if (isCancelled?.call() == true) break;
}
}