Skip to content

Commit 9b09ff9

Browse files
committed
refactor: use ??= to make stream subscriptions idempotent
Per Copilot review feedback, use ??= instead of cancel-and-reassign to ensure subscriptions are only created once even if initialize() is called multiple times. This is cleaner and avoids unnecessary cancellation of active subscriptions.
1 parent d4fb534 commit 9b09ff9

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

lib/providers/board_state_provider.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class BoardStateProvider extends ChangeNotifier {
4848

4949
if (configProvider.config.autoStart) {
5050
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
51-
await _usbSubscription?.cancel();
52-
_usbSubscription = UsbSerial.usbEventStream?.listen(
51+
_usbSubscription ??= UsbSerial.usbEventStream?.listen(
5352
(UsbEvent usbEvent) async {
5453
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
5554
if (_isProcessing) return;
@@ -73,8 +72,7 @@ class BoardStateProvider extends ChangeNotifier {
7372
}
7473
}
7574

76-
await _connectivitySubscription?.cancel();
77-
_connectivitySubscription = Connectivity()
75+
_connectivitySubscription ??= Connectivity()
7876
.onConnectivityChanged
7977
.listen((List<ConnectivityResult> results) {
8078
if (results.contains(ConnectivityResult.none)) {
@@ -134,4 +132,4 @@ class BoardStateProvider extends ChangeNotifier {
134132
legacyFirmwareNotifier.dispose();
135133
super.dispose();
136134
}
137-
}
135+
}

0 commit comments

Comments
 (0)