refact: optimize, ID search peers (#10853)

* refact: optimize, preload peers

Signed-off-by: fufesou <linlong1266@gmail.com>

* Update dialogs.dart

---------

Signed-off-by: fufesou <linlong1266@gmail.com>
Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com>
This commit is contained in:
fufesou
2025-02-20 18:31:12 +08:00
committed by GitHub
parent 055b351164
commit 8b9a7a3506
12 changed files with 171 additions and 169 deletions

View File

@@ -58,6 +58,9 @@ class AbModel {
String? _personalAbGuid;
RxBool legacyMode = false.obs;
// Only handles peers add/remove
final Map<String, VoidCallback> _peerIdUpdateListeners = {};
final sortTags = shouldSortTags().obs;
final filterByIntersection = filterAbTagByIntersection().obs;
@@ -188,6 +191,7 @@ class AbModel {
debugPrint("pull current Ab error: $e");
}
}
_callbackPeerUpdate();
if (listInitialized && current.initialized) {
_saveCache();
}
@@ -419,6 +423,7 @@ class AbModel {
}
});
}
_callbackPeerUpdate();
return ret;
}
@@ -620,6 +625,9 @@ class AbModel {
}
}
}
if (abEntries.isNotEmpty) {
_callbackPeerUpdate();
}
}
}
@@ -742,6 +750,20 @@ class AbModel {
}
}
void _callbackPeerUpdate() {
for (var listener in _peerIdUpdateListeners.values) {
listener();
}
}
void addPeerUpdateListener(String key, VoidCallback listener) {
_peerIdUpdateListeners[key] = listener;
}
void removePeerUpdateListener(String key) {
_peerIdUpdateListeners.remove(key);
}
// #endregion
}

View File

@@ -23,6 +23,8 @@ class GroupModel {
var _cacheLoadOnceFlag = false;
var _statusCode = 200;
final Map<String, VoidCallback> _peerIdUpdateListeners = {};
bool get emtpy => deviceGroups.isEmpty && users.isEmpty && peers.isEmpty;
late final Peers peersModel;
@@ -92,6 +94,7 @@ class GroupModel {
.map((e) => e.online = true)
.toList();
groupLoadError.value = '';
_callbackPeerUpdate();
}
Future<bool> _getDeviceGroups(
@@ -329,6 +332,7 @@ class GroupModel {
for (final peer in data['peers']) {
peers.add(Peer.fromJson(peer));
}
_callbackPeerUpdate();
}
} catch (e) {
debugPrint("load group cache: $e");
@@ -343,4 +347,18 @@ class GroupModel {
selectedAccessibleItemName.value = '';
await bind.mainClearGroup();
}
void _callbackPeerUpdate() {
for (var listener in _peerIdUpdateListeners.values) {
listener();
}
}
void addPeerUpdateListener(String key, VoidCallback listener) {
_peerIdUpdateListeners[key] = listener;
}
void removePeerUpdateListener(String key) {
_peerIdUpdateListeners.remove(key);
}
}

View File

@@ -165,6 +165,11 @@ class Peers extends ChangeNotifier {
final String name;
final String loadEvent;
List<Peer> peers = List.empty(growable: true);
// Part of the peers that are not in the rest peers list.
// When there're too many peers, we may want to load the front 100 peers first,
// so we can see peers in UI quickly. `restPeerIds` is the rest peers' ids.
// And then load all peers later.
List<String> restPeerIds = List.empty(growable: true);
final GetInitPeers? getInitPeers;
UpdateEvent event = UpdateEvent.load;
static const _cbQueryOnlines = 'callback_query_onlines';
@@ -238,6 +243,12 @@ class Peers extends ChangeNotifier {
} else {
peers = _decodePeers(evt['peers']);
}
restPeerIds = [];
if (evt['ids'] != null) {
restPeerIds = (evt['ids'] as String).split(',');
}
for (var peer in peers) {
final state = onlineStates[peer.id];
peer.online = state != null && state != false;