77
88import type { Platform } from '../types' ;
99
10- const fs = require ( 'fs' ) ;
1110const EventEmitter = require ( 'events' ) ;
1211const Events = require ( './events' ) ;
1312const createForkProcess = require ( './createForkProcess' ) ;
@@ -32,12 +31,20 @@ module.exports = class Fork extends EventEmitter {
3231 socket : WebSocket ;
3332 enqueuedMessages : any [ ] ;
3433 isProcessing : boolean ;
34+ options : any ;
3535
3636 constructor ( { platform, options } : ForkConstructorArgs ) {
3737 super ( ) ;
38+ this . isProcessing = true ;
39+ this . enqueuedMessages = [ ] ;
40+ this . platform = platform ;
41+ this . options = options ;
42+ }
3843
44+ async init ( ) {
3945 if ( ! transportServer ) {
40- transportServer = createWebSocketServer ( ) ;
46+ // eslint-disable-next-line require-atomic-updates
47+ transportServer = await createWebSocketServer ( ) ;
4148
4249 // WebSocket connection is established after the Fork is created.
4350 transportServer . on ( 'connection' , ( socket , req ) => {
@@ -58,17 +65,15 @@ module.exports = class Fork extends EventEmitter {
5865 } ) ;
5966 }
6067
61- this . isProcessing = true ;
62- this . enqueuedMessages = [ ] ;
63- this . platform = platform ;
68+ const { port } = transportServer . address ( ) ;
6469 this . process = createForkProcess (
65- platform ,
70+ this . platform ,
6671 __dirname ,
67- transportServer . options . server . address ( ) ,
68- options
72+ `localhost: ${ port } ` ,
73+ this . options
6974 ) ;
7075
71- forks [ platform ] = this ;
76+ forks [ this . platform ] = this ;
7277 }
7378
7479 setSocket ( socket : WebSocket ) {
@@ -116,10 +121,6 @@ module.exports = class Fork extends EventEmitter {
116121 }
117122
118123 if ( transportServer && ! Object . keys ( forks ) . length ) {
119- const socketAddress = transportServer . options . server . address ( ) ;
120- if ( fs . existsSync ( socketAddress ) ) {
121- fs . unlinkSync ( socketAddress ) ;
122- }
123124 transportServer . close ( ) ;
124125 }
125126 }
0 commit comments