File tree Expand file tree Collapse file tree 2 files changed +20
-13
lines changed Expand file tree Collapse file tree 2 files changed +20
-13
lines changed Original file line number Diff line number Diff line change @@ -33,9 +33,9 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
33
33
}
34
34
35
35
async * watchWorkspace ( req : WatchWorkspaceRequest , context : HandlerContext ) : AsyncIterable < WatchWorkspaceResponse > {
36
- const it = this . workspaceService . watchWorkspaces ( context . user . id ) ;
36
+ const it = this . workspaceService . watchWorkspaces ( context . user . id , { signal : context . signal } ) ;
37
37
context . signal . addEventListener ( "abort" , async ( ) => {
38
- await it . throw ( new Error ( "abort" ) ) ;
38
+ await it . throw ( new Error ( "abort" ) ) . then ( ) . catch ( ) ;
39
39
} ) ;
40
40
for await ( const info of it ) {
41
41
if ( ! info ) {
Original file line number Diff line number Diff line change @@ -733,29 +733,36 @@ export class WorkspaceService {
733
733
return urls ;
734
734
}
735
735
736
- public async * watchWorkspaces ( userId : string ) {
737
- let resolveNext : ( ( ) => void ) | null ;
738
- const next = ( ) => new Promise < void > ( ( res ) => ( resolveNext = res ) ) ;
739
- const nextOK = ( ) => {
736
+ public async * watchWorkspaces ( userId : string , opts : { signal : AbortSignal } ) {
737
+ const queue : WorkspaceInstance [ ] = [ ] ;
738
+ let resolveNext : ( ( d : WorkspaceInstance | void ) => void ) | null ;
739
+
740
+ const next = ( ) => new Promise < WorkspaceInstance | void > ( ( res ) => ( resolveNext = res ) ) ;
741
+ const nextOK = ( d : WorkspaceInstance | void ) => {
740
742
if ( resolveNext ) {
741
- resolveNext ( ) ;
743
+ resolveNext ( d ) ;
744
+ if ( d ) {
745
+ queue . push ( d ) ;
746
+ }
742
747
resolveNext = null ;
743
748
}
744
749
} ;
745
750
746
- const queue : WorkspaceInstance [ ] = [ ] ;
747
-
748
- const dispose = this . subscriber . listenForWorkspaceInstanceUpdates ( userId , ( _ctx , instance ) => {
749
- queue . push ( instance ) ;
751
+ let isStopped = false ;
752
+ opts . signal . addEventListener ( "abort" , ( ) => {
753
+ isStopped = true ;
750
754
nextOK ( ) ;
751
755
} ) ;
756
+ const dispose = this . subscriber . listenForWorkspaceInstanceUpdates ( userId , ( _ctx , instance ) => {
757
+ nextOK ( instance ) ;
758
+ } ) ;
752
759
753
760
try {
754
- while ( true ) {
761
+ while ( ! isStopped ) {
755
762
if ( queue . length ) {
756
763
yield queue . shift ( ) ;
757
764
} else {
758
- await next ( ) ;
765
+ yield next ( ) ;
759
766
}
760
767
}
761
768
} catch ( e ) {
You can’t perform that action at this time.
0 commit comments