withLock<T> method

Future<T> withLock<T>(
  1. Future<T> action()
)

Executes action while holding the exclusive lock.

Implementation

Future<T> withLock<T>(Future<T> Function() action) async {
  lockFile.parent.createSync(recursive: true);
  final raf = lockFile.openSync(mode: FileMode.write);
  try {
    await raf.lock(FileLock.exclusive);
    return await action();
  } finally {
    try {
      await raf.unlock();
    } finally {
      await raf.close();
    }
  }
}