Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make emit() wait for all listeners to settle #19

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function getListeners(instance, eventName) {
return events.get(eventName);
}

function hostReportError(err) {
Promise.reject(err);
}

class Emittery {
constructor() {
anyMap.set(this, new Set());
Expand Down Expand Up @@ -63,16 +67,17 @@ class Emittery {
const staticAnyListeners = [...anyListeners];

await resolvedPromise;
return Promise.all([
...staticListeners.map(async listener => {
if (listeners.has(listener)) {
return listener(eventData);
}

await Promise.all([
...staticListeners.map(listener => {
return listeners.has(listener) && new Promise(resolve => {
resolve(listener(eventData));
}).catch(hostReportError);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC I switched this to new Promise(), but @dinoboff do you think we should use try-await-catch instead?

Copy link
Contributor Author

@dinoboff dinoboff Feb 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer new Promise() in this case, but I don't mind.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, neither do I.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should use try-await-catch.

}),
...staticAnyListeners.map(async listener => {
if (anyListeners.has(listener)) {
return listener(eventName, eventData);
}
...staticAnyListeners.map(listener => {
return anyListeners.has(listener) && new Promise(resolve => {
resolve(listener(eventName, eventData));
}).catch(hostReportError);
})
]);
}
Expand Down
97 changes: 90 additions & 7 deletions test/_run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,58 @@ import test from 'ava';
import delay from 'delay';

module.exports = Emittery => {
let rejectionCollector;
let onUnhandledRejection;

test.before(() => {
onUnhandledRejection = function (err, promise) {
if (rejectionCollector) {
rejectionCollector.next(err);
promise.catch(() => {});
}
};

process.on('unhandledRejection', onUnhandledRejection);
});

test.after(() => {
process.removeListener('unhandledRejection', onUnhandledRejection);
onUnhandledRejection = undefined;
});

test.afterEach.always(() => {
if (rejectionCollector) {
rejectionCollector.throw();
}
});

async function catchUnhandledRejection(limit = 1) {
if (rejectionCollector) {
throw new Error('Already collecting unhandled rejections');
}

return new Promise((resolve, reject) => {
rejectionCollector = wait();
rejectionCollector.next();

function * wait() {
try {
const reasons = [];

while (reasons.length < limit) {
reasons.push(yield);
}

resolve(reasons);
} catch (err) {
reject(err);
} finally {
rejectionCollector = undefined;
}
}
});
}

test('on()', async t => {
const emitter = new Emittery();
const calls = [];
Expand Down Expand Up @@ -127,21 +179,52 @@ module.exports = Emittery => {
await t.throws(emitter.emit(42), TypeError);
});

test.cb('emit() - is async', t => {
t.plan(2);

test('emit() - is async', async t => {
const emitter = new Emittery();
let unicorn = false;

emitter.on('🦄', () => {
unicorn = true;
t.pass();
t.end();
});
const emitted = emitter.emit('🦄');

t.false(unicorn);
t.is(await emitted, undefined);
t.true(unicorn);
});

test.serial('emit() - settles once all handlers settle', async t => {
const rejectionsPromise = catchUnhandledRejection(1);
const emitter = new Emittery();
let settled = false;
emitter.on('🦄', () => Promise.reject(new Error()));
emitter.on('🦄', () => delay(10).then(() => {
settled = true;
}));

await emitter.emit('🦄');
t.true(settled);

await rejectionsPromise;
});

test.serial('emit() - let host handle listeners rejections', async t => {
const rejectionsPromise = catchUnhandledRejection(3);
const emitter = new Emittery();
const first = new Error('first rejection');
const second = new Error('second rejection');
const third = new Error('third rejection');

emitter.on('🦄', () => Promise.reject(first));
emitter.on('🦄', () => Promise.reject(second));
emitter.onAny(() => Promise.reject(third));

emitter.emit('🦄');

t.false(unicorn);
const errors = new Set(await rejectionsPromise);

t.true(errors.has(first));
t.true(errors.has(second));
t.true(errors.has(third));
});

test('emit() - calls listeners subscribed when emit() was invoked', async t => {
Expand Down