Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue.onComplete not return when queue is empty #8

Open
ductranit opened this issue Oct 5, 2021 · 4 comments
Open

Queue.onComplete not return when queue is empty #8

ductranit opened this issue Oct 5, 2021 · 4 comments

Comments

@ductranit
Copy link

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.

@givip
Copy link

givip commented Oct 21, 2021

I'm experiencing this as well

@rknell
Copy link
Owner

rknell commented Oct 28, 2021

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

@ductranit
Copy link
Author

ductranit commented Oct 28, 2021

@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?

@Hyla96
Copy link

Hyla96 commented Nov 8, 2021

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants