lockRelease method

dynamic lockRelease(
  1. String name, {
  2. required Function perform,
  3. bool shouldSetState = true,
})

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

Implementation

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());
  }

  _updateLockState(shouldSetState: shouldSetState, name: name, value: false);
}