dart_queue icon indicating copy to clipboard operation
dart_queue copied to clipboard

Queue.onComplete not return when queue is empty

Open ductranit opened this issue 4 years ago • 4 comments

I used queue to wait for all requests finished like this:

var queue = Queue(parallel: 4);
for (final item in items) {
   if(valid(item)) {
        queue.add(() => _sendRequest(item));
   }
}

await queue.onComplete;
print('completed');

For some reasons, if there is no valid item in for loop, the queue is empty. And in this case it pauses at await queue.onComplete forever. Expect: it should allow to go through next line if queue is empty.

ductranit avatar Oct 05 '21 06:10 ductranit

I'm experiencing this as well

givip avatar Oct 21 '21 10:10 givip

Hey guys,

Thanks for raising this. For me the expected behaviour is to wait until the queue has been populated and implementing the change as described could break other peoples code unexpectedly.

What I would support is adding an additional optional parameter, but since its a getter - which I now see was a poor engineering decision on my part - thats not possible.

Perhaps we could look at deprecating .onComplete in favour of .onDone(returnIfEmpty: true) which gives existing users and packages the chance to opt in or out as desired.

Right now im in the middle of opening up a gin/rum distillery so don't have a lot of time to sit and focus on this, but will come back to it when I get a chance - I will be using queue a fair bit I would think as I am also creating a rasperry pi controller that will be written in dart to control the still, but it will be a couple of months off. So if someone wants to implement it as above, write some tests for and submit a PR it would be gladly accepted, otherwise just need to wait up until i'm back on deck

rknell avatar Oct 28 '21 02:10 rknell

@rknell I can do a pull request for it, but I'm having a trouble to check if queue is empty. I can't get the current value of remainingItems stream. I tried await remainingItems.isEmpty but it isn't correct Do you have any suggestion?

ductranit avatar Oct 28 '21 04:10 ductranit

@ductranit @givip Until we don't have a proper implementation try with:

  Future<void> completeQueue() async {
    unawaited(queue.add(() => Future.value()));
    await queue.onComplete;
  }

Hyla96 avatar Nov 08 '21 15:11 Hyla96