Merge pull request #4712 from fufesou/refact/query_online_on_load_peers

query online on load peers
This commit is contained in:
RustDesk 2023-06-20 23:38:18 +08:00 committed by GitHub
commit d7a7f87a75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 12 deletions

View File

@ -168,6 +168,7 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
Widget _buildPeersView(Peers peers) {
_loaded = true;
final updateEvent = peers.event;
final body = ObxValue<RxList>((filters) {
return FutureBuilder<List<Peer>>(
builder: (context, snapshot) {
@ -191,7 +192,14 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
)
: SizedBox(width: mobileWidth, child: visibilityChild));
}
return Wrap(spacing: space, runSpacing: space, children: cards);
final child =
Wrap(spacing: space, runSpacing: space, children: cards);
if (updateEvent == UpdateEvent.load) {
_curPeers.clear();
_curPeers.addAll(peers.map((e) => e.id));
_queryOnlines(true);
}
return child;
} else {
return const Center(
child: CircularProgressIndicator(),
@ -205,26 +213,19 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
return body;
}
// ignore: todo
// TODO: variables walk through async tasks?
final _queryInterval = const Duration(seconds: 20);
void _startCheckOnlines() {
final queryInterval = const Duration(seconds: 20);
() async {
while (!_exit) {
final now = DateTime.now();
if (!setEquals(_curPeers, _lastQueryPeers)) {
if (now.difference(_lastChangeTime) > const Duration(seconds: 1)) {
if (_curPeers.isNotEmpty) {
platformFFI.ffiBind
.queryOnlines(ids: _curPeers.toList(growable: false));
_lastQueryPeers = {..._curPeers};
_lastQueryTime = DateTime.now().subtract(queryInterval);
_queryCount = 0;
}
_queryOnlines(false);
}
} else {
if (_queryCount < _maxQueryCount) {
if (now.difference(_lastQueryTime) >= queryInterval) {
if (now.difference(_lastQueryTime) >= _queryInterval) {
if (_curPeers.isNotEmpty) {
platformFFI.ffiBind
.queryOnlines(ids: _curPeers.toList(growable: false));
@ -239,6 +240,19 @@ class _PeersViewState extends State<_PeersView> with WindowListener {
}();
}
_queryOnlines(bool isLoadEvent) {
if (_curPeers.isNotEmpty) {
platformFFI.ffiBind.queryOnlines(ids: _curPeers.toList(growable: false));
_lastQueryPeers = {..._curPeers};
if (isLoadEvent) {
_lastChangeTime = DateTime.now();
} else {
_lastQueryTime = DateTime.now().subtract(_queryInterval);
}
_queryCount = 0;
}
}
Future<List<Peer>>? matchPeers(
String searchText, String sortedBy, List<Peer> peers) async {
if (widget.peerFilter != null) {

View File

@ -72,10 +72,13 @@ class Peer {
);
}
enum UpdateEvent { online, load }
class Peers extends ChangeNotifier {
final String name;
final String loadEvent;
List<Peer> peers;
UpdateEvent event = UpdateEvent.load;
static const _cbQueryOnlines = 'callback_query_onlines';
Peers({required this.name, required this.peers, required this.loadEvent}) {
@ -123,6 +126,7 @@ class Peers extends ChangeNotifier {
}
});
event = UpdateEvent.online;
notifyListeners();
}
@ -133,6 +137,7 @@ class Peers extends ChangeNotifier {
final state = onlineStates[peer.id];
peer.online = state != null && state != false;
}
event = UpdateEvent.load;
notifyListeners();
}