d-markey / squadron

Multithreading and worker thread pool for Dart / Flutter, to offload CPU-bound and heavy I/O tasks to Isolate or Web Worker threads.
https://pub.dev/packages/squadron
MIT License
79 stars 0 forks source link

Listen to task started/completed events #28

Closed sabin26 closed 9 months ago

sabin26 commented 9 months ago

Is there any similar api to workerPool.registerWorkerPoolListener((worker, removed) {}); that can listen to task being started or completed? I didn't find one.

d-markey commented 9 months ago

This API is not for tasks, it's for workers: after the pool has started or stopped a worker, it triggers the callback.

It does not make sense for individual workers because you control the worker's lifecycle. You can still use the future returned by Worker.start() if you want to take action after the worker has effectively started, but you'd typically simply await that call in your code. Worker.stop() does not return a future because it closes the channel immediately.

There's no such mechanism for tasks. WorkerPool provides a way to get hold of a task per se via scheduleTask() and scheduleStream(), but there's no callback, future or stream for its lyfecycle events. Instead, these task objects have flags to describe their status (see isRunning, isFinished...).