@@ -49,13 +49,13 @@ export async function startWorkerRuntime(options: WorkerRuntimeOptions): Promise
4949 * - Receiving snapshot update pings (via socket)
5050 */
5151class DevSupervisor implements WorkerRuntime {
52- private config : DevConfigResponseBody ;
52+ private config ? : DevConfigResponseBody ;
5353 private disconnectPresence : ( ( ) => void ) | undefined ;
5454 private lastManifest ?: BuildManifest ;
5555 private latestWorkerId ?: string ;
5656
5757 /** Receive notifications when runs change state */
58- private socket : Socket < WorkerServerToClientEvents , WorkerClientToServerEvents > ;
58+ private socket ? : Socket < WorkerServerToClientEvents , WorkerClientToServerEvents > ;
5959 private socketIsReconnecting = false ;
6060
6161 /** Workers are versions of the code */
@@ -93,7 +93,7 @@ class DevSupervisor implements WorkerRuntime {
9393
9494 this . runLimiter = pLimit ( maxConcurrentRuns ) ;
9595
96- this . #createSocket( ) ;
96+ this . socket = this . #createSocket( ) ;
9797
9898 //start an SSE connection for presence
9999 this . disconnectPresence = await this . #startPresenceConnection( ) ;
@@ -105,7 +105,7 @@ class DevSupervisor implements WorkerRuntime {
105105 async shutdown ( ) : Promise < void > {
106106 this . disconnectPresence ?.( ) ;
107107 try {
108- this . socket . close ( ) ;
108+ this . socket ? .close ( ) ;
109109 } catch ( error ) {
110110 logger . debug ( "[DevSupervisor] shutdown, socket failed to close" , { error } ) ;
111111 }
@@ -187,6 +187,10 @@ class DevSupervisor implements WorkerRuntime {
187187 * For the latest version we will pull from the main queue, so we don't specify that.
188188 */
189189 async #dequeueRuns( ) {
190+ if ( ! this . config ) {
191+ throw new Error ( "No config, can't dequeue runs" ) ;
192+ }
193+
190194 if ( ! this . latestWorkerId ) {
191195 //try again later
192196 logger . debug ( `[DevSupervisor] dequeueRuns. No latest worker ID, trying again later` ) ;
@@ -409,13 +413,14 @@ class DevSupervisor implements WorkerRuntime {
409413 const wsUrl = new URL ( this . options . client . apiURL ) ;
410414 wsUrl . pathname = "/dev-worker" ;
411415
412- this . socket = io ( wsUrl . href , {
416+ const socket = io ( wsUrl . href , {
413417 transports : [ "websocket" ] ,
414418 extraHeaders : {
415419 Authorization : `Bearer ${ this . options . client . accessToken } ` ,
416420 } ,
417421 } ) ;
418- this . socket . on ( "run:notify" , async ( { version, run } ) => {
422+
423+ socket . on ( "run:notify" , async ( { version, run } ) => {
419424 logger . debug ( "[DevSupervisor] Received run notification" , { version, run } ) ;
420425
421426 this . options . client . dev . sendDebugLog ( run . friendlyId , {
@@ -434,10 +439,11 @@ class DevSupervisor implements WorkerRuntime {
434439
435440 await controller . getLatestSnapshot ( ) ;
436441 } ) ;
437- this . socket . on ( "connect" , ( ) => {
442+
443+ socket . on ( "connect" , ( ) => {
438444 logger . debug ( "[DevSupervisor] Connected to supervisor" ) ;
439445
440- if ( this . socket . recovered || this . socketIsReconnecting ) {
446+ if ( socket . recovered || this . socketIsReconnecting ) {
441447 logger . debug ( "[DevSupervisor] Socket recovered" ) ;
442448 eventBus . emit ( "socketConnectionReconnected" , `Connection was recovered` ) ;
443449 }
@@ -448,19 +454,21 @@ class DevSupervisor implements WorkerRuntime {
448454 controller . resubscribeToRunNotifications ( ) ;
449455 }
450456 } ) ;
451- this . socket . on ( "connect_error" , ( error ) => {
457+
458+ socket . on ( "connect_error" , ( error ) => {
452459 logger . debug ( "[DevSupervisor] Connection error" , { error } ) ;
453460 } ) ;
454- this . socket . on ( "disconnect" , ( reason , description ) => {
461+
462+ socket . on ( "disconnect" , ( reason , description ) => {
455463 logger . debug ( "[DevSupervisor] socket was disconnected" , {
456464 reason,
457465 description,
458- active : this . socket . active ,
466+ active : socket . active ,
459467 } ) ;
460468
461469 if ( reason === "io server disconnect" ) {
462470 // the disconnection was initiated by the server, you need to manually reconnect
463- this . socket . connect ( ) ;
471+ socket . connect ( ) ;
464472 } else {
465473 this . socketIsReconnecting = true ;
466474 eventBus . emit ( "socketConnectionDisconnected" , reason ) ;
@@ -472,6 +480,8 @@ class DevSupervisor implements WorkerRuntime {
472480 connections : Array . from ( this . socketConnections ) ,
473481 } ) ;
474482 } , 5000 ) ;
483+
484+ return socket ;
475485 }
476486
477487 #subscribeToRunNotifications( ) {
0 commit comments