Skip to content

Commit

Permalink
Avoid maximum listeners exceeded warning (#4502)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 19, 2022
1 parent bf1ff32 commit 0537d93
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/utils/hookActions.ts
@@ -1,3 +1,4 @@
import { EventEmitter } from 'events';
import process from 'process';
import { HookAction, PluginDriver } from './PluginDriver';

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

// We do not directly listen on process to avoid max listeners warnings for
// complicated build processes
const beforeExitEvent = 'beforeExit';
const beforeExitEmitter = new EventEmitter();
beforeExitEmitter.setMaxListeners(0);
process.on(beforeExitEvent, () => beforeExitEmitter.emit(beforeExitEvent));

export async function catchUnfinishedHookActions<T>(
pluginDriver: PluginDriver,
callback: () => Promise<T>
Expand All @@ -34,10 +42,10 @@ export async function catchUnfinishedHookActions<T>(
)
);
};
process.once('beforeExit', handleEmptyEventLoop);
beforeExitEmitter.once(beforeExitEvent, handleEmptyEventLoop);
});

const result = await Promise.race([callback(), emptyEventLoopPromise]);
process.off('beforeExit', handleEmptyEventLoop!);
beforeExitEmitter.off(beforeExitEvent, handleEmptyEventLoop!);
return result;
}

0 comments on commit 0537d93

Please sign in to comment.