1
1
import {
2
2
ApplicationError ,
3
- CommandRegistry ,
4
3
Disposable ,
5
4
Emitter ,
6
5
MessageService ,
7
6
nls ,
8
7
} from '@theia/core' ;
9
8
import { Deferred } from '@theia/core/lib/common/promise-util' ;
10
9
import { 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' ;
11
12
import { Board , Port } from '../common/protocol' ;
12
13
import {
13
14
Monitor ,
@@ -25,21 +26,31 @@ import { BoardsServiceProvider } from './boards/boards-service-provider';
25
26
export class MonitorManagerProxyClientImpl
26
27
implements MonitorManagerProxyClient
27
28
{
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
+
28
39
// When pluggable monitor messages are received from the backend
29
40
// this event is triggered.
30
41
// Ideally a frontend component is connected to this event
31
42
// to update the UI.
32
- protected readonly onMessagesReceivedEmitter = new Emitter < {
43
+ private readonly onMessagesReceivedEmitter = new Emitter < {
33
44
messages : string [ ] ;
34
45
} > ( ) ;
35
46
readonly onMessagesReceived = this . onMessagesReceivedEmitter . event ;
36
47
37
- protected readonly onMonitorSettingsDidChangeEmitter =
48
+ private readonly onMonitorSettingsDidChangeEmitter =
38
49
new Emitter < MonitorSettings > ( ) ;
39
50
readonly onMonitorSettingsDidChange =
40
51
this . onMonitorSettingsDidChangeEmitter . event ;
41
52
42
- protected readonly onMonitorShouldResetEmitter = new Emitter < void > ( ) ;
53
+ private readonly onMonitorShouldResetEmitter = new Emitter < void > ( ) ;
43
54
readonly onMonitorShouldReset = this . onMonitorShouldResetEmitter . event ;
44
55
45
56
// WebSocket used to handle pluggable monitor communication between
@@ -53,21 +64,6 @@ export class MonitorManagerProxyClientImpl
53
64
return this . wsPort ;
54
65
}
55
66
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
-
71
67
/**
72
68
* Connects a localhost WebSocket using the specified port.
73
69
* @param addressPort port of the WebSocket
@@ -165,11 +161,11 @@ export class MonitorManagerProxyClientImpl
165
161
? Port . keyOf ( this . lastConnectedBoard . selectedPort )
166
162
: undefined )
167
163
) {
168
- this . onMonitorShouldResetEmitter . fire ( ) ;
169
164
this . lastConnectedBoard = {
170
165
selectedBoard : selectedBoard ,
171
166
selectedPort : selectedPort ,
172
167
} ;
168
+ this . onMonitorShouldResetEmitter . fire ( ) ;
173
169
} else {
174
170
// a board is plugged and it's the same as prev, rerun "this.startMonitor" to
175
171
// recreate the listener callback
@@ -183,11 +179,12 @@ export class MonitorManagerProxyClientImpl
183
179
this . boardsServiceProvider . boardsConfig ;
184
180
if ( ! selectedBoard || ! selectedBoard . fqbn || ! selectedPort ) return ;
185
181
try {
182
+ this . clearVisibleNotification ( ) ;
186
183
await this . server ( ) . startMonitor ( selectedBoard , selectedPort , settings ) ;
187
184
} 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 ) ;
191
188
}
192
189
}
193
190
@@ -220,4 +217,24 @@ export class MonitorManagerProxyClientImpl
220
217
} )
221
218
) ;
222
219
}
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
+ }
223
240
}
0 commit comments