File tree Expand file tree Collapse file tree 3 files changed +26
-7
lines changed
Expand file tree Collapse file tree 3 files changed +26
-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
28
39export interface Disposable {
410 dispose ( ) : void
@@ -32,8 +38,21 @@ export class Emitter<T> {
3238 /**
3339 * Emit an event with a value.
3440 */
35- public emit ( value : T ) : void {
36- this . listeners . forEach ( ( cb ) => cb ( value ) )
41+ public async emit ( value : T ) : Promise < void > {
42+ let resolve : ( ) => void
43+ const promise = new Promise < void > ( ( r ) => ( resolve = r ) )
44+
45+ await Promise . all (
46+ this . listeners . map ( async ( cb ) => {
47+ try {
48+ await cb ( value , promise )
49+ } catch ( error ) {
50+ logger . error ( error . message )
51+ }
52+ } ) ,
53+ )
54+
55+ resolve ! ( )
3756 }
3857
3958 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 {
3939 process . on ( "SIGTERM" , ( ) => this . _onDispose . emit ( "SIGTERM" ) )
4040 process . on ( "exit" , ( ) => this . _onDispose . emit ( undefined ) )
4141
42- this . onDispose ( ( signal ) => {
42+ this . onDispose ( ( signal , wait ) => {
4343 // Remove listeners to avoid possibly triggering disposal again.
4444 process . removeAllListeners ( )
4545
46- // Let any other handlers run first then exit.
46+ // Try waiting for other handlers run first then exit.
4747 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 )
4950 } )
5051
5152 // 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