synchronized_call 1.0.2 copy "synchronized_call: ^1.0.2" to clipboard
synchronized_call: ^1.0.2 copied to clipboard

outdated

Lock mechanism to prevent concurrent access to asynchronous code.

synchronized_call #

pub package

Feature #

Synchronized mechanism for async function calls

  • Prevent concurrent access to the asynchronous code
  • Throttle and debounce calls for asynchronous function
  • Pure Dart language implementation, no other dependencies

Inspired by synchronized package, but it eliminates the disadvantage of creating too many Completer at once, and supports observers to listen when all blocs were done executed.

If you are able to get all Future, recommend to use Future.forEach(in turn) or Future.wait (order not guaranteed).

Example #

Consider the following dummy code

Future writeBatch(List<int> indexes) async {
  for (var i in indexes) {
    await Future.delayed(Duration(microseconds: 1));
    print('$i');
  }
}

void doWrite() async {
  await writeBatch([1, 2, 3, 4, 5]);
  print(' ');
}

Doing

doWrite();
doWrite();
doWrite();

/// will print: '111222333444555'
/// but we expect: '123451234512345'

So using the CallLock in synchronized_call package:

import 'package:synchronized_call/synchronized_call.dart';
CallLock lock = CallLock.create();

lock.call(doWrite);
lock.call(doWrite);
lock.call(doWrite);

/// now the output will be: '123451234512345'

Want to receive a callback when all bloc invoked in queue were done:

lock.addListener(() {
  print('All bloc are done executed.');
});
Except for SerialLock and SyncLock, a extra lock called InclusiveLock provides functionality that execute only head-to-tail bloc tasks, please feel free to use :)

Features and bugs #

Please feel free to: request new features and bugs at the issue tracker

9
likes
0
points
72
downloads

Publisher

unverified uploader

Weekly Downloads

Lock mechanism to prevent concurrent access to asynchronous code.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on synchronized_call