Git Product home page Git Product logo

Comments (4)

rknell avatar rknell commented on August 19, 2024 2

hey @hacker1024 I've pushed a few new versions out with a "fix" for what you are asking.

In order to not make breaking changes to the library, but also to give more flexibility to the lifecycle, I ultimately ended up improving the way dispose works and adding a new getter - onComplete.

Now if you call dispose, instead of just cleaning up a few resources, it will cancel any outstanding futures in the queue as well as lock the object so you can't any any more items.

If you want to ensure all items are complete before disposing, you can call Queue.onComplete first and then Queue.dispose - giving you both the ability to either wait for queue completion or to exit early if you want to. I may add an option parameter to dispose first, something like Queue.dispose({waitUntilComplete: true}) to bundle this behaviour together in future versions, if you want to do a PR for that no problems, will happily accept it.

Let me know if you have any questions, i'll close for now but if you have any objections more than happy to open again and explore further.

from dart_queue.

rknell avatar rknell commented on August 19, 2024 1

Hey Mate!

I was going to add a listener or something to the library the other day to check if all items were complete and ultimately decided not to. I am using this massively for a project right now doing integrations where I have queues feeding into queues, and lots of things that might queue up tens of thousands of requests which need to play nicely with other items.

There are two things you can do if you want to wait for it to be done:

If you want to make sure all the operations in one area are complete, add them to a List and then await Future.wait(actions).

Like this:

final queue = Queue();
final actions = <Future>[];
for(var item in collection){
   actions.add(queue.add(item))
}
await Future.wait(actions)

This will wait for everything to complete from the actions list.

If you want to do something when there are no longer any items in the queue, not worrying about where they are, you can subscribe to the remainingItems stream.

So something like this:

final queue = Queue();
queue.remainingItems.listen((itemsRemaining)=>if(itemsRemaining == 0) {//do something})

I might still add a queue complete listener in the future - but I ultimately ended up going with the first example as I had more control over the result.

Its all pseudo code, so ymmv. Let me know if you have any questions.

from dart_queue.

hacker1024 avatar hacker1024 commented on August 19, 2024

Thanks for the suggestion - I ended up doing something similar.
I feel like an implementation this simple would be worth building in to the plugin, though it would make it use slightly more memory.

I can make a pull request, if you'd like.

final _queue = Queue();
final _pending = <Future<void>>[];

void _queueFuture(Future<void> Function() getFuture) async {
  final addedFuture = _queue.add(getFuture);
  _pending.add(addedFuture);
  await addedFuture;
  _pending.remove(addedFuture);
}

Future<void> dispose() async {
  await Future.wait<void>(_pending);
  await _queue.dispose();
}

from dart_queue.

rknell avatar rknell commented on August 19, 2024

I see, you mostly just want to make sure they are completed before you dispose of the queue.

Let me have a think about it. The key is to make sure you don't add anything else as there is already a list of futures that are outstanding, so it could be as simple as adding the await Future.wait(_pending); as part of the dispose function, but might need some more checks.

from dart_queue.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.