fix: reduce rebuild for online state (#9331)
* fix: reduce rebuild for online state Signed-off-by: fufesou <linlong1266@gmail.com> * Query online, update on focus changed Signed-off-by: fufesou <linlong1266@gmail.com> * Use to ensure is right Signed-off-by: fufesou <linlong1266@gmail.com> * refact, window events on peer view Signed-off-by: fufesou <linlong1266@gmail.com> * chore Signed-off-by: fufesou <linlong1266@gmail.com> * Remove unused code Signed-off-by: fufesou <linlong1266@gmail.com> --------- Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
parent
2e81bcb447
commit
d2e98cc620
@ -89,6 +89,7 @@ class _PeersViewState extends State<_PeersView>
|
|||||||
var _lastChangeTime = DateTime.now();
|
var _lastChangeTime = DateTime.now();
|
||||||
var _lastQueryPeers = <String>{};
|
var _lastQueryPeers = <String>{};
|
||||||
var _lastQueryTime = DateTime.now();
|
var _lastQueryTime = DateTime.now();
|
||||||
|
var _lastWindowRestoreTime = DateTime.now();
|
||||||
var _queryCount = 0;
|
var _queryCount = 0;
|
||||||
var _exit = false;
|
var _exit = false;
|
||||||
bool _isActive = true;
|
bool _isActive = true;
|
||||||
@ -117,11 +118,37 @@ class _PeersViewState extends State<_PeersView>
|
|||||||
@override
|
@override
|
||||||
void onWindowFocus() {
|
void onWindowFocus() {
|
||||||
_queryCount = 0;
|
_queryCount = 0;
|
||||||
|
_isActive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onWindowBlur() {
|
||||||
|
// We need this comparison because window restore (on Windows) also triggers `onWindowBlur()`.
|
||||||
|
// Maybe it's a bug of the window manager, but the source code seems to be correct.
|
||||||
|
//
|
||||||
|
// Although `onWindowRestore()` is called after `onWindowBlur()` in my test,
|
||||||
|
// we need the following comparison to ensure that `_isActive` is true in the end.
|
||||||
|
if (isWindows && DateTime.now().difference(_lastWindowRestoreTime) <
|
||||||
|
const Duration(milliseconds: 300)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_queryCount = _maxQueryCount;
|
||||||
|
_isActive = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onWindowRestore() {
|
||||||
|
// Window restore (on MacOS and Linux) also triggers `onWindowFocus()`.
|
||||||
|
// But on Windows, it triggers `onWindowBlur()`, mybe it's a bug of the window manager.
|
||||||
|
if (!isWindows) return;
|
||||||
|
_queryCount = 0;
|
||||||
|
_isActive = true;
|
||||||
|
_lastWindowRestoreTime = DateTime.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onWindowMinimize() {
|
void onWindowMinimize() {
|
||||||
_queryCount = _maxQueryCount;
|
// Window minimize also triggers `onWindowBlur()`.
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is required for mobile.
|
// This function is required for mobile.
|
||||||
@ -234,7 +261,8 @@ class _PeersViewState extends State<_PeersView>
|
|||||||
physics: DraggableNeverScrollableScrollPhysics(),
|
physics: DraggableNeverScrollableScrollPhysics(),
|
||||||
itemCount: peers.length,
|
itemCount: peers.length,
|
||||||
itemBuilder: (BuildContext context, int index) {
|
itemBuilder: (BuildContext context, int index) {
|
||||||
return buildOnePeer(peers[index], false).marginOnly(
|
return buildOnePeer(peers[index], false)
|
||||||
|
.marginOnly(
|
||||||
right: space,
|
right: space,
|
||||||
top: index == 0 ? 0 : space / 2,
|
top: index == 0 ? 0 : space / 2,
|
||||||
bottom: space / 2);
|
bottom: space / 2);
|
||||||
|
@ -194,25 +194,34 @@ class Peers extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _updateOnlineState(Map<String, dynamic> evt) {
|
void _updateOnlineState(Map<String, dynamic> evt) {
|
||||||
|
int changedCount = 0;
|
||||||
evt['onlines'].split(',').forEach((online) {
|
evt['onlines'].split(',').forEach((online) {
|
||||||
for (var i = 0; i < peers.length; i++) {
|
for (var i = 0; i < peers.length; i++) {
|
||||||
if (peers[i].id == online) {
|
if (peers[i].id == online) {
|
||||||
|
if (!peers[i].online) {
|
||||||
|
changedCount += 1;
|
||||||
peers[i].online = true;
|
peers[i].online = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
evt['offlines'].split(',').forEach((offline) {
|
evt['offlines'].split(',').forEach((offline) {
|
||||||
for (var i = 0; i < peers.length; i++) {
|
for (var i = 0; i < peers.length; i++) {
|
||||||
if (peers[i].id == offline) {
|
if (peers[i].id == offline) {
|
||||||
|
if (peers[i].online) {
|
||||||
|
changedCount += 1;
|
||||||
peers[i].online = false;
|
peers[i].online = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (changedCount > 0) {
|
||||||
event = UpdateEvent.online;
|
event = UpdateEvent.online;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _updatePeers(Map<String, dynamic> evt) {
|
void _updatePeers(Map<String, dynamic> evt) {
|
||||||
final onlineStates = _getOnlineStates();
|
final onlineStates = _getOnlineStates();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user