11import {
22 ApplicationError ,
3- CommandRegistry ,
43 Disposable ,
54 Emitter ,
65 MessageService ,
76 nls ,
87} from '@theia/core' ;
98import { Deferred } from '@theia/core/lib/common/promise-util' ;
109import { inject , injectable } from '@theia/core/shared/inversify' ;
10+ import { NotificationManager } from '@theia/messages/lib/browser/notifications-manager' ;
11+ import { MessageType } from '@theia/core/lib/common/message-service-protocol' ;
1112import { Board , Port } from '../common/protocol' ;
1213import {
1314 Monitor ,
@@ -25,21 +26,31 @@ import { BoardsServiceProvider } from './boards/boards-service-provider';
2526export class MonitorManagerProxyClientImpl
2627 implements MonitorManagerProxyClient
2728{
29+ @inject ( MessageService )
30+ private readonly messageService : MessageService ;
31+ // This is necessary to call the backend methods from the frontend
32+ @inject ( MonitorManagerProxyFactory )
33+ private readonly server : MonitorManagerProxyFactory ;
34+ @inject ( BoardsServiceProvider )
35+ private readonly boardsServiceProvider : BoardsServiceProvider ;
36+ @inject ( NotificationManager )
37+ private readonly notificationManager : NotificationManager ;
38+
2839 // When pluggable monitor messages are received from the backend
2940 // this event is triggered.
3041 // Ideally a frontend component is connected to this event
3142 // to update the UI.
32- protected readonly onMessagesReceivedEmitter = new Emitter < {
43+ private readonly onMessagesReceivedEmitter = new Emitter < {
3344 messages : string [ ] ;
3445 } > ( ) ;
3546 readonly onMessagesReceived = this . onMessagesReceivedEmitter . event ;
3647
37- protected readonly onMonitorSettingsDidChangeEmitter =
48+ private readonly onMonitorSettingsDidChangeEmitter =
3849 new Emitter < MonitorSettings > ( ) ;
3950 readonly onMonitorSettingsDidChange =
4051 this . onMonitorSettingsDidChangeEmitter . event ;
4152
42- protected readonly onMonitorShouldResetEmitter = new Emitter < void > ( ) ;
53+ private readonly onMonitorShouldResetEmitter = new Emitter < void > ( ) ;
4354 readonly onMonitorShouldReset = this . onMonitorShouldResetEmitter . event ;
4455
4556 // WebSocket used to handle pluggable monitor communication between
@@ -53,21 +64,6 @@ export class MonitorManagerProxyClientImpl
5364 return this . wsPort ;
5465 }
5566
56- constructor (
57- @inject ( MessageService )
58- protected messageService : MessageService ,
59-
60- // This is necessary to call the backend methods from the frontend
61- @inject ( MonitorManagerProxyFactory )
62- protected server : MonitorManagerProxyFactory ,
63-
64- @inject ( CommandRegistry )
65- protected readonly commandRegistry : CommandRegistry ,
66-
67- @inject ( BoardsServiceProvider )
68- protected readonly boardsServiceProvider : BoardsServiceProvider
69- ) { }
70-
7167 /**
7268 * Connects a localhost WebSocket using the specified port.
7369 * @param addressPort port of the WebSocket
@@ -165,11 +161,11 @@ export class MonitorManagerProxyClientImpl
165161 ? Port . keyOf ( this . lastConnectedBoard . selectedPort )
166162 : undefined )
167163 ) {
168- this . onMonitorShouldResetEmitter . fire ( ) ;
169164 this . lastConnectedBoard = {
170165 selectedBoard : selectedBoard ,
171166 selectedPort : selectedPort ,
172167 } ;
168+ this . onMonitorShouldResetEmitter . fire ( ) ;
173169 } else {
174170 // a board is plugged and it's the same as prev, rerun "this.startMonitor" to
175171 // recreate the listener callback
@@ -183,11 +179,12 @@ export class MonitorManagerProxyClientImpl
183179 this . boardsServiceProvider . boardsConfig ;
184180 if ( ! selectedBoard || ! selectedBoard . fqbn || ! selectedPort ) return ;
185181 try {
182+ this . clearVisibleNotification ( ) ;
186183 await this . server ( ) . startMonitor ( selectedBoard , selectedPort , settings ) ;
187184 } catch ( err ) {
188- this . messageService . error (
189- ApplicationError . is ( err ) ? err . message : String ( err )
190- ) ;
185+ const message = ApplicationError . is ( err ) ? err . message : String ( err ) ;
186+ this . previousNotificationId = this . notificationId ( message ) ;
187+ this . messageService . error ( message ) ;
191188 }
192189 }
193190
@@ -220,4 +217,24 @@ export class MonitorManagerProxyClientImpl
220217 } )
221218 ) ;
222219 }
220+
221+ /**
222+ * This is the internal (Theia) ID of the notification that is currently visible.
223+ * It's stored here as a field to be able to close it before starting a new monitor connection. It's a hack.
224+ */
225+ private previousNotificationId : string | undefined ;
226+ private clearVisibleNotification ( ) : void {
227+ if ( this . previousNotificationId ) {
228+ this . notificationManager . clear ( this . previousNotificationId ) ;
229+ this . previousNotificationId = undefined ;
230+ }
231+ }
232+
233+ private notificationId ( message : string , ...actions : string [ ] ) : string {
234+ return this . notificationManager [ 'getMessageId' ] ( {
235+ text : message ,
236+ actions,
237+ type : MessageType . Error ,
238+ } ) ;
239+ }
223240}
0 commit comments