Skip to content

Commit

Permalink
pref: extension install optimization (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint authored Sep 19, 2023
1 parent 1fb5064 commit 7687260
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/pages/search/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class SearchPageController extends GetxController {
String _randomKey = "";
int get finishCount =>
searchResultList.where((element) => element.completed).length;
bool needRefresh = true;

@override
void onInit() {
getRuntime();
ever(search, (callback) {
_randomKey = DateTime.now().millisecondsSinceEpoch.toString();
getResult(_randomKey);
Expand All @@ -37,6 +37,7 @@ class SearchPageController extends GetxController {
searchResultList.add(SearchResult(runitme: element));
}
getResult(_randomKey);
needRefresh = false;
}

Future<void> getResult(String key) async {
Expand Down
3 changes: 3 additions & 0 deletions lib/pages/search/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class _SearchPageState extends State<SearchPage> {
@override
void initState() {
c = Get.put(SearchPageController());
if (c.needRefresh) {
c.getRuntime();
}
super.initState();
}

Expand Down
11 changes: 9 additions & 2 deletions lib/utils/extension.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:dio/dio.dart';
Expand All @@ -20,6 +22,7 @@ import 'package:path/path.dart' as path;
class ExtensionUtils {
static late Map<String, ExtensionRuntime> runtimes;
static late Map<String, String> extensionErrorMap;
static Timer? _timer;

static Future<String> get getExtensionsDir async =>
path.join(await MiruDirectory.getDirectory, 'extensions');
Expand All @@ -31,7 +34,11 @@ class ExtensionUtils {
await _loadExtensions();
// 监听目录变化
Directory(await getExtensionsDir).watch().listen((event) {
_loadExtensions();
_timer?.cancel();
_timer = Timer(const Duration(seconds: 1), () async {
await _loadExtensions();
debugPrint("load extension");
});
});
}

Expand Down Expand Up @@ -67,7 +74,7 @@ class ExtensionUtils {
}
// 重载搜索页面
if (Get.isRegistered<SearchPageController>()) {
Get.find<SearchPageController>().getRuntime();
Get.find<SearchPageController>().needRefresh = true;
}
}

Expand Down

0 comments on commit 7687260

Please sign in to comment.