@@ -4,7 +4,7 @@ import { inject, injectable, named } from '@theia/core/shared/inversify';
4
4
import { spawn , ChildProcess } from 'child_process' ;
5
5
import { FileUri } from '@theia/core/lib/node/file-uri' ;
6
6
import { ILogger } from '@theia/core/lib/common/logger' ;
7
- import { Deferred } from '@theia/core/lib/common/promise-util' ;
7
+ import { Deferred , retry } from '@theia/core/lib/common/promise-util' ;
8
8
import {
9
9
Disposable ,
10
10
DisposableCollection ,
@@ -23,26 +23,26 @@ export class ArduinoDaemonImpl
23
23
{
24
24
@inject ( ILogger )
25
25
@named ( 'daemon' )
26
- protected readonly logger : ILogger ;
26
+ private readonly logger : ILogger ;
27
27
28
28
@inject ( EnvVariablesServer )
29
- protected readonly envVariablesServer : EnvVariablesServer ;
29
+ private readonly envVariablesServer : EnvVariablesServer ;
30
30
31
31
@inject ( NotificationServiceServer )
32
- protected readonly notificationService : NotificationServiceServer ;
32
+ private readonly notificationService : NotificationServiceServer ;
33
33
34
- protected readonly toDispose = new DisposableCollection ( ) ;
35
- protected readonly onDaemonStartedEmitter = new Emitter < string > ( ) ;
36
- protected readonly onDaemonStoppedEmitter = new Emitter < void > ( ) ;
34
+ private readonly toDispose = new DisposableCollection ( ) ;
35
+ private readonly onDaemonStartedEmitter = new Emitter < string > ( ) ;
36
+ private readonly onDaemonStoppedEmitter = new Emitter < void > ( ) ;
37
37
38
- protected _running = false ;
39
- protected _port = new Deferred < string > ( ) ;
40
- protected _execPath : string | undefined ;
38
+ private _running = false ;
39
+ private _port = new Deferred < string > ( ) ;
40
+ private _execPath : string | undefined ;
41
41
42
42
// Backend application lifecycle.
43
43
44
44
onStart ( ) : void {
45
- this . startDaemon ( ) ; // no await
45
+ this . start ( ) ; // no await
46
46
}
47
47
48
48
// Daemon API
@@ -58,7 +58,7 @@ export class ArduinoDaemonImpl
58
58
return undefined ;
59
59
}
60
60
61
- async startDaemon ( ) : Promise < void > {
61
+ async start ( ) : Promise < string > {
62
62
try {
63
63
this . toDispose . dispose ( ) ; // This will `kill` the previously started daemon process, if any.
64
64
const cliPath = await this . getExecPath ( ) ;
@@ -86,24 +86,29 @@ export class ArduinoDaemonImpl
86
86
] ) ;
87
87
this . fireDaemonStarted ( port ) ;
88
88
this . onData ( 'Daemon is running.' ) ;
89
+ return port ;
89
90
} catch ( err ) {
90
- this . onData ( 'Failed to start the daemon.' ) ;
91
- this . onError ( err ) ;
92
- let i = 5 ; // TODO: make this better
93
- while ( i ) {
94
- this . onData ( `Restarting daemon in ${ i } seconds...` ) ;
95
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
96
- i -- ;
97
- }
98
- this . onData ( 'Restarting daemon now...' ) ;
99
- return this . startDaemon ( ) ;
91
+ return retry (
92
+ ( ) => {
93
+ this . onError ( err ) ;
94
+ return this . start ( ) ;
95
+ } ,
96
+ 1_000 ,
97
+ 5
98
+ ) ;
100
99
}
101
100
}
102
101
103
- async stopDaemon ( ) : Promise < void > {
102
+ async stop ( ) : Promise < void > {
104
103
this . toDispose . dispose ( ) ;
105
104
}
106
105
106
+ async restart ( ) : Promise < string > {
107
+ return this . start ( ) ;
108
+ }
109
+
110
+ // Backend only daemon API
111
+
107
112
get onDaemonStarted ( ) : Event < string > {
108
113
return this . onDaemonStartedEmitter . event ;
109
114
}
@@ -275,14 +280,14 @@ export class ArduinoDaemonImpl
275
280
return ready . promise ;
276
281
}
277
282
278
- protected fireDaemonStarted ( port : string ) : void {
283
+ private fireDaemonStarted ( port : string ) : void {
279
284
this . _running = true ;
280
285
this . _port . resolve ( port ) ;
281
286
this . onDaemonStartedEmitter . fire ( port ) ;
282
287
this . notificationService . notifyDaemonDidStart ( port ) ;
283
288
}
284
289
285
- protected fireDaemonStopped ( ) : void {
290
+ private fireDaemonStopped ( ) : void {
286
291
if ( ! this . _running ) {
287
292
return ;
288
293
}
@@ -297,7 +302,8 @@ export class ArduinoDaemonImpl
297
302
this . logger . info ( message ) ;
298
303
}
299
304
300
- protected onError ( error : any ) : void {
305
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
306
+ private onError ( error : any ) : void {
301
307
this . logger . error ( error ) ;
302
308
}
303
309
}
0 commit comments