process method
Processes a list of items in parallel
items
is the list of items to process
processFunction
is the function to apply to each item
onProgress
is an optional callback for progress updates
Implementation
Future<List<R>> process({
required List<T> items,
required Future<R> Function(T item) processFunction,
void Function(int completed, int total)? onProgress,
}) async {
if (items.isEmpty) return [];
// Use a single thread for small lists
if (items.length <= 2) {
final results = <R>[];
for (var i = 0; i < items.length; i++) {
final result = await processFunction(items[i]);
results.add(result);
onProgress?.call(i + 1, items.length);
}
return results;
}
// Initialize the isolate pool if needed
if (_isolatePool == null) {
await initialize();
}
// Register the process function
_isolatePool!.registerFunction(processFunction);
// Process the items using the isolate pool
return _isolatePool!.process<T, R>(
items: items,
processFunction: processFunction,
onProgress: onProgress,
);
}