AsyncCallQueue is a Dart class which provides a queuing mechanism to prevent concurrent access to asynchronous code.

Overview

async_call_queue

Pub

AsyncCallQueue is a Dart class which provides a queuing mechanism to prevent concurrent access to asynchronous code.

Getting Started

Add this to your app's pubspec.yaml file:

dependencies:
  async_call_queue: ^1.0.0

Usage

Then you have to import the package with:

import 'package:async_call_queue/async_call_queue.dart';

And use AsyncCallQueue where appropriate.

Some examples:

// This is the control. The delayedWrite1234To function writes each
// number in the array [1, 2, 3, 4] to the string buffer with a
// millisecond delay before each write. Calling it two times in a row
// should result in '11223344'.
var buff = StringBuffer();
var f1 = delayedWrite1234To(buff);
var f2 = delayedWrite1234To(buff);
await Future.wait<void>([f1, f2]);
expect(buff.toString(), '11223344');

// This verifies that `queueCall` synchronizes the calls to
// delayedWrite1234To so that the first call finishes before the
// second call is executed, resulting in '12341234'.
var acq = AsyncCallQueue();
buff = StringBuffer();
f1 = acq.queueCall<void>((acq, callId) => delayedWrite1234To(buff));
f2 = acq.queueCall<void>((acq, callId) => delayedWrite1234To(buff));
await Future.wait<void>([f1, f2]);
acq.dispose();
expect(buff.toString(), '12341234');

// This verifies that the first call can cancel some of its work when
// the next call starts waiting. We delay the second call by two
// milliseconds, so the result should be '121234'
acq = AsyncCallQueue();
buff = StringBuffer();
buff = StringBuffer();
f1 = acq.queueCall<void>((acq, callId) async {
    for (final value in [1, 2, 3, 4]) {
      await _delayedWriteTo(buff, value);
      if (acq.hasCallsWaitingAfter(callId)) return;
    }
});
await Future<void>.delayed(twoMilliseconds);
f2 = acq.queueCall<void>((acq, callId) => delayedWrite1234To(buff));
await Future.wait<void>([f1, f2]);
acq.dispose();
expect(buff.toString(), '121234');

// In this example, only the last call should complete because
// we're making a new call every millisecond and the delay is
// 5 milliseconds.
final buff = StringBuffer();
final acq = AsyncCallQueue();
final completer = Completer<void>();
for (var c = 0; c < 10; c++) {
    await Future<void>.delayed(oneMillisecond);
    acq.delayCall((acq, callId) {
    buff.write(callId);
    completer.complete();
    }, delay: fiveMilliseconds);
}
await completer.future;
acq.dispose();
expect(buff.toString(), '10');

/// Writes the [value] to the [buff] after the [delay].
Future _delayedWriteTo(
  StringBuffer buff,
  int value, {
  Duration delay = oneMillisecond,
}) async {
  await Future<void>.delayed(delay);
  buff.write(value);
}

/// Writes the values 1, 2, 3, 4 to the [buff] with a [delay] before each
/// number is written.
Future delayedWrite1234To(
  StringBuffer buff, {
  Duration delay = oneMillisecond,
}) async {
  for (final value in [1, 2, 3, 4]) {
    await _delayedWriteTo(buff, value, delay: delay);
  }
}
You might also like...

A flutter plugin that provides external storage path and external public storage path

ext_storage ext_storage is minimal flutter plugin that provides external storage path and external public storage path

Nov 16, 2021

A flutter package provides controllers and editors for complex models and lists

A flutter package provides controllers and editors for complex models and lists

This package provides controllers and editors for complex models and lists and is inspired by simplicity of TextEditingController. It encapsulates sta

Sep 1, 2022

🚀The Flutter dart code generator from zeplin. ex) Container, Text, Color, TextStyle, ... - Save your time.

🚀The Flutter dart code generator from zeplin. ex) Container, Text, Color, TextStyle, ... - Save your time.

Flutter Gen Zeplin Extension 🚀 The Flutter dart code generator from zeplin. ex) Container, Text, Color, TextStyle, ... - Save your time. ⬇ 1.1k Getti

Oct 12, 2022

A Dart package to web scraping data from websites easily and faster using less code lines.

Chaleno A flutter package to webscraping data from websites This package contains a set of high-level functions that make it easy to webscrap websites

Dec 29, 2022

Basic Dart reverse shell code

Basic Dart reverse shell code

dart_rs Basic Dart reverse shell based on this one by Potato-Industries. Pretty self explanatory. You’ll need Windows. I used a Windows 7 64-bit VM. F

Oct 2, 2022

Dart Code Generator for generating mapper classes

Smartstruct - Dart bean mappings - the easy nullsafe way! Code generator for generating type-safe mappers in dart, inspired by https://mapstruct.org/

Nov 29, 2022

A Dart testing utility for asserting that some code emits a compilation error.

A Dart testing utility for asserting that some code emits a compilation error.

Dec 11, 2022

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. There is no way to get the package version from the code in the Dart ecosystem. Installation Add bu

Dec 14, 2022

A flutter plugin for execute dart code in background.

flutter_background_service A flutter plugin for execute dart code in background. Android No additional setting required. To change notification icon,

Dec 27, 2022
Owner
Ron Booth
Ron Booth
A simple Flutter / Dart Utility class for converting complex objects to uri and query string

A simple Flutter / Dart Utility class for converting complex or nested objects to uri and query strings you can follow the the article on how this cla

Opata Joshua 5 Sep 7, 2022
Automatically generate usecase classes from your repository class definition in Dart and Flutter

Repo Case Automatically generate usecase classes from your repository class definition in Dart and Flutter. Check out the official guide on repo_case

Sandro Maglione 5 Jul 30, 2022
Provides API to generate Dart source code

DartWriter DartWriter provides API to generate Dart source code. It can make your job easier while developing flutter/dart tools. You can also generat

Ahmet ÇELİK 11 Oct 24, 2022
A generator to create config class from json files that support many environments

A generator to create config class from json files that support many environments. Motivation If you use a json file to config your applications, perp

Diego Cardenas 0 Oct 9, 2021
A builder that generates an ArgsParser from a class

Parse command line arguments directly into an annotation class using the Dart Build System. Example Annotate a class with @CliOptions() from package:b

Kevin Moore 43 Oct 30, 2022
This is a simple class to package up and send requests to the Kroki.io web service.

Kroki.dart This is a simple class to package up and send requests to the Kroki.io web service. A live editor to editing diagrams that Kroki supports c

Tim Maffett 1 Jun 8, 2022
VS Code `.code-workspace` file generator

VS Code .code-workspace file generator (for monorepositories with Dart and Flutter projects) TL;DR; Create yaml file config.yaml (check #Format sectio

Mike T 1 Feb 18, 2022
Provides Dart Build System builder for creating Injection pattern using annotations.

Provides Dart Build System builder for creating Injection pattern using annotations. Gate generator The core package providing generators using annoat

Apparence.io 17 Dec 20, 2022
Provides simple conversion between Dart classes and Protobuf / Fixnum classes used in gRPC.

grpc_protobuf_convert Provides simple conversion between Dart classes and Protobuf / Fixnum classes used in gRPC. Using the library Add the repo to yo

null 2 Nov 1, 2022
Provides null-safety implementation to simplify JSON data handling by adding extension method to JSON object

Lazy JSON Provides null-safety implementation to simplify JSON data handling by adding extension method to JSON object and JSON array. Getting started

Kinnara Digital Studio 0 Oct 27, 2021