Skip to content

Commit

Permalink
waitForEmpty initialCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
alexobviously committed May 25, 2023
1 parent be8371d commit 5922147
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.1
- Added `initialCheck` parameter to `waitForEmpty()`, which causes the function to return immediately if the queue is already empty.

## 1.0.0
- `Secretary.waitForEmpty()`: wait for the task list to be empty. Useful for cases where a fixed number of tasks is added at once, and you just want to wait for all of them to finish.
- Recurring tasks now emit a `RecurringTaskFinishedEvent` when they finish.
Expand Down
15 changes: 14 additions & 1 deletion lib/src/secretary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,20 @@ class Secretary<K, T> {
/// Waits for all tasks to finish.
/// Useful in cases where a fixed number of tasks are added in the beginning,
/// and you just want to wait for all of them to complete.
Future<void> waitForEmpty({bool includeRecurring = false}) async {
///
/// If [initialCheck] is true, this function will return immediately if the
/// queue is empty when it is called. Defaults to false.
Future<void> waitForEmpty({
bool includeRecurring = false,
bool initialCheck = false,
}) async {
if (initialCheck &&
state.active.isEmpty &&
state.queue.isEmpty &&
(!includeRecurring || state.recurring.isEmpty)) {
return;
}

final stream = stateStream.map((e) =>
e.active.length +
e.queue.length +
Expand Down

0 comments on commit 5922147

Please sign in to comment.