twonly-app/lib/src/utils/exclusive_access.dart
otsmr 5204984f9c
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run
fix issue with workmanager
2026-03-15 19:59:29 +01:00

51 lines
1.3 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:mutex/mutex.dart';
import 'package:twonly/globals.dart';
Future<T> exclusiveAccess<T>({
required String lockName,
required Future<T> Function() action,
required Mutex mutex,
}) async {
final lockFile = File('$globalApplicationSupportDirectory/$lockName.lock');
return mutex.protect(() async {
var lockAcquired = false;
while (!lockAcquired) {
try {
lockFile.createSync(exclusive: true);
lockAcquired = true;
} on FileSystemException catch (e) {
final isExists = e is PathExistsException || e.osError?.errorCode == 17;
if (!isExists) {
break;
}
try {
final stat = lockFile.statSync();
if (stat.type != FileSystemEntityType.notFound) {
final age = DateTime.now().difference(stat.modified).inMilliseconds;
if (age > 1000) {
lockFile.deleteSync();
continue;
}
}
} catch (_) {}
await Future.delayed(const Duration(milliseconds: 50));
} catch (_) {
break;
}
}
try {
return await action();
} finally {
if (lockAcquired) {
try {
if (lockFile.existsSync()) {
lockFile.deleteSync();
}
} catch (_) {}
}
}
});
}