withLock<T> static method
Acquires the lock at path, runs action, and releases it afterwards
(even if action throws). See acquire for the parameters.
Implementation
static Future<T> withLock<T>(
String path,
Future<T> Function() action, {
required StaleLockPolicy staleWhen,
Duration timeout = const Duration(minutes: 6),
Duration pollInterval = const Duration(milliseconds: 50),
Duration maxPollInterval = const Duration(seconds: 1),
Duration? heartbeatInterval,
}) async {
final lock = await acquire(
path,
staleWhen: staleWhen,
timeout: timeout,
pollInterval: pollInterval,
maxPollInterval: maxPollInterval,
heartbeatInterval: heartbeatInterval,
);
try {
return await action();
} finally {
await lock.release();
}
}