Skip to content

Commit

Permalink
Avoid MaxListenersExceededWarning in Sequence
Browse files Browse the repository at this point in the history
Fixes #1345.
  • Loading branch information
novemberborn committed Apr 6, 2017
1 parent 8f80ed1 commit d821921
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/sequence.js
@@ -1,5 +1,29 @@
'use strict';

const beforeExitSubscribers = new Set();
const beforeExitHandler = () => {
for (const subscriber of beforeExitSubscribers) {
subscriber();
}
};
const onBeforeExit = subscriber => {
if (beforeExitSubscribers.size === 0) {
// Only listen for the event once, no matter how many Sequences are run
// concurrently.
process.on('beforeExit', beforeExitHandler);
}

beforeExitSubscribers.add(subscriber);
return {
dispose() {
beforeExitSubscribers.delete(subscriber);
if (beforeExitSubscribers.size === 0) {
process.removeListener('beforeExit', beforeExitHandler);
}
}
};
};

class Sequence {
constructor(runnables, bail) {
if (!Array.isArray(runnables)) {
Expand All @@ -14,16 +38,15 @@ class Sequence {
const iterator = this.runnables[Symbol.iterator]();

let activeRunnable;
const onBeforeExit = () => {
const beforeExit = onBeforeExit(() => {
if (activeRunnable.finishDueToInactivity) {
activeRunnable.finishDueToInactivity();
}
};
process.on('beforeExit', onBeforeExit);
});

let allPassed = true;
const finish = () => {
process.removeListener('beforeExit', onBeforeExit);
beforeExit.dispose();
return allPassed;
};

Expand Down

0 comments on commit d821921

Please sign in to comment.