Files
FlClash/lib/common/task_pool.dart
T
chen08209 8378c7081c feat(views): rework the proxies and scan pages, lay out records by their parts and unify search
A queued delay test shows a static clock until the task pool runs it, so
only the running tests animate while the list scrolls.

Changelog: Hide timed-out nodes and search every list
2026-10-02 20:32:42 +08:00

37 lines
793 B
Dart

import 'dart:async';
import 'dart:collection';
class TaskPool {
TaskPool(this.concurrency) : assert(concurrency > 0);
final int concurrency;
final Queue<Completer<void>> _waiting = Queue();
int _active = 0;
int get activeCount => _active;
int get pendingCount => _waiting.length;
int get idleSlots => _waiting.isEmpty ? concurrency - _active : 0;
Future<T> run<T>(Future<T> Function() task) async {
if (_active >= concurrency || _waiting.isNotEmpty) {
final waiter = Completer<void>();
_waiting.add(waiter);
await waiter.future;
} else {
_active++;
}
try {
return await task();
} finally {
if (_waiting.isNotEmpty) {
_waiting.removeFirst().complete();
} else {
_active--;
}
}
}
}