From fc0f1adb5d7c7a66cf2a7687a396d911f43b8300 Mon Sep 17 00:00:00 2001 From: mark8044 <87546778+mark8044@users.noreply.github.com> Date: Wed, 2 Apr 2025 17:24:16 -0700 Subject: [PATCH] Add an option to cancel the queue without throwing errors --- lib/src/dart_queue_base.dart | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/src/dart_queue_base.dart b/lib/src/dart_queue_base.dart index eadd3bc..a18a1be 100644 --- a/lib/src/dart_queue_base.dart +++ b/lib/src/dart_queue_base.dart @@ -52,9 +52,12 @@ class Queue { /// A timeout before processing the next item in the queue final Duration? timeout; - /// Shoud we process this queue LIFO (last in first out) + /// Should we process this queue LIFO (last in first out) final bool lifo; + ///Should we throw an exception for items still in the Queue when .cancel() is called + final bool supressErrorsOnCancel; + /// The number of items to process at one time /// /// Can be edited mid processing @@ -97,7 +100,14 @@ class Queue { /// Subsquent calls to [add] will throw. void cancel() { for (final item in _nextCycle) { - item.completer.completeError(QueueCancelledException()); + if (supressErrorsOnCancel == false) + { + item.completer.completeError(QueueCancelledException()); + } + else + { + item.completer.complete(); + } } _nextCycle.removeWhere((item) => item.completer.isCompleted); _isCancelled = true; @@ -113,7 +123,7 @@ class Queue { cancel(); } - Queue({this.delay, this.parallel = 1, this.timeout, this.lifo = false}); + Queue({this.delay, this.parallel = 1, this.timeout, this.lifo = false, this.supressErrorsOnCancel = false}); /// Adds the future-returning closure to the queue. ///