refactor: enhance connectivity service with disposal checks
- Added checks in the ConnectivityService to prevent operations on a closed stream controller, ensuring stability and preventing potential errors. - Updated the timer creation and stream addition logic to only execute if the service is not disposed, improving resource management. - Enhanced the dispose method to safely close the connectivity controller, maintaining clean resource handling.
|
Before Width: | Height: | Size: 45 MiB After Width: | Height: | Size: 42 MiB |
|
Before Width: | Height: | Size: 705 KiB After Width: | Height: | Size: 2.2 MiB |
|
Before Width: | Height: | Size: 387 KiB After Width: | Height: | Size: 312 KiB |
|
Before Width: | Height: | Size: 293 KiB After Width: | Height: | Size: 318 KiB |
|
Before Width: | Height: | Size: 568 KiB After Width: | Height: | Size: 227 KiB |
|
Before Width: | Height: | Size: 240 KiB After Width: | Height: | Size: 297 KiB |
@@ -50,6 +50,9 @@ class ConnectivityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _checkConnectivity() async {
|
Future<void> _checkConnectivity() async {
|
||||||
|
// Don't check connectivity if service is disposed
|
||||||
|
if (_connectivityController.isClosed) return;
|
||||||
|
|
||||||
final serverReachability = await _probeActiveServer();
|
final serverReachability = await _probeActiveServer();
|
||||||
if (serverReachability != null) {
|
if (serverReachability != null) {
|
||||||
if (serverReachability) {
|
if (serverReachability) {
|
||||||
@@ -80,7 +83,10 @@ class ConnectivityService {
|
|||||||
void _updateStatus(ConnectivityStatus status) {
|
void _updateStatus(ConnectivityStatus status) {
|
||||||
if (_lastStatus != status) {
|
if (_lastStatus != status) {
|
||||||
_lastStatus = status;
|
_lastStatus = status;
|
||||||
_connectivityController.add(status);
|
// Only add to stream if controller is not closed
|
||||||
|
if (!_connectivityController.isClosed) {
|
||||||
|
_connectivityController.add(status);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adapt polling interval based on recent failures to reduce battery/CPU
|
// Adapt polling interval based on recent failures to reduce battery/CPU
|
||||||
@@ -99,10 +105,13 @@ class ConnectivityService {
|
|||||||
if (newInterval != _interval) {
|
if (newInterval != _interval) {
|
||||||
_interval = newInterval;
|
_interval = newInterval;
|
||||||
_connectivityTimer?.cancel();
|
_connectivityTimer?.cancel();
|
||||||
_connectivityTimer = Timer.periodic(
|
// Only create new timer if service is not disposed
|
||||||
_interval,
|
if (!_connectivityController.isClosed) {
|
||||||
(_) => _checkConnectivity(),
|
_connectivityTimer = Timer.periodic(
|
||||||
);
|
_interval,
|
||||||
|
(_) => _checkConnectivity(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +122,10 @@ class ConnectivityService {
|
|||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_connectivityTimer?.cancel();
|
_connectivityTimer?.cancel();
|
||||||
_connectivityController.close();
|
_connectivityTimer = null;
|
||||||
|
if (!_connectivityController.isClosed) {
|
||||||
|
_connectivityController.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool?> _probeActiveServer() async {
|
Future<bool?> _probeActiveServer() async {
|
||||||
|
|||||||