mirror of
https://github.com/Livinglist/Hacki.git
synced 2025-08-24 10:04:11 +08:00
added throttle.
This commit is contained in:
@ -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);
|
||||
|
@ -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
25
lib/utils/throttle.dart
Normal 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();
|
||||
}
|
@ -2,3 +2,4 @@ export 'debouncer.dart';
|
||||
export 'html_util.dart';
|
||||
export 'link_util.dart';
|
||||
export 'service_exception.dart';
|
||||
export 'throttle.dart';
|
||||
|
Reference in New Issue
Block a user