You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
559 B
25 lines
559 B
import 'dart:async';
|
|
|
|
class Debounce {
|
|
static final Map<String, Timer> _timers = {};
|
|
|
|
static void debounce(String tag, Duration duration, Function onExecute) {
|
|
if (duration == Duration.zero) {
|
|
_timers[tag]?.cancel();
|
|
_timers.remove(tag);
|
|
onExecute();
|
|
} else {
|
|
_timers[tag]?.cancel();
|
|
_timers[tag] = Timer(duration, () {
|
|
_timers[tag]?.cancel();
|
|
_timers.remove(tag);
|
|
onExecute();
|
|
});
|
|
}
|
|
}
|
|
|
|
static void cancel(String tag) {
|
|
_timers[tag]?.cancel();
|
|
_timers.remove(tag);
|
|
}
|
|
}
|