From 59221479d38b51731114ab02b5ab16fb77f0ffbf Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 25 May 2023 14:55:07 +0700 Subject: [PATCH] waitForEmpty initialCheck --- CHANGELOG.md | 3 +++ lib/src/secretary.dart | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 846d82c..f3d07bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/src/secretary.dart b/lib/src/secretary.dart index 87422e7..94656a2 100644 --- a/lib/src/secretary.dart +++ b/lib/src/secretary.dart @@ -308,7 +308,20 @@ class Secretary { /// 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 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 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 +