2022-08-29 20:38:28 -06:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class Throttler {
|
|
|
|
Throttler(this.interval);
|
|
|
|
final Duration interval;
|
|
|
|
|
|
|
|
VoidCallback? _action;
|
|
|
|
Timer? _timer;
|
|
|
|
|
|
|
|
void call(VoidCallback action, {bool immediateCall = true}) {
|
|
|
|
// Let the latest action override whatever was there before
|
|
|
|
_action = action;
|
|
|
|
// If no timer is running, we want to start one
|
|
|
|
if (_timer == null) {
|
|
|
|
// If immediateCall is true, we handle the action now
|
|
|
|
if (immediateCall) {
|
|
|
|
_callAction();
|
|
|
|
}
|
|
|
|
// Start a timer that will temporarily throttle subsequent calls, and eventually make a call to whatever _action is (if anything)
|
|
|
|
_timer = Timer(interval, _callAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _callAction() {
|
|
|
|
_action?.call(); // If we have an action queued up, complete it.
|
2023-09-22 11:26:41 -07:00
|
|
|
_action = null; // Once an action is called, do not call the same action again unless another action is queued.
|
2022-08-29 20:38:28 -06:00
|
|
|
_timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
_action = null;
|
|
|
|
_timer = null;
|
|
|
|
}
|
|
|
|
}
|