From 6e1f8f0294b04d80c7bea987ff7a308c644d1c10 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Tue, 29 Nov 2022 22:36:35 +0800 Subject: [PATCH] new fetchID and periodic_immediate --- flutter/lib/common.dart | 7 ++++++ .../lib/desktop/pages/connection_page.dart | 2 +- .../lib/desktop/pages/desktop_home_page.dart | 3 ++- flutter/lib/models/server_model.dart | 25 ++++--------------- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index fed58e999..60936cfdf 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -1403,3 +1403,10 @@ void onActiveWindowChanged() async { } } } + +Timer periodic_immediate(Duration duration, Future Function() callback) { + Future.delayed(Duration.zero, callback); + return Timer.periodic(duration, (timer) async { + await callback(); + }); +} diff --git a/flutter/lib/desktop/pages/connection_page.dart b/flutter/lib/desktop/pages/connection_page.dart index a1e87b418..a830d6399 100644 --- a/flutter/lib/desktop/pages/connection_page.dart +++ b/flutter/lib/desktop/pages/connection_page.dart @@ -61,7 +61,7 @@ class _ConnectionPageState extends State } }(); } - _updateTimer = Timer.periodic(Duration(seconds: 1), (timer) { + _updateTimer = periodic_immediate(Duration(seconds: 1), () async { updateStatus(); }); _idFocusNode.addListener(() { diff --git a/flutter/lib/desktop/pages/desktop_home_page.dart b/flutter/lib/desktop/pages/desktop_home_page.dart index a6d4691ce..712563a56 100644 --- a/flutter/lib/desktop/pages/desktop_home_page.dart +++ b/flutter/lib/desktop/pages/desktop_home_page.dart @@ -461,7 +461,8 @@ class _DesktopHomePageState extends State void initState() { super.initState(); bind.mainStartGrabKeyboard(); - _updateTimer = Timer.periodic(const Duration(seconds: 1), (timer) async { + _updateTimer = periodic_immediate(const Duration(seconds: 1), () async { + await gFFI.serverModel.fetchID(); final url = await bind.mainGetSoftwareUpdateUrl(); if (updateUrl != url) { updateUrl = url; diff --git a/flutter/lib/models/server_model.dart b/flutter/lib/models/server_model.dart index 456c3cdd2..344733324 100644 --- a/flutter/lib/models/server_model.dart +++ b/flutter/lib/models/server_model.dart @@ -324,7 +324,6 @@ class ServerModel with ChangeNotifier { parent.target?.ffiModel.updateEventListener(""); await parent.target?.invokeMethod("init_service"); await bind.mainStartService(); - _fetchID(); updateClientState(); if (!Platform.isLinux) { // current linux is not supported @@ -360,26 +359,12 @@ class ServerModel with ChangeNotifier { } } - _fetchID() async { - final old = _serverId.id; - var count = 0; - const maxCount = 10; - while (count < maxCount) { - await Future.delayed(Duration(seconds: 1)); - final id = await bind.mainGetMyId(); - if (id.isEmpty) { - continue; - } else { - _serverId.id = id; - } - - debugPrint("fetch id again at $count:id:${_serverId.id}"); - count++; - if (_serverId.id != old) { - break; - } + fetchID() async { + final id = await bind.mainGetMyId(); + if (id != _serverId.id) { + _serverId.id = id; + notifyListeners(); } - notifyListeners(); } changeStatue(String name, bool value) {