release_schedule/lib/model/delayed_function_caller.dart

27 lines
660 B
Dart
Raw Normal View History

import 'dart:async';
class DelayedFunctionCaller {
2024-01-08 11:56:35 +01:00
final void Function() function;
final Duration duration;
2024-01-09 13:24:35 +01:00
final bool resetTimerOnCall;
Timer? _timer;
2024-01-09 13:24:35 +01:00
DelayedFunctionCaller(this.function, this.duration,
{this.resetTimerOnCall = false});
get scheduled => _timer != null && _timer!.isActive;
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();
}
// Create a timer that calls the function after the specified duration.
2024-01-08 11:56:35 +01:00
_timer = Timer(duration, function);
}
}