File tree Expand file tree Collapse file tree 3 files changed +22
-7
lines changed Expand file tree Collapse file tree 3 files changed +22
-7
lines changed Original file line number Diff line number Diff line change 1
- import { Callback } from "./types"
1
+ import { logger } from "@coder/logger"
2
+
3
+ /**
4
+ * Event emitter callback. Called with the emitted value and a promise that
5
+ * resolves when all emitters have finished.
6
+ */
7
+ export type Callback < T , R = void | Promise < void > > = ( t : T , p : Promise < void [ ] > ) => R
2
8
3
9
export interface Disposable {
4
10
dispose ( ) : void
@@ -32,8 +38,17 @@ export class Emitter<T> {
32
38
/**
33
39
* Emit an event with a value.
34
40
*/
35
- public emit ( value : T ) : void {
36
- this . listeners . forEach ( ( cb ) => cb ( value ) )
41
+ public emit ( value : T ) : Promise < void [ ] > {
42
+ const promise = Promise . all (
43
+ this . listeners . map ( async ( cb ) => {
44
+ try {
45
+ await cb ( value , promise )
46
+ } catch ( error ) {
47
+ logger . error ( error . message )
48
+ }
49
+ } ) ,
50
+ )
51
+ return promise
37
52
}
38
53
39
54
public dispose ( ) : void {
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -39,13 +39,14 @@ export class IpcMain {
39
39
process . on ( "SIGTERM" , ( ) => this . _onDispose . emit ( "SIGTERM" ) )
40
40
process . on ( "exit" , ( ) => this . _onDispose . emit ( undefined ) )
41
41
42
- this . onDispose ( ( signal ) => {
42
+ this . onDispose ( ( signal , wait ) => {
43
43
// Remove listeners to avoid possibly triggering disposal again.
44
44
process . removeAllListeners ( )
45
45
46
- // Let any other handlers run first then exit.
46
+ // Try waiting for other handlers run first then exit.
47
47
logger . debug ( `${ parentPid ? "inner process" : "wrapper" } ${ process . pid } disposing` , field ( "code" , signal ) )
48
- setTimeout ( ( ) => this . exit ( 0 ) , 0 )
48
+ wait . then ( ( ) => this . exit ( 0 ) )
49
+ setTimeout ( ( ) => this . exit ( 0 ) , 5000 )
49
50
} )
50
51
51
52
// Kill the inner process if the parent dies. This is for the case where the
You can’t perform that action at this time.
0 commit comments