wonders/lib/logic/common/debouncer.dart

30 lines
592 B
Dart
Raw Permalink Normal View History

2022-08-29 20:38:28 -06:00
import 'dart:async';
import 'package:flutter/material.dart';
class Debouncer {
Debouncer(this.interval);
final Duration interval;
VoidCallback? _action;
Timer? _timer;
void call(VoidCallback action) {
// Let the latest action override whatever was there before
_action = action;
// Always cancel and restart the timer
_timer?.cancel();
_timer = Timer(interval, _callAction);
}
void _callAction() {
_action?.call(); // If we have an action queued up, complete it.
_timer = null;
}
void reset() {
_action = null;
_timer = null;
}
}