Comments (4)
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.
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.
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.
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)
- Inserting item at a specific position in the queue HOT 2
- What is "unawaited" used for? HOT 1
- Insert new item in front HOT 1
- Specify the maximum size
- Queue.onComplete not return when queue is empty HOT 4
- identifier to each queue so that the same algorithm is rerun while in process HOT 2
- Is this package still maintained? HOT 1
- In readme file Import path is incorrect. HOT 1
- Queue swallows StackTraces
- how to clear Queue HOT 1
- how can I stop the queue better? HOT 2
- The remainingItems stream is not returning any value
- don't remove from queue if future response has error
- [FeatureRequest] Expose activeItems.length HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart_queue.