Files
MWClash/lib/common/function.dart

34 lines
569 B
Dart
Raw Normal View History

2024-04-30 23:38:49 +08:00
import 'dart:async';
class Debouncer {
Map<dynamic, Timer> operators = {};
2024-04-30 23:38:49 +08:00
call(
dynamic tag,
Function func, {
List<dynamic>? args,
Duration duration = const Duration(milliseconds: 600),
}) {
final timer = operators[tag];
if (timer != null) {
timer.cancel();
}
operators[tag] = Timer(
duration,
() {
operators.remove(tag);
Function.apply(
func,
args,
);
},
);
}
2024-04-30 23:38:49 +08:00
cancel(dynamic tag) {
operators[tag]?.cancel();
2024-04-30 23:38:49 +08:00
}
}
final debouncer = Debouncer();