Skip to content

Commit

Permalink
fixup! fixup! TW-1586: add testing to guarantee that only 1 file is d…
Browse files Browse the repository at this point in the history
…ownloading at once
  • Loading branch information
sherlockvn committed Mar 19, 2024
1 parent b8e8c3a commit dbf4cbe
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 74 deletions.
18 changes: 9 additions & 9 deletions lib/utils/task_queue/worker_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ typedef OnTaskCompleted = void Function(String? taskId);

abstract class WorkerQueue {
final Queue<Task> queue = Queue<Task>();
Completer? completer;
Completer? _completer;

String get workerName;

Expand All @@ -21,13 +21,13 @@ abstract class WorkerQueue {
}

Future _processTask() async {
if (completer != null) {
return completer!.future;
if (_completer != null) {
return _completer!.future;
}
Logs().i(
'WorkerQueue<$workerName>::addTask(): QUEUE is ready',
);
completer = Completer();
_completer = Completer();
if (queue.isNotEmpty) {
final firstTask = queue.removeFirst();
Logs().i('WorkerQueue<$workerName>::_processTask(): ${firstTask.id}');
Expand All @@ -37,14 +37,14 @@ abstract class WorkerQueue {
.catchError(_handleTaskExecuteError)
.whenComplete(() => firstTask.onTaskCompleted?.call());
} else {
completer?.complete();
_completer?.complete();
}
return completer!.future;
return _completer!.future;
}

void _handleTaskExecuteCompleted(dynamic value) {
Logs().i('WorkerQueue<$workerName>::_handleTaskExecuteCompleted(): $value');
completer?.complete(value);
_completer?.complete(value);
_releaseCompleter();
if (queue.isNotEmpty) {
_processTask();
Expand All @@ -53,15 +53,15 @@ abstract class WorkerQueue {

void _handleTaskExecuteError(error) {
Logs().i('WorkerQueue<$workerName>::_handleTaskExecuteError(): $error');
completer?.complete(error);
_completer?.complete(error);
_releaseCompleter();
if (queue.isNotEmpty) {
_processTask();
}
}

void _releaseCompleter() {
completer = null;
_completer = null;
}

Future release() async {
Expand Down
Loading

0 comments on commit dbf4cbe

Please sign in to comment.