Flutter同步调用插件synchronized_call的使用
Flutter同步调用插件synchronized_call的使用
synchronized_call 是一个用于在Flutter中同步调用异步函数的插件。它能够防止并发访问异步代码,节流和去抖动异步函数调用,并支持添加监听器以观察所有异步函数或BLoC是否已完成。
功能特点
- 防止对异步代码的并发访问。
- 节流和去抖动异步函数调用。
- 支持添加监听器以观察所有异步函数/BLoC是否已完成。
示例
基本用法
考虑以下异步函数doWrite
:
import 'package:synchronized_call/synchronized_call.dart';
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(' ');
}
直接调用三次doWrite
:
doWrite();
doWrite();
doWrite();
/// 输出:'111222333444555'
/// 期望输出:'12345 12345 12345'
使用synchronized_call
包中的CallLock
:
BaseLock lock = CallLock.create();
lock.call(doWrite);
lock.call(doWrite);
lock.call(doWrite);
/// 输出将是期望的:'12345 12345 12345'
想要在所有BLoC执行完毕后接收回调:
lock.addListener(() {
print('All bloc are done executed.');
});
另一种用法
将异步代码/BLoC放在await lock()
和unlock()
方法之间:
Locker lock = Locker();
void do() async {
await lock.lock();
/// ...
/// 其他异步或同步代码在这里...
/// ...
await doWrite();
lock.unlock();
}
do();
do();
do();
/// 输出将是期望的:'12345 12345 12345'
完整示例Demo
以下是一个完整的示例程序,演示了如何使用不同的锁机制来确保异步函数按顺序执行:
import 'dart:async';
import 'dart:io';
import 'package:synchronized_call/synchronized_call.dart';
Future writeBatch(List<int> indexes) async {
for (var i in indexes) {
await Future.delayed(Duration(milliseconds: 100));
stdout.write('$i');
}
}
Future<int> doWrite() async {
await writeBatch([1, 2, 3, 4, 5]);
print('');
return 0;
}
/// 测试 [SerialLock] 和 [SyncLock]
void main() async {
int _count = 5;
///
print('>>>>>>>>>> Not in sequence');
for (int i = 0; i < _count; i++) {
doWrite();
}
await Future.delayed(Duration(seconds: 3));
///
print('>>>>>>>>>> Start [SerialLock] async test');
BaseLock lock = CallLock.create();
for (int i = 0; i < _count; i++) {
lock.call(doWrite);
}
lock.addListener(() {
print('>>>>>>>>>> Done [SerialLock] async test\n');
});
await Future.delayed(Duration(seconds: 3));
///
print('>>>>>>>>>> Start [SerialLock] sync test, using Completer.sync()');
BaseLock syncLock = CallLock.create(isSync: true);
for (int i = 0; i < _count; i++) {
syncLock.call(doWrite);
}
syncLock.addListener(() {
print('>>>>>>>>>> Done [SerialLock] sync test\n');
});
await Future.delayed(Duration(seconds: 3));
///
print('>>>>>>>>>> Start [SerialLock] test with name ~~~');
for (int i = 0; i < _count; i++) {
CallLock.get('__async_test__').call(doWrite);
}
CallLock.get('__async_test__').addListener(() {
print('>>>>>>>>>> Done [SerialLock] test with name ~~~\n');
});
await Future.delayed(Duration(seconds: 3));
///
print('>>>>>>>>>> Start [SyncLock] async test');
CallLock.set('__async_lock__', SyncLock());
for (int i = 0; i < _count; i++) {
CallLock.get('__async_lock__').call(doWrite);
}
CallLock.get('__async_lock__').addListener(() {
print('>>>>>>>>>> Done [SyncLock] async test\n');
});
await Future.delayed(Duration(seconds: 3));
///
print('>>>>>>>>>> Start [SyncLock] sync test, using Completer.sync()');
CallLock.set('__sync_lock__', SyncLock(isSync: true));
for (int i = 0; i < _count; i++) {
CallLock.get('__sync_lock__').call(doWrite);
}
CallLock.get('__sync_lock__').addListener(() {
print('>>>>>>>>>> Done [SyncLock] sync test\n');
});
await Future.delayed(Duration(seconds: 3));
}
通过上述示例,你可以看到如何使用synchronized_call
包来确保异步函数按顺序执行,并且可以添加监听器来观察所有异步操作是否完成。
更多关于Flutter同步调用插件synchronized_call的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复