Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

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);
  }
}

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.