withLock function

Future<bool> withLock(
  1. String versionPath,
  2. String lockFilePath,
  3. Future<void> callback()
)

Execute a callback while holding a lock. Returns true if the callback executed, false if lock couldn't be acquired.

Implementation

Future<bool> withLock(
  String versionPath,
  String lockFilePath,
  Future<void> Function() callback,
) async {
  final release = await tryAcquireLock(versionPath, lockFilePath);
  if (release == null) return false;

  try {
    await callback();
    return true;
  } finally {
    release();
  }
}