mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-02 14:56:20 +01:00
24 lines
671 B
Dart
24 lines
671 B
Dart
import 'package:nc_photos/platform/k.dart' as platform_k;
|
|
import 'package:nc_photos_plugin/nc_photos_plugin.dart' as plugin;
|
|
|
|
class Lock {
|
|
static Future<T> synchronized<T>(int lockId, Future<T> Function() fn) async {
|
|
if (platform_k.isAndroid) {
|
|
return _synchronizedAndroid(lockId, fn);
|
|
} else {
|
|
throw UnimplementedError();
|
|
}
|
|
}
|
|
|
|
static Future<T> _synchronizedAndroid<T>(
|
|
int lockId, Future<T> Function() fn) async {
|
|
while (!await plugin.Lock.tryLock(lockId)) {
|
|
await Future.delayed(const Duration(milliseconds: 50));
|
|
}
|
|
try {
|
|
return await fn();
|
|
} finally {
|
|
await plugin.Lock.unlock(lockId);
|
|
}
|
|
}
|
|
}
|