@@ -45,20 +45,20 @@ GlobalBus.on("event", (event) => {
4545
4646let server : Awaited < ReturnType < typeof Server . listen > > | undefined
4747
48- const eventStream = {
49- abort : undefined as AbortController | undefined ,
50- }
48+ const eventStreams = new Map < string , AbortController > ( )
49+
50+ function startEventStream ( directory : string ) {
51+ const id = crypto . randomUUID ( )
5152
52- const startEventStream = ( input : { directory : string ; workspaceID ?: string } ) => {
53- if ( eventStream . abort ) eventStream . abort . abort ( )
5453 const abort = new AbortController ( )
55- eventStream . abort = abort
5654 const signal = abort . signal
5755
58- ; ( async ( ) => {
56+ eventStreams . set ( id , abort )
57+
58+ async function run ( ) {
5959 while ( ! signal . aborted ) {
6060 const shouldReconnect = await Instance . provide ( {
61- directory : input . directory ,
61+ directory,
6262 init : InstanceBootstrap ,
6363 fn : ( ) =>
6464 new Promise < boolean > ( ( resolve ) => {
@@ -77,7 +77,10 @@ const startEventStream = (input: { directory: string; workspaceID?: string }) =>
7777 }
7878
7979 const unsub = Bus . subscribeAll ( ( event ) => {
80- Rpc . emit ( "event" , event as Event )
80+ Rpc . emit ( "event" , {
81+ id,
82+ event : event as Event ,
83+ } )
8184 if ( event . type === Bus . InstanceDisposed . type ) {
8285 settle ( true )
8386 }
@@ -104,14 +107,24 @@ const startEventStream = (input: { directory: string; workspaceID?: string }) =>
104107 await sleep ( 250 )
105108 }
106109 }
107- } ) ( ) . catch ( ( error ) => {
110+ }
111+
112+ run ( ) . catch ( ( error ) => {
108113 Log . Default . error ( "event stream error" , {
109114 error : error instanceof Error ? error . message : error ,
110115 } )
111116 } )
117+
118+ return id
112119}
113120
114- startEventStream ( { directory : process . cwd ( ) } )
121+ function stopEventStream ( id : string ) {
122+ const abortController = eventStreams . get ( id )
123+ if ( ! abortController ) return
124+
125+ abortController . abort ( )
126+ eventStreams . delete ( id )
127+ }
115128
116129export const rpc = {
117130 async fetch ( input : { url : string ; method : string ; headers : Record < string , string > ; body ?: string } ) {
@@ -154,12 +167,19 @@ export const rpc = {
154167 async reload ( ) {
155168 await Config . invalidate ( true )
156169 } ,
157- async setWorkspace ( input : { workspaceID ?: string } ) {
158- startEventStream ( { directory : process . cwd ( ) , workspaceID : input . workspaceID } )
170+ async subscribe ( input : { directory : string | undefined } ) {
171+ return startEventStream ( input . directory || process . cwd ( ) )
172+ } ,
173+ async unsubscribe ( input : { id : string } ) {
174+ stopEventStream ( input . id )
159175 } ,
160176 async shutdown ( ) {
161177 Log . Default . info ( "worker shutting down" )
162- if ( eventStream . abort ) eventStream . abort . abort ( )
178+
179+ for ( const id of [ ...eventStreams . keys ( ) ] ) {
180+ stopEventStream ( id )
181+ }
182+
163183 await Instance . disposeAll ( )
164184 if ( server ) await server . stop ( true )
165185 } ,
0 commit comments