| 1 |  |  | import '../stats/worker_stat.dart'; | 
                
                
                    | 2 |  |  | import '../worker/worker.dart'; | 
                
                
                    | 3 |  |  | import '_worker_task.dart'; | 
                
                
                    | 4 |  |  | import 'worker_pool.dart'; | 
                
                
                    | 5 |  |  |  | 
                
                
                    | 6 |  |  | /// Class representing a [Worker] from a [WorkerPool]. | 
                
                
                    | 7 |  |  | class PoolWorker<W extends Worker> { | 
                
                
                    | 8 |  |  |   /// Constructs a new [PoolWorker]. | 
                
                
                    | 9 |  | 4 |   PoolWorker(this.worker, this._maxWorkload) : _capacity = _maxWorkload; | 
                
                
                    | 10 |  |  |  | 
                
                
                    | 11 |  |  |   /// The [Worker] associated to this [PoolWorker]. | 
                
                
                    | 12 |  |  |   final W worker; | 
                
                
                    | 13 |  |  |  | 
                
                
                    | 14 |  |  |   final int _maxWorkload; | 
                
                
                    | 15 |  |  |   int? _lastStart; | 
                
                
                    | 16 |  |  |   int _capacity; | 
                
                
                    | 17 |  |  |  | 
                
                
                    | 18 |  |  |   /// The current capacity of this [PoolWorker]. | 
                
                
                    | 19 |  | 8 |   int get capacity => _capacity; | 
                
                
                    | 20 |  |  |  | 
                
                
                    | 21 |  |  |   /// Whether this [PoolWorker] is stopped or idle. | 
                
                
                    | 22 |  | 6 |   bool get isIdle => worker.isStopped || _capacity == _maxWorkload; | 
                
                
                    | 23 |  |  |  | 
                
                
                    | 24 |  |  |   /// Whether this [PoolWorker] is stopped. | 
                
                
                    | 25 |  | 12 |   bool get isStopped => worker.isStopped; | 
                
                
                    | 26 |  |  |  | 
                
                
                    | 27 |  |  |   /// Run the specified [task] in the [worker]. | 
                
                
                    | 28 |  | 4 |   Future<void> run(WorkerTask task) { | 
                
                
                    | 29 |  | 12 |     _lastStart = DateTime.now().millisecondsSinceEpoch; | 
                
                
                    | 30 |  | 8 |     _capacity--; | 
                
                
                    | 31 |  | 16 |     return task.run(worker).whenComplete(() { | 
                
                
                    | 32 |  | 8 |       _capacity++; | 
                
                
                    | 33 |  | 12 |       if (_capacity == _maxWorkload) { | 
                
                
                    | 34 |  | 4 |         _lastStart = null; | 
                
                
                    | 35 |  |  |       } | 
                
                
                    | 36 |  |  |     }); | 
                
                
                    | 37 |  |  |   } | 
                
                
                    | 38 |  |  |  | 
                
                
                    | 39 |  |  |   /// Compares [PoolWorker] instances by capacity (descending) and last execution (ascending). | 
                
                
                    | 40 |  | 4 |   static int compareCapacity(PoolWorker a, PoolWorker b) { | 
                
                
                    | 41 |  | 21 |     if (a.capacity != b.capacity) return a.capacity.compareTo(b.capacity); | 
                
                
                    | 42 |  | 8 |     if (a._lastStart == null) return -1; | 
                
                
                    | 43 |  | 2 |     if (b._lastStart == null) return 1; | 
                
                
                    | 44 |  | 6 |     return b._lastStart!.compareTo(a._lastStart!); | 
                
                
                    | 45 |  |  |   } | 
                
                
                    | 46 |  |  |  | 
                
                
                    | 47 |  | 3 |   static WorkerStat getStats(PoolWorker w) => w.worker.getStats(); | 
                
                
                    | 48 |  |  | } |