-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconnection_monitor.dart
More file actions
50 lines (42 loc) · 1.27 KB
/
connection_monitor.dart
File metadata and controls
50 lines (42 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import 'dart:async';
import 'package:celest_cli/src/context.dart';
/// Monitors the local connection to the Internet.
final class ConnectionMonitor {
/// We use pub.dev to check if an Internet connection is available generally
/// since this is mostly used to determine if we need to pass `--offline`
/// to pub commands.
static final Uri _checkUri = Uri.parse('https://pub.dev/');
final _connectionStream = StreamController<bool>.broadcast();
bool _isConnected = true;
bool get isConnected => _isConnected;
Stream<bool> get stream => _connectionStream.stream;
void init() {
_checkConnection();
Timer.periodic(const Duration(seconds: 5), (timer) {
if (_connectionStream.isClosed) {
timer.cancel();
return;
}
_checkConnection();
});
}
Future<void> _checkConnection() async {
final status = await checkConnection();
_isConnected = status;
if (!_connectionStream.isClosed) {
_connectionStream.add(status);
}
}
Future<bool> checkConnection() async {
try {
final result =
await httpClient.head(_checkUri).timeout(const Duration(seconds: 1));
return result.statusCode == 200;
} on Object {
return false;
}
}
void close() {
_connectionStream.close();
}
}