2023-11-16 13:12:55 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
class DelayedFunctionCaller {
|
2024-01-08 11:56:35 +01:00
|
|
|
final void Function() function;
|
2023-11-16 13:12:55 +01:00
|
|
|
final Duration duration;
|
2024-01-09 13:24:35 +01:00
|
|
|
final bool resetTimerOnCall;
|
2023-11-16 13:12:55 +01:00
|
|
|
Timer? _timer;
|
|
|
|
|
2024-01-09 13:24:35 +01:00
|
|
|
DelayedFunctionCaller(this.function, this.duration,
|
|
|
|
{this.resetTimerOnCall = false});
|
2023-11-16 13:12:55 +01:00
|
|
|
|
2024-01-09 12:47:42 +01:00
|
|
|
get scheduled => _timer != null && _timer!.isActive;
|
|
|
|
|
2023-11-16 13:12:55 +01:00
|
|
|
void call() {
|
|
|
|
if (_timer != null && _timer!.isActive) {
|
2024-01-09 13:24:35 +01:00
|
|
|
// If a timer is already active and we don't want to reset it, return.
|
|
|
|
if (!resetTimerOnCall) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_timer!.cancel();
|
2023-11-16 13:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a timer that calls the function after the specified duration.
|
2024-01-08 11:56:35 +01:00
|
|
|
_timer = Timer(duration, function);
|
2023-11-16 13:12:55 +01:00
|
|
|
}
|
|
|
|
}
|