tryLockForever static method
SafetyTime.tryLockForever can lock key for a long time.
When key is locked, both unavailable and unavailableOf return true,
but synchronizedKey is not affected.
updateUserInfo() async {
if(SafetyTime.tryLockForever('UpdateUserInfo')) {
await doSomething();
SafetyTime.unlockForever('UpdateUserInfo');
}
}
InPageA { updateUserInfo(); }
InPageB { updateUserInfo(); }
timeout allows you to remove the current tryLockForever after some time
also see: SafetyTime.unlockForever
Implementation
static bool tryLockForever(dynamic key, {Duration? timeout}) {
var state = SafetyTime()._stateMap[key];
if (state == null) {
state = SafetyTimeState(date: DateTime.now());
SafetyTime()._stateMap[key] = state;
}
if (!state._lock) {
if (null != timeout) {
Future.delayed(timeout).then((value) {
unlockForever(key);
});
}
state._lock = true;
return true;
}
return false;
}