Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix bug in debouncer & increase search tab debounce time

Neeraj Guptaf8209ff60433
diff --git a/lib/states/all_sections_examples_state.dart b/lib/states/all_sections_examples_state.dart
index 8cccb7132c..5a15e06865 100644
--- a/lib/states/all_sections_examples_state.dart
+++ b/lib/states/all_sections_examples_state.dart
@@ -31,8 +31,11 @@ class _AllSectionsExamplesProviderState
late StreamSubscription<FilesUpdatedEvent> _filesUpdatedEvent;
final _logger = Logger("AllSectionsExamplesProvider");
- final _debouncer =
- Debouncer(const Duration(seconds: 3), executionIntervalInSeconds: 6000);
+ final _debouncer = Debouncer(
+ const Duration(seconds: 5),
+ executionIntervalInMilliSeconds: 15000,
+ );
+
@override
void initState() {
super.initState();
@@ -44,9 +47,10 @@ class _AllSectionsExamplesProviderState
}
void reloadAllSections() {
+ _logger.info('_debounceTimer: queue timer');
_debouncer.run(() async {
setState(() {
- _logger.info("reloading all sections in search tab");
+ _logger.info("'_debounceTimer: reloading all sections in search tab");
final allSectionsExamples = <Future<List<SearchResult>>>[];
for (SectionType sectionType in SectionType.values) {
if (sectionType == SectionType.face ||
diff --git a/lib/ui/map/map_view.dart b/lib/ui/map/map_view.dart
index c7e4f14758..ce3c1839af 100644
--- a/lib/ui/map/map_view.dart
+++ b/lib/ui/map/map_view.dart
@@ -40,8 +40,10 @@ class MapView extends StatefulWidget {
class _MapViewState extends State<MapView> {
late List<Marker> _markers;
- final _debouncer = Debouncer(const Duration(milliseconds: 300),
- executionIntervalInSeconds: 750);
+ final _debouncer = Debouncer(
+ const Duration(milliseconds: 300),
+ executionIntervalInMilliSeconds: 750,
+ );
@override
void initState() {
diff --git a/lib/utils/debouncer.dart b/lib/utils/debouncer.dart
index 755348042b..afa3b2cdec 100644
--- a/lib/utils/debouncer.dart
+++ b/lib/utils/debouncer.dart
@@ -12,22 +12,32 @@ class Debouncer {
/// If executionIntervalInSeconds is not null, then the debouncer will execute the
/// current callback it has in run() method repeatedly in the given interval.
/// This is useful for example when you want to execute a callback every 5 seconds
- final int? executionIntervalInSeconds;
+ final int? executionIntervalInMilliSeconds;
Timer? _debounceTimer;
- Debouncer(this._duration, {this.executionIntervalInSeconds});
+ Debouncer(this._duration, {this.executionIntervalInMilliSeconds});
final Stopwatch _stopwatch = Stopwatch();
void run(FutureVoidCallback fn) {
- if (executionIntervalInSeconds != null) {
- runCallbackIfIntervalTimeElapses(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();
+ }
}
if (isActive()) {
_debounceTimer!.cancel();
}
- _debounceTimer = Timer(_duration, () async {
+ _debounceTimer =
+ Timer(shouldRunImmediately ? Duration.zero : _duration, () async {
+ _stopwatch.stop();
+ _stopwatch.reset();
await fn();
_debounceActiveNotifier.value = false;
});
@@ -40,14 +50,6 @@ class Debouncer {
}
}
- runCallbackIfIntervalTimeElapses(FutureVoidCallback fn) {
- _stopwatch.isRunning ? null : _stopwatch.start();
- if (_stopwatch.elapsedMilliseconds > executionIntervalInSeconds!) {
- _stopwatch.reset();
- fn();
- }
- }
-
bool isActive() => _debounceTimer != null && _debounceTimer!.isActive;
ValueNotifier<bool> get debounceActiveNotifier {