Last updated:
0 purchases
flutter safety time
Features #
[SafetyTime] is a timelock which designed to block multiple calls within an interval. It works like a synchronized mutex ([lock] and [unlock]).
[SafetyTime] provides two cor functions:
The first is to block users from repeated clicks, repeated network requests, etc.For example, in a multi-select list, the user touches multiple options at the same time.As another example, when the network requests, the user clicks again to request.
Specifically, [SafetyTime] will compare the interval between two times,and if it is less than the safe interval [SafetyTime], the event will be discarded.
The second is a pair of methods similar to [lock] and [unlock], they are [tryLockForever] and [unlockForever]. Specifically, [SafetyTime] will lock a indefinitely [key] until the user manually calls [unlockForever] to unlock the lock.
Getting started #
Add dependency #
You can use the command to add flutter_safety_time as a dependency with the latest stable version:
$ dart pub add flutter_safety_time
copied to clipboard
Or you can manually add flutter_safety_time into the dependencies section in your pubspec.yaml:
dependencies:
flutter_safety_time:
copied to clipboard
Usage #
The user taped multiple buttons before the new page was pushed.
onATap: {
if(SafetyTime.unavailable) return;
... ...
Navigator.push(context, PageA());
}
onBTap: {
if(SafetyTime.unavailable) return;
... ...
Navigator.push(context, PageB());
}
copied to clipboard
Limit onece login in 1 minute.
loginRequest() async {
if(SafetyTime.unavailableOf('Login', Duration(minutes: 1))) {
alert('Limit onece login in 1 minute.');
return;
}
await login();
alert('success');
}
copied to clipboard
[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 {
// Make calls to "updateUserInfo" unique within the same time period.
if(SafetyTime.tryLockForever('UpdateUserInfo')) {
await doSomething();
SafetyTime.unlockForever('UpdateUserInfo');
}
}
InPageA {
... ...
updateUserInfo(); // Called at any time
... ...
}
InPageB {
... ...
updateUserInfo(); // Called at any time
... ...
}
copied to clipboard
Synchronous key
Executes [computation] when lock is available.
Only one asynchronous block can run while the key is retained.
If [timeout] is specified, it will try to grab the lock and will not call the computation callback and throw a [TimeoutExpection] is the lock cannot be grabbed in the given duration.
save(x) {
await SafetyTime.synchronizedKey('WritToFile', (userInfo) async {
writToFile(x);
});
}
InPageA {
... ...
save(A); // Called at any time
... ...
}
InPageB {
... ...
save(B); // Called at any time
... ...
}
copied to clipboard
Additional information #
Click 'Star👍' to bookmark this library, which you can find in your profile.
Github
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.