24 lines
438 B
Dart
24 lines
438 B
Dart
|
|
import 'dart:async';
|
||
|
|
|
||
|
|
import 'package:flutter/widgets.dart';
|
||
|
|
|
||
|
|
class DebounceThrottleTool<T> {
|
||
|
|
DebounceThrottleTool(this._debounceTime, this._callback);
|
||
|
|
|
||
|
|
Timer? _timer;
|
||
|
|
final Duration _debounceTime;
|
||
|
|
final void Function(T param) _callback;
|
||
|
|
|
||
|
|
void trigger(T param) {
|
||
|
|
_timer?.cancel();
|
||
|
|
_timer = Timer(_debounceTime, () {
|
||
|
|
_callback(param);
|
||
|
|
_timer = null;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
void cancel() {
|
||
|
|
_timer?.cancel();
|
||
|
|
}
|
||
|
|
}
|