Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/src/compute_api/compute_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ class ComputeAPI {
Future<R> compute<P, R>(
Function fn, {
P? param,
String? taskName = "Unknown",
}) async {
_logger?.log('Started computation');

_logger?.log('Started computation for task ${taskName ?? ""}');
final taskCapability = Capability();
final taskCompleter = Completer<R>();

final task = Task(
task: fn,
param: param,
name: taskName,
capability: taskCapability,
);

Expand All @@ -73,14 +74,14 @@ class ComputeAPI {
final freeWorker = _findFreeWorker();

if (freeWorker == null) {
_logger?.log('No free workers, add task to the queue');
_logger?.log('No free workers, add task ${taskName ?? ""} to the queue');
if (_workers.length == 1) {
_workers.single.execute(task);
} else {
_taskQueue.add(task);
}
} else {
_logger?.log('Found free worker, executing on it');
_logger?.log('Found free worker, executing ${taskName ?? ""} on it');
freeWorker.execute(task);
}

Expand Down Expand Up @@ -122,6 +123,7 @@ class ComputeAPI {
void _onTaskFinished(TaskResult result, Worker worker) {
final taskCompleter = _activeTaskCompleters.remove(result.capability)!;
taskCompleter.complete(result.result);
_logger?.log('Finished task ${result.name ?? ""}');

if (_taskQueue.isNotEmpty) {
_logger?.log("Finished task on worker, queue isn't empty, pick task");
Expand Down
13 changes: 4 additions & 9 deletions lib/src/compute_api/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@ import 'dart:isolate';
class Task {
final Function task;
final dynamic param;
final String? name;

final Capability capability;

Task({
required this.task,
required this.capability,
this.param,
});
Task({required this.task, required this.capability, this.param, this.name});
}

class TaskResult {
final dynamic result;
final Capability capability;
final String? name;

TaskResult({
required this.result,
required this.capability,
});
TaskResult({required this.result, required this.capability, this.name});
}
1 change: 1 addition & 0 deletions lib/src/compute_api/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Future<void> isolateEntryPoint(IsolateInitParams params) async {
final result = TaskResult(
result: computationResult,
capability: task.capability,
name: task.name,
);
sendPort.send(result);
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion lib/src/computer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class Computer {

/// Executes function `fn` with passed `param`. Takes only top-level functions and static methods.
/// `P` is `param` type, `R` is function return type
/// `taskName` is a identifier for the task that's only used during logging
Future<R> compute<P, R>(
Function fn, {
P? param,
String? taskName,
}) async {
return _computeDelegate.compute<P, R>(fn, param: param);
return _computeDelegate.compute<P, R>(fn, param: param, taskName: taskName);
}

/// Turn off `Computer`
Expand Down