A library to easily handle sequential queueing of futures in dart.

Related tags

Templates dart_queue
Overview

queue

Build Status

Easily queue futures and await their values.

This library allows you to send a future to central queue. The queue will execute the futures in the order they are queued and once the future is complete it will return its result.

My use case was to rate limit calls to bluetooth devices. There were multiple bluetooth devices connected that may have different commands being sent from lots of areas in the app. The devices were tripping over themselves and not responding. A stream wasn't the appropriate tool as I needed to get the result back. Hence a library was born.

Alternative use cases could be spidering a website, downloading a number of files, or rate limiting calls to an API.

πŸ”₯ πŸ”₯ πŸ”₯ Shameless plug! πŸ”₯ πŸ”₯ πŸ”₯

Want to write server apps in dart like ExpressJS? Check out my new open source plugin Alfred https://pub.dev/packages/alfred

Usage

The most simple example:

import 'package:dart_queue/dart_queue.dart';

main() async {
  final queue = Queue();

  //Queue up a future and await its result
  final result = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

  //Thats it!
}

A proof of concept:

import 'package:dart_queue/dart_queue.dart';

main() async {
  //Create the queue container
  final Queue queue = Queue(delay: Duration(milliseconds: 10));
  
  //Add items to the queue asyncroniously
  queue.add(()=>Future.delayed(Duration(milliseconds: 100)));
  queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
  
  //Get a result from the future in line with await
  final result = await queue.add(() async {
    await Future.delayed(Duration(milliseconds: 1));
    return "Future Complete";
  });
  
  //100, 10, 1 will reslove in that order.
  result == "Future Complete"; //true
}

Parallel processing

This doesn't work in batches and will fire the next item as soon as as there is space in the queue Use [Queue(delayed: ...)] to specify a delay before firing the next item

import 'package:dart_queue/dart_queue.dart';

main() async {
  final queue = Queue(parallel: 2);

  //Queue up a future and await its result
  final result1 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
  final result2 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

  //Thats it!
}

On complete

import 'package:dart_queue/dart_queue.dart';

main() async {
  final queue = Queue(parallel: 2);

  //Queue up a couple of futures
  queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
  queue.add(()=>Future.delayed(Duration(milliseconds: 10)));


  // Will only resolve when all the queue items have resolved.
  await queue.onComplete;
}

Rate limiting

You can specify a delay before the next item is fired as per the following example:

import 'package:dart_queue/dart_queue.dart';

main() async {
  final queue = Queue(delay: Duration(milliseconds: 500)); // Set the delay here

  //Queue up a future and await its result
  final result1 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
  final result2 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

  //Thats it!
}

Cancel

If you need to stop a queue from processing call Queue.cancel();

This will cancel the remaining items in the queue by throwing a QueueCancelledException. A cancelled queue is "dead" and should be recreated. If you try adding items to the queue after you call cancel, it will throw a QueueCancelledException.

If you have no reason to listen to the results of the items, simply call dispose.

If you want to wait until all the items which are inflight complete, call Queue.onComplete first.

Disposing

If you need to dispose of the queue object (best practice in flutter any any time the queue object will close) simply call queue.dispose();

This is necessary to close the Queue.remainingItems controller.

Reporting

If you want to query how many items are outstanding in the queue, listen to the Queue.remainingItems stream.

import 'package:dart_queue/dart_queue.dart';
final queue = Queue();

final remainingItemsStream = queue.remainingItems.listen((numberOfItems)=>print(numberOfItems));

//Queue up a couple of futures
queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

// Will only resolve when all the queue items have resolved.
await queue.onComplete;
remainingItemsStream.close();
queue.dispose(); // Will clean up any resources in the queue if you are done with it.

Contributing

Pull requests are welcome. There is a shell script ci_checks.sh that will run the checks to get past CI and also format the code before committing. If that all passes your PR will likely be accepted.

Please write tests to cover your new feature.

Comments
  • Wait for all futures before disposing?

    Wait for all futures before disposing?

    Hi, thanks for this useful plugin!

    Would it be possible to add a way to wait on all futures before disposing? I'm using this to queue writes to a file, so it'd be quite helpful as it's important data that needs to be written.

    opened by hacker1024 4
  • identifier to each queue so that the same algorithm is rerun while in process

    identifier to each queue so that the same algorithm is rerun while in process

    Hello, I am implementing this library with the video thumbnail, but I have a problem, which is that sometimes the same element is called 2 times, causing an unnecessary queue to be generated, so I would like for example:

    opened by kevin4dhd 2
  • In readme file Import path is incorrect.

    In readme file Import path is incorrect.

    I am getting error if I user import path as

    import "package:dart_queue/dart_queue.dart";
    

    It is resolved by keeping path as

    import "package:queue/queue.dart";
    
    opened by suneeltechnew 1
  • Is this package still maintained?

    Is this package still maintained?

    Hi @rknell, what is the status of the package? Are you actively maintaining it, looking for contributors or something else?

    I noticed that there was no activity in the last months, should we guess that the gin distillery is taking more time than expected? Let us know what kind of support you need here, if any, and/or what is the plan for this package.

    All the best πŸ‘‹

    opened by lucaspal 1
  • Queue swallows StackTraces

    Queue swallows StackTraces

    The queue ignores the stackTrace and doesn't pass it to the completer.

    https://github.com/rknell/dart_queue/blob/77ab85956277434a670c6a413fb067b1a9f0fee4/lib/src/dart_queue_base.dart#L36-L38

    opened by Bungeefan 0
  • Add generic types + hide implementation

    Add generic types + hide implementation

    Hi,

    Thanks for creating this package, I find it very useful :) I edited your code a little, I thought I could make this PR.

    This PR:

    • Adds generic types. I think they're very necessary for type inference, particularly for the add method.
    • Hides the implementation. In particular, it makes private the following fields: nextCycle, isCancelled, isProcessing. Keeping them public could break the logic if they're modified. It also makes the process function private, I thought it would be better, what do you think?
    • Adds some documentation for public functions and fields.
    opened by axel-op 0
  • Queue.onComplete not return when queue is empty

    Queue.onComplete not return when queue is empty

    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.

    opened by ductranit 4
  • Specify the maximum size

    Specify the maximum size

    In combination with #6, a way to limit the queue in size would be helpful as well.

    This may be trickier to achieve if items are added from both sides.

    opened by ened 0
  • Feature/priorities

    Feature/priorities

    We needed the option to enqueue tasks with priorities. This is a first shot at it. Let me know what you think.

    • Also fixes obsolete imports in the README.

    First step towards https://github.com/rknell/dart_queue/issues/3


    And one question: Some tests were running very flaky on my machine, namely it should handle an error correctly (also testing oncomplete) fails from time to time with this error message:

    00:32 +5 -1: Queue it should handle an error correctly (also testing oncomplete) [E]                                                                             
      TimeoutException after 0:00:30.000000: Test timed out after 30 seconds. See https://pub.dev/packages/test#timeouts
      dart:isolate  _RawReceivePortImpl._handleMessage
    

    Any idea? I am on latest macOS 11.2.1 and this is my flutter doctor:

    [βœ“] Flutter (Channel stable, 2.0.1, on macOS 11.2.1 20D74 darwin-x64, locale en-GB)
    [βœ“] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    [βœ“] Xcode - develop for iOS and macOS
    [βœ—] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
        ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
    [βœ“] Android Studio (version 4.1)
    [βœ“] VS Code (version 1.54.1)
    [βœ“] Connected device (2 available)
    
    opened by hffmnn 4
  • Inserting item at a specific position in the queue

    Inserting item at a specific position in the queue

    If i for example have five items remaining in the queue is there any way to insert a new future into for example a second position? And thanks for the package, it's really useful!

    opened by logic-and-math 2
Owner
Ryan Knell
Ryan Knell
A most easily usable cookie management library in Dart. With SweetCookieJar, you can easily manage cookie on your application.

A most easily usable cookie management library in Dart! 1. About 1.1. Introduction 1.1.1. Install Library 1.1.2. Import It 1.1.3. Use SweetCookieJar 1

Kato Shinya 9 Oct 27, 2022
A most easily usable cache management library in Dart. With CacheStorage, you can easily manage cache on your application.

A most easily usable cache management library in Dart! 1. About 1.1. Introduction 1.1.1. Install Library 1.1.2. Import It 1.1.3. Use CacheStorage 1.2.

Kato Shinya 1 Dec 13, 2021
A most easily usable RESAS API wrapper in Dart. With this library, you can easily integrate your application with the RESAS API.

A most easily usable RESAS API wrapper library in Dart! 1. About 1.1. What Is RESAS? 1.2. Introduction 1.2.1. Install Library 1.2.2. Import It 1.2.3.

Kato Shinya 2 Apr 7, 2022
A Dart package to handle HTTP services

http_services A package to support the creation of Http services in a Dart application. Features convenient methods to perform HTTP requests disposing

Antonello GalipΓ² 5 Jul 27, 2021
A most easily usable Duolingo API wrapper in Dart. Duolingo4D is an open-sourced Dart library.

A most easily usable Duolingo API wrapper in Dart! 1. About Duolingo4D Duolingo4D is an open-sourced Dart library. With Duolingo4D, you can easily int

Kato Shinya 18 Oct 17, 2022
A package help you to make api call and handle error faster, also you can check for internet before call api.

http_solver ##not for production use, only for learning purpose. A package help you to make api call and handle error faster, also you can check for i

Abdelrahman Saed 1 Jun 18, 2020
SearchBar widget to handle most of search cases

flappy_search_bar A SearchBar widget handling most of search cases. Usage To use this plugin, add flappy_search_bar as a dependency in your pubspec.ya

Smart&Soft 171 Dec 2, 2022
⚑ Cache Manager A tidy utility to handle cache of your flutter app like a Boss.

⚑ Cache Manager A tidy utility to handle cache of your flutter app like a Boss. It provides support for both iOS and Android platforms (offcourse). ??

Abhishek Chavhan 10 Oct 25, 2022
AuthorizationHeader is an open-sourced Dart library. With AuthorizationHeader, you can easily manage authorization header on your application.

A most easily usable authorization header management library in Dart! 1. About 1.1. Supported 1.1.1. Authorization Header 1.1.2. Authorization Type 1.

Kato Shinya 3 Dec 24, 2021
A library for building REST APIs easily with Dart

A library for building REST APIs easily with Dart modeled after Express JS for Node Js. The library is still a work in progress and open to contributi

Albert Oboh 43 Oct 4, 2022
A most easily usable JSON wrapper library in Dart

A most easily usable JSON response wrapper library in Dart! 1. About 1.1. Introd

Kato Shinya 2 Jan 4, 2022
A most easily usable improvement rate calculator library in Dart.

A most easily usable improvement rate calculator library in Dart. With ImprovementRate, you can easily calculate improvement rate on your application.

Kato Shinya 1 Dec 27, 2021
A UI library for easily adding audio waveforms to your apps, with several customization options

A UI library for easily adding audio waveforms to your apps, with several custom

RutvikTak 64 Dec 11, 2022
πŸš€ Simple library to download files easily.

Dart DL Simple library to download files easily. Links GitHub Pub.dev Documentation Features Uses native dart:io to get data. Ability to parse differe

ZYROUGE 2 Feb 11, 2022
Toast Library for Flutter, Easily create toast messages in single line of code

Create Toast messages on a Flutter Event. fluttertoast: ^8.0.8 Toast Library for Flutter, Easily create Personalised toast messages in single line of

Bouziani Mohammed 1 Feb 14, 2022
UI library to easily implement auth functionalities of Supabase in your app.

flutter-auth-ui A simple library of predefined widgets to easily and quickly create a auth compooents using Flutter and Supabase. ⚠️ Developer Preview

Supabase Community 20 Dec 13, 2022
A middleware library for Dart's http library.

http_middleware A middleware library for Dart http library. Getting Started http_middleware is a module that lets you build middleware for Dart's http

TED Consulting 38 Oct 23, 2021
This library provides the easiest and powerful Dart/Flutter library for Mastodon API 🎯

The Easiest and Powerful Dart/Flutter Library for Mastodon API ?? 1. Guide ?? 1.1. Features ?? 1.2. Getting Started ⚑ 1.2.1. Install Library 1.2.2. Im

Mastodon.dart 55 Jul 27, 2023