lockRelease method
The lockRelease method will call the function provided in perform
and then block the function from being called again until it has finished.
E.g. lockRelease('update', perform: () async { await handleSomething(); });
Use isLocked to check if the function is still locked. E.g. isLocked('update') // true/false
The lock is released however perform ends. An Exception it throws is
logged; anything else is rethrown once the lock is released.
Implementation
Future<void> lockRelease(
String name, {
required Function perform,
bool shouldSetState = true,
}) async {
if (isLocked(name) == true) {
return;
}
_updateLockState(shouldSetState: shouldSetState, name: name, value: true);
try {
await perform();
} on Exception catch (e) {
NyLogger.error(e.toString());
} finally {
_updateLockState(
shouldSetState: shouldSetState,
name: name,
value: false,
);
}
}