Make Lock support running in unit tests

This commit is contained in:
Ming Ming 2022-07-08 16:38:09 +08:00
parent 715bfaa0ee
commit 310fa424d6
2 changed files with 10 additions and 0 deletions

View file

@ -1,10 +1,13 @@
import 'package:nc_photos/platform/k.dart' as platform_k;
import 'package:nc_photos/web/lock.dart' as web;
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 if (platform_k.isDesktop) {
return _synchronizedDesktop(lockId, fn);
} else {
throw UnimplementedError();
}
@ -21,4 +24,9 @@ class Lock {
await plugin.Lock.unlock(lockId);
}
}
// this is mainly used to run test cases
static Future<T> _synchronizedDesktop<T>(
int lockId, Future<T> Function() fn) =>
web.Lock.synchronized(lockId, fn);
}

View file

@ -6,3 +6,5 @@ const isWeb = kIsWeb;
// Platform n/a on web, need checking kIsWeb first
final isMobile = !kIsWeb && (Platform.isAndroid || Platform.isIOS);
final isAndroid = !kIsWeb && Platform.isAndroid;
final isDesktop =
!kIsWeb && (Platform.isLinux || Platform.isMacOS || Platform.isWindows);