mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-07-17 13:25:38 +03:00
review fix
This commit is contained in:
@@ -102,11 +102,14 @@ class AllPeersLoader {
|
|||||||
Set<String> _lastQueryOnlineIds = {};
|
Set<String> _lastQueryOnlineIds = {};
|
||||||
DateTime _lastQueryOnlineTime = DateTime.fromMillisecondsSinceEpoch(0);
|
DateTime _lastQueryOnlineTime = DateTime.fromMillisecondsSinceEpoch(0);
|
||||||
Timer? _queryOnlineTimer;
|
Timer? _queryOnlineTimer;
|
||||||
|
final Future<void> Function(List<String> ids) _queryOnlines;
|
||||||
|
final Duration _queryOnlineDebounce;
|
||||||
|
|
||||||
final String _listenerKey = 'AllPeersLoader';
|
final String _listenerKey = 'AllPeersLoader';
|
||||||
static const String _cbQueryOnlines = 'callback_query_onlines';
|
static const String _cbQueryOnlines = 'callback_query_onlines';
|
||||||
static const Duration _queryOnlineInterval = Duration(seconds: 5);
|
static const Duration _queryOnlineInterval = Duration(seconds: 5);
|
||||||
static const Duration _queryOnlineDebounce = Duration(milliseconds: 300);
|
static const Duration _defaultQueryOnlineDebounce =
|
||||||
|
Duration(milliseconds: 300);
|
||||||
static const int _maxQueryOnlineOptions = 20;
|
static const int _maxQueryOnlineOptions = 20;
|
||||||
|
|
||||||
late void Function(VoidCallback) setState;
|
late void Function(VoidCallback) setState;
|
||||||
@@ -114,7 +117,12 @@ class AllPeersLoader {
|
|||||||
bool get needLoad => !_isPeersLoaded && !_isPeersLoading;
|
bool get needLoad => !_isPeersLoaded && !_isPeersLoading;
|
||||||
bool get isPeersLoaded => _isPeersLoaded;
|
bool get isPeersLoaded => _isPeersLoaded;
|
||||||
|
|
||||||
AllPeersLoader();
|
AllPeersLoader({
|
||||||
|
@visibleForTesting Future<void> Function(List<String> ids)? queryOnlines,
|
||||||
|
@visibleForTesting Duration? queryOnlineDebounce,
|
||||||
|
}) : _queryOnlines = queryOnlines ?? ((ids) => bind.queryOnlines(ids: ids)),
|
||||||
|
_queryOnlineDebounce =
|
||||||
|
queryOnlineDebounce ?? _defaultQueryOnlineDebounce;
|
||||||
|
|
||||||
void init(void Function(VoidCallback) setState) {
|
void init(void Function(VoidCallback) setState) {
|
||||||
this.setState = setState;
|
this.setState = setState;
|
||||||
@@ -197,6 +205,8 @@ class AllPeersLoader {
|
|||||||
options,
|
options,
|
||||||
limit: _maxQueryOnlineOptions,
|
limit: _maxQueryOnlineOptions,
|
||||||
).toSet();
|
).toSet();
|
||||||
|
_queryOnlineTimer?.cancel();
|
||||||
|
_queryOnlineTimer = null;
|
||||||
if (ids.isEmpty) {
|
if (ids.isEmpty) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -206,13 +216,14 @@ class AllPeersLoader {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_queryOnlineTimer?.cancel();
|
_queryOnlineTimer = Timer(_queryOnlineDebounce, () async {
|
||||||
_queryOnlineTimer = Timer(_queryOnlineDebounce, () {
|
try {
|
||||||
_lastQueryOnlineIds = ids;
|
await _queryOnlines(ids.toList(growable: false));
|
||||||
_lastQueryOnlineTime = DateTime.now();
|
_lastQueryOnlineIds = ids;
|
||||||
bind.queryOnlines(ids: ids.toList(growable: false)).catchError((e) {
|
_lastQueryOnlineTime = DateTime.now();
|
||||||
|
} catch (e) {
|
||||||
debugPrint('query autocomplete online state failed: $e');
|
debugPrint('query autocomplete online state failed: $e');
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,4 +80,39 @@ void main() {
|
|||||||
expect(ids.where((id) => id == '0'), hasLength(1));
|
expect(ids.where((id) => id == '0'), hasLength(1));
|
||||||
expect(ids.last, '19');
|
expect(ids.last, '19');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('empty online query ids cancel pending debounce', () async {
|
||||||
|
final queriedIds = <List<String>>[];
|
||||||
|
final loader = AllPeersLoader(
|
||||||
|
queryOnlines: (ids) async {
|
||||||
|
queriedIds.add(ids);
|
||||||
|
},
|
||||||
|
queryOnlineDebounce: Duration(milliseconds: 1),
|
||||||
|
);
|
||||||
|
|
||||||
|
loader.queryOnlines([_peer(id: '123456789')]);
|
||||||
|
loader.queryOnlines([]);
|
||||||
|
await Future.delayed(Duration(milliseconds: 2));
|
||||||
|
|
||||||
|
expect(queriedIds, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failed online query enqueue does not suppress retry', () async {
|
||||||
|
var queryCount = 0;
|
||||||
|
final loader = AllPeersLoader(
|
||||||
|
queryOnlines: (ids) {
|
||||||
|
queryCount += 1;
|
||||||
|
return Future<void>.error(Exception('queue full'));
|
||||||
|
},
|
||||||
|
queryOnlineDebounce: Duration(milliseconds: 1),
|
||||||
|
);
|
||||||
|
|
||||||
|
loader.queryOnlines([_peer(id: '123456789')]);
|
||||||
|
await Future.delayed(Duration(milliseconds: 2));
|
||||||
|
|
||||||
|
loader.queryOnlines([_peer(id: '123456789')]);
|
||||||
|
await Future.delayed(Duration(milliseconds: 2));
|
||||||
|
|
||||||
|
expect(queryCount, 2);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user