change interval time type from int to Duration in debouncer

This commit is contained in:
ashilkn 2023-11-30 19:29:27 +05:30
parent 8e8ac25a66
commit 6a26e01af0
3 changed files with 6 additions and 6 deletions

View file

@ -38,7 +38,7 @@ class _AllSectionsExamplesProviderState
final _debouncer = Debouncer( final _debouncer = Debouncer(
const Duration(seconds: 4), const Duration(seconds: 4),
executionIntervalInMilliSeconds: 15000, executionInterval: const Duration(seconds: 15),
); );
@override @override

View file

@ -42,7 +42,7 @@ class _MapViewState extends State<MapView> {
late List<Marker> _markers; late List<Marker> _markers;
final _debouncer = Debouncer( final _debouncer = Debouncer(
const Duration(milliseconds: 300), const Duration(milliseconds: 300),
executionIntervalInMilliSeconds: 750, executionInterval: const Duration(milliseconds: 750),
); );
@override @override

View file

@ -12,19 +12,19 @@ class Debouncer {
/// If executionIntervalInSeconds is not null, then the debouncer will execute the /// If executionIntervalInSeconds is not null, then the debouncer will execute the
/// current callback it has in run() method repeatedly in the given interval. /// current callback it has in run() method repeatedly in the given interval.
/// This is useful for example when you want to execute a callback every 5 seconds /// This is useful for example when you want to execute a callback every 5 seconds
final int? executionIntervalInMilliSeconds; final Duration? executionInterval;
Timer? _debounceTimer; Timer? _debounceTimer;
Debouncer(this._duration, {this.executionIntervalInMilliSeconds}); Debouncer(this._duration, {this.executionInterval});
final Stopwatch _stopwatch = Stopwatch(); final Stopwatch _stopwatch = Stopwatch();
void run(FutureVoidCallback fn) { void run(FutureVoidCallback fn) {
bool shouldRunImmediately = false; bool shouldRunImmediately = false;
if (executionIntervalInMilliSeconds != null) { if (executionInterval != null) {
// ensure the stop watch is running // ensure the stop watch is running
_stopwatch.start(); _stopwatch.start();
if (_stopwatch.elapsedMilliseconds > executionIntervalInMilliSeconds!) { if (_stopwatch.elapsedMilliseconds > executionInterval!.inMilliseconds) {
shouldRunImmediately = true; shouldRunImmediately = true;
_stopwatch.stop(); _stopwatch.stop();
_stopwatch.reset(); _stopwatch.reset();