@@ -102,15 +102,14 @@ final class ExtensionDeviceClient extends DeviceService {
102102/// A [DeviceLogReader] that streams log lines from a tool extension isolate
103103/// via [DeviceService.logEntryNotificationMethod] notifications.
104104class ExtensionDeviceLogReader extends DeviceLogReader {
105- ExtensionDeviceLogReader ({
105+ ExtensionDeviceLogReader ._ ({
106106 required ExtensionConnection connection,
107107 required this ._deviceId,
108108 required this ._deviceService,
109109 required this ._logger,
110110 required this .name,
111- this ._appId,
112111 }) {
113- _logLinesController = StreamController <String >.broadcast (onListen: _startLogStream );
112+ _logLinesController = StreamController <String >.broadcast (onListen: _flushPendingLines );
114113 _notificationSubscription = connection
115114 .onNotification (DeviceService .logEntryNotificationMethod)
116115 .listen ((Map <String , Object ?> params) {
@@ -119,65 +118,86 @@ class ExtensionDeviceLogReader extends DeviceLogReader {
119118 }
120119 if (params[DeviceService .deviceIdParam] == _deviceId) {
121120 if (params[DeviceService .lineKey] case final String line) {
122- _logLinesController.add (line);
121+ if (_logLinesController.hasListener) {
122+ _logLinesController.add (line);
123+ } else {
124+ _pendingLines.add (line);
125+ }
123126 }
124127 }
125128 });
126129 }
127130
128- final String ? _appId;
131+ /// Creates an [ExtensionDeviceLogReader] and starts the device log stream on
132+ /// the extension isolate.
133+ static Future <ExtensionDeviceLogReader > create ({
134+ required ExtensionConnection connection,
135+ required String deviceId,
136+ required DeviceService deviceService,
137+ required Logger logger,
138+ required String name,
139+ String ? appId,
140+ }) async {
141+ final reader = ExtensionDeviceLogReader ._(
142+ connection: connection,
143+ deviceId: deviceId,
144+ deviceService: deviceService,
145+ logger: logger,
146+ name: name,
147+ );
148+ await reader._startLogStream (appId: appId);
149+ return reader;
150+ }
151+
129152 final String _deviceId;
130153 final DeviceService _deviceService;
131154 final Logger _logger;
155+ final _pendingLines = < String > [];
132156
133157 @override
134158 final String name;
135159
136160 late final StreamController <String > _logLinesController;
137161 StreamSubscription <Map <String , Object ?>>? _notificationSubscription;
138- bool _hasStarted = false ;
139162 bool _isDisposed = false ;
140163
141164 @override
142165 Stream <String > get logLines => _logLinesController.stream;
143166
144- void _startLogStream () {
145- if (_hasStarted || _isDisposed ) {
167+ void _flushPendingLines () {
168+ if (_isDisposed || _logLinesController.isClosed || _pendingLines.isEmpty ) {
146169 return ;
147170 }
148- _hasStarted = true ;
149- unawaited ( _invokeStartLogStream () );
171+ _pendingLines. forEach (_logLinesController.add) ;
172+ _pendingLines. clear ( );
150173 }
151174
152- Future <void > _invokeStartLogStream ( ) async {
175+ Future <void > _startLogStream ({ String ? appId} ) async {
153176 try {
154- await _deviceService.startLogStream (deviceId: _deviceId, appId: _appId );
177+ await _deviceService.startLogStream (deviceId: _deviceId, appId: appId );
155178 } on Object catch (error) {
156179 _logger.printWarning ('Failed to start log stream for extension device "$_deviceId ": $error ' );
157180 }
158181 }
159182
160- Future <void > _invokeStopLogStream () async {
161- try {
162- await _deviceService.stopLogStream (deviceId: _deviceId);
163- } on Object {
164- // Ignore errors when stopping log streams during disposal.
165- }
166- }
167-
168183 @override
169184 Future <void > provideVmService (FlutterVmService connectedVmService) async {}
170185
171186 @override
172- void dispose () {
187+ Future < void > dispose () async {
173188 if (_isDisposed) {
174189 return ;
175190 }
176191 _isDisposed = true ;
177- unawaited (_notificationSubscription? .cancel ());
192+ _pendingLines.clear ();
193+ await _notificationSubscription? .cancel ();
178194 _notificationSubscription = null ;
179- unawaited (_invokeStopLogStream ());
180- unawaited (_logLinesController.close ());
195+ try {
196+ await _deviceService.stopLogStream (deviceId: _deviceId);
197+ } on Object {
198+ // Ignore errors when stopping log streams during disposal.
199+ }
200+ await _logLinesController.close ();
181201 }
182202}
183203
@@ -264,7 +284,7 @@ class ExtensionBackedDevice extends Device {
264284 final Logger _logger;
265285 final TargetDevice _targetDevice;
266286 final ExtensionConnection connection;
267- final _logReaders = < String ? , ExtensionDeviceLogReader > {};
287+ final _logReaders = < String ? , Future < ExtensionDeviceLogReader > > {};
268288
269289 @override
270290 String get name => _targetDevice.name;
@@ -309,10 +329,10 @@ class ExtensionBackedDevice extends Device {
309329 DevicePortForwarder ? get portForwarder => null ;
310330
311331 @override
312- DeviceLogReader getLogReader ({ApplicationPackage ? app, bool includePastLogs = false }) {
332+ Future < DeviceLogReader > getLogReader ({ApplicationPackage ? app, bool includePastLogs = false }) {
313333 return _logReaders.putIfAbsent (
314334 app? .id,
315- () => ExtensionDeviceLogReader (
335+ () => ExtensionDeviceLogReader . create (
316336 appId: app? .id,
317337 connection: connection,
318338 deviceId: id,
@@ -328,8 +348,9 @@ class ExtensionBackedDevice extends Device {
328348
329349 @override
330350 Future <void > dispose () async {
331- for (final ExtensionDeviceLogReader reader in _logReaders.values) {
332- reader.dispose ();
351+ for (final Future <ExtensionDeviceLogReader > readerFuture in _logReaders.values) {
352+ final ExtensionDeviceLogReader reader = await readerFuture;
353+ await reader.dispose ();
333354 }
334355 _logReaders.clear ();
335356 }
0 commit comments