ente/lib/utils/debouncer.dart

59 lines
1.6 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
import "package:photos/models/typedefs.dart";
class Debouncer {
2022-08-19 09:06:25 +00:00
final Duration _duration;
///in milliseconds
2022-08-22 08:26:01 +00:00
final ValueNotifier<bool> _debounceActiveNotifier = ValueNotifier(false);
2023-11-28 06:57:15 +00:00
/// If executionIntervalInSeconds is not null, then the debouncer will execute the
/// current callback it has in run() method repeatedly in the given interval.
2023-11-28 06:57:15 +00:00
/// This is useful for example when you want to execute a callback every 5 seconds
final int? executionIntervalInMilliSeconds;
2022-09-20 12:13:15 +00:00
Timer? _debounceTimer;
2022-08-22 09:14:52 +00:00
Debouncer(this._duration, {this.executionIntervalInMilliSeconds});
final Stopwatch _stopwatch = Stopwatch();
void run(FutureVoidCallback fn) {
bool shouldRunImmediately = false;
if (executionIntervalInMilliSeconds != null) {
// ensure the stop watch is running
_stopwatch.start();
if (_stopwatch.elapsedMilliseconds > executionIntervalInMilliSeconds!) {
shouldRunImmediately = true;
_stopwatch.stop();
_stopwatch.reset();
}
}
2022-08-22 08:26:01 +00:00
if (isActive()) {
2022-09-20 12:13:15 +00:00
_debounceTimer!.cancel();
}
_debounceTimer =
Timer(shouldRunImmediately ? Duration.zero : _duration, () async {
_stopwatch.stop();
_stopwatch.reset();
2022-08-22 08:26:01 +00:00
await fn();
_debounceActiveNotifier.value = false;
});
_debounceActiveNotifier.value = true;
}
2022-08-22 09:14:52 +00:00
void cancelDebounce() {
2022-08-19 09:16:26 +00:00
if (_debounceTimer != null) {
2022-09-20 12:13:15 +00:00
_debounceTimer!.cancel();
}
}
2022-08-19 09:06:25 +00:00
2022-09-20 12:13:15 +00:00
bool isActive() => _debounceTimer != null && _debounceTimer!.isActive;
2022-08-19 09:16:26 +00:00
2022-08-22 08:26:01 +00:00
ValueNotifier<bool> get debounceActiveNotifier {
return _debounceActiveNotifier;
}
}