Skip to content

Commit 0537d93

Browse files
authored
Avoid maximum listeners exceeded warning (#4502)
1 parent bf1ff32 commit 0537d93

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/utils/hookActions.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventEmitter } from 'events';
12
import process from 'process';
23
import { HookAction, PluginDriver } from './PluginDriver';
34

@@ -19,6 +20,13 @@ function formatAction([pluginName, hookName, args]: HookAction): string {
1920
return action;
2021
}
2122

23+
// We do not directly listen on process to avoid max listeners warnings for
24+
// complicated build processes
25+
const beforeExitEvent = 'beforeExit';
26+
const beforeExitEmitter = new EventEmitter();
27+
beforeExitEmitter.setMaxListeners(0);
28+
process.on(beforeExitEvent, () => beforeExitEmitter.emit(beforeExitEvent));
29+
2230
export async function catchUnfinishedHookActions<T>(
2331
pluginDriver: PluginDriver,
2432
callback: () => Promise<T>
@@ -34,10 +42,10 @@ export async function catchUnfinishedHookActions<T>(
3442
)
3543
);
3644
};
37-
process.once('beforeExit', handleEmptyEventLoop);
45+
beforeExitEmitter.once(beforeExitEvent, handleEmptyEventLoop);
3846
});
3947

4048
const result = await Promise.race([callback(), emptyEventLoopPromise]);
41-
process.off('beforeExit', handleEmptyEventLoop!);
49+
beforeExitEmitter.off(beforeExitEvent, handleEmptyEventLoop!);
4250
return result;
4351
}

0 commit comments

Comments
 (0)