synchronized_call

Creator: coderz1093

Last updated:

0 purchases

TODO
Add to Cart

Description:

synchronized call

synchronized_call #

Feature #
Synchronized mechanism for async function calls. Inspired by synchronized package.

Prevent concurrent access to the asynchronous code
Throttle and debounce calls for asynchronous function
Supports add listener to observe whether all async function/bloc are completed


Differ from Future.forEach(in order) or Future.wait (order not guaranteed), you can use this/synchronized package at the scenario that without having to get all futures at the same time.

Example #
Consider the following async fuction doWrite:
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(' ');
}
copied to clipboard
Calling doWrite 3 times:
doWrite();
doWrite();
doWrite();

/// will print: '111222333444555'
/// but we expect: '12345 12345 12345'
copied to clipboard
Then using the CallLock in synchronized_call package:
import 'package:synchronized_call/synchronized_call.dart';

BaseLock lock = CallLock.create();

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

/// now the output will be you expected: '12345 12345 12345'
copied to clipboard
Want to receive a callback when all bloc were done executed:
lock.addListener(() {
print('All bloc are done executed.');
});
copied to clipboard
Another way to Use #
Put async codes/bloc between await lock() and unlock() methods
Locker lock = Locker();

void do() async {
await lock.lock();

/// ...
/// other async or sync codes here ...
/// ...
await doWrite();

lock.unlock();
}

do();
do();
do();

/// the output will be you expected: '12345 12345 12345'
copied to clipboard
Features and bugs #
Please feel free to:
request new features and bugs at the issue tracker

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.