@@ -37,6 +37,12 @@ export type ChangedFile = {
37
37
type : 'create' | 'update' | 'delete' ;
38
38
} ;
39
39
40
+ enum DaemonStatus {
41
+ CONNECTING ,
42
+ DISCONNECTED ,
43
+ CONNECTED ,
44
+ }
45
+
40
46
export class DaemonClient {
41
47
constructor ( private readonly nxJson : NxJsonConfiguration ) {
42
48
this . reset ( ) ;
@@ -50,7 +56,9 @@ export class DaemonClient {
50
56
private currentReject ;
51
57
52
58
private _enabled : boolean | undefined ;
53
- private _connected : boolean ;
59
+ private _daemonStatus : DaemonStatus = DaemonStatus . DISCONNECTED ;
60
+ private _waitForDaemonReady : Promise < void > | null = null ;
61
+ private _daemonReady : ( ) => void | null = null ;
54
62
private _out : FileHandle = null ;
55
63
private _err : FileHandle = null ;
56
64
@@ -104,7 +112,10 @@ export class DaemonClient {
104
112
this . _out = null ;
105
113
this . _err = null ;
106
114
107
- this . _connected = false ;
115
+ this . _daemonStatus = DaemonStatus . DISCONNECTED ;
116
+ this . _waitForDaemonReady = new Promise < void > (
117
+ ( resolve ) => ( this . _daemonReady = resolve )
118
+ ) ;
108
119
}
109
120
110
121
async requestShutdown ( ) : Promise < void > {
@@ -149,27 +160,28 @@ export class DaemonClient {
149
160
) => void
150
161
) : Promise < UnregisterCallback > {
151
162
await this . getProjectGraph ( ) ;
152
- const messenger = new SocketMessenger ( connect ( FULL_OS_SOCKET_PATH ) ) . listen (
153
- ( message ) => {
154
- try {
155
- const parsedMessage = JSON . parse ( message ) ;
156
- callback ( null , parsedMessage ) ;
157
- } catch ( e ) {
158
- callback ( e , null ) ;
159
- }
160
- } ,
161
- ( ) => {
162
- callback ( 'closed' , null ) ;
163
- } ,
164
- ( err ) => callback ( err , null )
165
- ) ;
166
-
167
- await this . queue . sendToQueue ( ( ) =>
168
- messenger . sendMessage ( { type : 'REGISTER_FILE_WATCHER' , config } )
169
- ) ;
163
+ let messenger : SocketMessenger | undefined ;
164
+
165
+ await this . queue . sendToQueue ( ( ) => {
166
+ messenger = new SocketMessenger ( connect ( FULL_OS_SOCKET_PATH ) ) . listen (
167
+ ( message ) => {
168
+ try {
169
+ const parsedMessage = JSON . parse ( message ) ;
170
+ callback ( null , parsedMessage ) ;
171
+ } catch ( e ) {
172
+ callback ( e , null ) ;
173
+ }
174
+ } ,
175
+ ( ) => {
176
+ callback ( 'closed' , null ) ;
177
+ } ,
178
+ ( err ) => callback ( err , null )
179
+ ) ;
180
+ return messenger . sendMessage ( { type : 'REGISTER_FILE_WATCHER' , config } ) ;
181
+ } ) ;
170
182
171
183
return ( ) => {
172
- messenger . close ( ) ;
184
+ messenger ? .close ( ) ;
173
185
} ;
174
186
}
175
187
@@ -232,7 +244,7 @@ export class DaemonClient {
232
244
// it's ok for the daemon to terminate if the client doesn't wait on
233
245
// any messages from the daemon
234
246
if ( this . queue . isEmpty ( ) ) {
235
- this . _connected = false ;
247
+ this . reset ( ) ;
236
248
} else {
237
249
output . error ( {
238
250
title : 'Daemon process terminated and closed the connection' ,
@@ -280,12 +292,17 @@ export class DaemonClient {
280
292
}
281
293
282
294
private async sendMessageToDaemon ( message : Message ) : Promise < any > {
283
- if ( ! this . _connected ) {
284
- this . _connected = true ;
295
+ if ( this . _daemonStatus == DaemonStatus . DISCONNECTED ) {
296
+ this . _daemonStatus = DaemonStatus . CONNECTING ;
297
+
285
298
if ( ! ( await this . isServerAvailable ( ) ) ) {
286
299
await this . startInBackground ( ) ;
287
300
}
288
301
this . setUpConnection ( ) ;
302
+ this . _daemonStatus = DaemonStatus . CONNECTED ;
303
+ this . _daemonReady ( ) ;
304
+ } else if ( this . _daemonStatus == DaemonStatus . CONNECTING ) {
305
+ await this . _waitForDaemonReady ;
289
306
}
290
307
291
308
return new Promise ( ( resolve , reject ) => {
0 commit comments