Fix bug in debouncer & increase search tab debounce time

Signed-off-by: Neeraj Gupta <254676+ua741@users.noreply.github.com>
This commit is contained in:
Neeraj Gupta 2023-11-28 13:00:35 +05:30
parent 5dd09a79ae
commit f8209ff604
3 changed files with 26 additions and 18 deletions

View file

@ -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 ||

View file

@ -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() {

View file

@ -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 {