send method
Sends a ThreadRequest to the least busy thread and returns the result as a Uint8List.
data
- The data to be processed by the thread.
Returns the result of the processing as a Uint8List.
Implementation
Future<Uint8List?> send(ThreadRequest data) async {
String taskId = data.id;
Thread thread = leastBusyThread;
thread.activeTasks++;
/// Await that isolate is ready and setup new completer
if (!thread.readyState.isCompleted) {
await thread.readyState.future;
}
ThreadTaskModel task = ThreadTaskModel(
taskId: taskId,
bytes$: Completer(),
threadId: thread.id,
);
tasks.add(task);
/// Destroy active tasks in thread if reached the convurrency limit
if ((processorConfigs.processorMode == ProcessorMode.limit ||
processorConfigs.processorMode == ProcessorMode.auto) &&
thread.activeTasks > processorConfigs.maxConcurrency) {
thread.destroyActiveTasks(taskId);
/// Await that all active tasks stopped.
List<ThreadTaskModel> activeTasks = tasks
.where((el) => el.threadId == thread.id && el.taskId != taskId)
.toList();
await Future.wait(activeTasks.map((el) async {
if (!el.bytes$.isCompleted) {
await el.bytes$.future;
}
}));
}
thread.send(data);
Uint8List? bytes = await task.bytes$.future;
tasks.remove(task);
return bytes;
}