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

outdated

Lock mechanism to prevent concurrent access to asynchronous code.

example/main.dart

import 'dart:async';
import 'dart:math';

import 'package:synchronized_call/synchronized_call.dart';

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

  Future<int> doWrite() async {
    await writeBatch([1, 2, 3, 4, 5]);
    print(' ');
    return 0;
    // int returnValue = Random().nextInt(10) + 1;
    // print('return value: $returnValue');
    // return returnValue;
  }

  () async {
    int _count = 5;

    ///
    print('>>>>>>>>> Not in sequence');
    for (int i = 0; i < _count; i++) {
      doWrite();
    }
    await Future.delayed(Duration(seconds: 2));

    ///
    print('>>>>>>>>> Start test async');
    CallLock lock = CallLock.create();
    for (int i = 0; i < _count; i++) {
      lock.call(doWrite);
    }
    lock.addListener(() {
      print('------------->>>>>>> DONE ASYNC');
    });
    await Future.delayed(Duration(seconds: 2));

    ///
    print('>>>>>>>>> Start test sync');
    CallLock syncLock = CallLock.create(isSync: true);
    for (int i = 0; i < _count; i++) {
      syncLock.call(doWrite);
    }
    syncLock.addListener(() {
      print('------------->>>>>>> DONE SYNC');
    });
    await Future.delayed(Duration(seconds: 2));

    ///
    print('>>>>>>>>> Start Test with name async ~~~');
    for (int i = 0; i < _count; i++) {
      CallLock.get('__async_test__').call(doWrite);
    }
    await Future.delayed(Duration(seconds: 2));

    ///
    print('>>>>>>>>> Start Test with name sync ~~~');
    CallLock.set('__sync_lock__', CallLock.create(isSync: true));
    for (int i = 0; i < _count; i++) {
      CallLock.get('__sync_lock__').call(doWrite);
    }
    await Future.delayed(Duration(seconds: 2));

    // ///
    // print('>>>>>>>>> Start Test inclusive lock ~~~');
    // InclusiveLock inclusiveLock = InclusiveLock();
    // for (int i = 0; i < _count; i++) {
    //   print('caller request a new task to execute $i ~~~');
    //   FutureOr futureOr = inclusiveLock.call(doWrite);
    //   if (futureOr is Future) {
    //     futureOr.then((value) {
    //       print('---->>>>>>>>> Got the return value: $value');
    //     });
    //   }
    // }
    // await Future.delayed(Duration(seconds: 2));
  }();
}
9
likes
0
points
8
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