added throttle.

This commit is contained in:
Livinglist
2022-02-15 13:21:02 -08:00
parent 6910a66448
commit 9ca7cceb91
4 changed files with 47 additions and 15 deletions

View File

@ -76,6 +76,7 @@ class _StoryScreenState extends State<StoryScreen> {
initialRefreshStatus: RefreshStatus.refreshing,
);
final focusNode = FocusNode();
final throttle = Throttle(delay: const Duration(seconds: 2));
final sadFaces = <String>[
'ಥ_ಥ',
'(╯°□°)╯︵ ┻━┻',
@ -124,6 +125,7 @@ class _StoryScreenState extends State<StoryScreen> {
refreshController.dispose();
commentEditingController.dispose();
scrollController.dispose();
throttle.dispose();
super.dispose();
}
@ -423,20 +425,23 @@ class _StoryScreenState extends State<StoryScreen> {
final match = regex.stringMatch(link) ?? '';
final id = int.tryParse(match);
if (id != null) {
locator
.get<StoriesRepository>()
.fetchParentStory(id: id)
.then((story) {
if (mounted) {
if (story != null) {
HackiApp.navigatorKey.currentState!
.pushNamed(
StoryScreen.routeName,
arguments:
StoryScreenArgs(story: story),
);
} else {}
}
throttle.run(() {
locator
.get<StoriesRepository>()
.fetchParentStory(id: id)
.then((story) {
if (mounted) {
if (story != null) {
HackiApp
.navigatorKey.currentState!
.pushNamed(
StoryScreen.routeName,
arguments: StoryScreenArgs(
story: story),
);
} else {}
}
});
});
} else {
LinkUtil.launchUrl(link);

View File

@ -1,5 +1,6 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
class Debouncer {
Debouncer({

25
lib/utils/throttle.dart Normal file
View File

@ -0,0 +1,25 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
class Throttle {
Throttle({
required this.delay,
});
final Duration delay;
Timer? _timer;
bool _canInvoke = true;
void run(VoidCallback action) {
if (_canInvoke) {
action();
_canInvoke = false;
_timer = Timer(delay, () {
_canInvoke = true;
});
}
}
void dispose() => _timer?.cancel();
}

View File

@ -2,3 +2,4 @@ export 'debouncer.dart';
export 'html_util.dart';
export 'link_util.dart';
export 'service_exception.dart';
export 'throttle.dart';