block method
Polls until the lock is acquired or the timeout is reached.
If callback is provided, it runs while this lock is held and the lock
is released when the callback completes or throws. Throws a
LockTimeoutException if the lock cannot be acquired within seconds.
Polling uses CacheLock.sleepMilliseconds between attempts.
Implementation
@override
Future<dynamic> block(int seconds, [Function? callback]) async {
final starting = DateTime.now().millisecondsSinceEpoch;
final milliseconds = seconds * 1000;
while (!await acquire()) {
final now = DateTime.now().millisecondsSinceEpoch;
if ((now + super.sleepMilliseconds - milliseconds) >= starting) {
throw LockTimeoutException('Lock timeout');
}
await Future<void>.delayed(
Duration(milliseconds: super.sleepMilliseconds),
);
}
if (callback != null) {
try {
return await Function.apply(callback, const <dynamic>[]);
} finally {
await release();
}
}
return true;
}