-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add EventEmitter.on to async iterate over events
Port of nodejs/node#27994
- Loading branch information
1 parent
48e3d18
commit a205441
Showing
3 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
'use strict'; | ||
|
||
var common = require('./common'); | ||
var assert = require('assert'); | ||
var EventEmitter = require('../').EventEmitter; | ||
var on = require('../').on; | ||
|
||
async function basic() { | ||
var ee = new EventEmitter(); | ||
process.nextTick(function () { | ||
ee.emit('foo', 'bar'); | ||
// 'bar' is a spurious event, we are testing | ||
// that it does not show up in the iterable | ||
ee.emit('bar', 24); | ||
ee.emit('foo', 42); | ||
}); | ||
|
||
var iterable = on(ee, 'foo'); | ||
|
||
var expected = [['bar'], [42]]; | ||
|
||
for await (var event of iterable) { | ||
var current = expected.shift(); | ||
|
||
assert.deepStrictEqual(current, event); | ||
|
||
if (expected.length === 0) { | ||
break; | ||
} | ||
} | ||
assert.strictEqual(ee.listenerCount('foo'), 0); | ||
assert.strictEqual(ee.listenerCount('error'), 0); | ||
} | ||
|
||
async function error() { | ||
var ee = new EventEmitter(); | ||
var _err = new Error('kaboom'); | ||
process.nextTick(function () { | ||
ee.emit('error', _err); | ||
}); | ||
|
||
var iterable = on(ee, 'foo'); | ||
let looped = false; | ||
let thrown = false; | ||
|
||
try { | ||
// eslint-disable-next-line no-unused-vars | ||
for await (var event of iterable) { | ||
looped = true; | ||
} | ||
} catch (err) { | ||
thrown = true; | ||
assert.strictEqual(err, _err); | ||
} | ||
assert.strictEqual(thrown, true); | ||
assert.strictEqual(looped, false); | ||
} | ||
|
||
async function errorDelayed() { | ||
var ee = new EventEmitter(); | ||
var _err = new Error('kaboom'); | ||
process.nextTick(function () { | ||
ee.emit('foo', 42); | ||
ee.emit('error', _err); | ||
}); | ||
|
||
var iterable = on(ee, 'foo'); | ||
var expected = [[42]]; | ||
let thrown = false; | ||
|
||
try { | ||
for await (var event of iterable) { | ||
var current = expected.shift(); | ||
assert.deepStrictEqual(current, event); | ||
} | ||
} catch (err) { | ||
thrown = true; | ||
assert.strictEqual(err, _err); | ||
} | ||
assert.strictEqual(thrown, true); | ||
assert.strictEqual(ee.listenerCount('foo'), 0); | ||
assert.strictEqual(ee.listenerCount('error'), 0); | ||
} | ||
|
||
async function throwInLoop() { | ||
var ee = new EventEmitter(); | ||
var _err = new Error('kaboom'); | ||
|
||
process.nextTick(function () { | ||
ee.emit('foo', 42); | ||
}); | ||
|
||
try { | ||
for await (var event of on(ee, 'foo')) { | ||
assert.deepStrictEqual(event, [42]); | ||
throw _err; | ||
} | ||
} catch (err) { | ||
assert.strictEqual(err, _err); | ||
} | ||
|
||
assert.strictEqual(ee.listenerCount('foo'), 0); | ||
assert.strictEqual(ee.listenerCount('error'), 0); | ||
} | ||
|
||
async function next() { | ||
var ee = new EventEmitter(); | ||
var iterable = on(ee, 'foo'); | ||
|
||
process.nextTick(function() { | ||
ee.emit('foo', 'bar'); | ||
ee.emit('foo', 42); | ||
iterable.return(); | ||
}); | ||
|
||
var results = await Promise.all([ | ||
iterable.next(), | ||
iterable.next(), | ||
iterable.next() | ||
]); | ||
|
||
assert.deepStrictEqual(results, [{ | ||
value: ['bar'], | ||
done: false | ||
}, { | ||
value: [42], | ||
done: false | ||
}, { | ||
value: undefined, | ||
done: true | ||
}]); | ||
|
||
assert.deepStrictEqual(await iterable.next(), { | ||
value: undefined, | ||
done: true | ||
}); | ||
} | ||
|
||
async function nextError() { | ||
var ee = new EventEmitter(); | ||
var iterable = on(ee, 'foo'); | ||
var _err = new Error('kaboom'); | ||
process.nextTick(function() { | ||
ee.emit('error', _err); | ||
}); | ||
var results = await Promise.allSettled([ | ||
iterable.next(), | ||
iterable.next(), | ||
iterable.next() | ||
]); | ||
assert.deepStrictEqual(results, [{ | ||
status: 'rejected', | ||
reason: _err | ||
}, { | ||
status: 'fulfilled', | ||
value: { | ||
value: undefined, | ||
done: true | ||
} | ||
}, { | ||
status: 'fulfilled', | ||
value: { | ||
value: undefined, | ||
done: true | ||
} | ||
}]); | ||
assert.strictEqual(ee.listeners('error').length, 0); | ||
} | ||
|
||
async function iterableThrow() { | ||
var ee = new EventEmitter(); | ||
var iterable = on(ee, 'foo'); | ||
|
||
process.nextTick(function () { | ||
ee.emit('foo', 'bar'); | ||
ee.emit('foo', 42); // lost in the queue | ||
iterable.throw(_err); | ||
}); | ||
|
||
var _err = new Error('kaboom'); | ||
let thrown = false; | ||
|
||
assert.throws(function () { | ||
// No argument | ||
iterable.throw(); | ||
}, { | ||
message: 'The "EventEmitter.AsyncIterator" property must be' + | ||
' an instance of Error. Received undefined', | ||
name: 'TypeError' | ||
}); | ||
|
||
var expected = [['bar'], [42]]; | ||
|
||
try { | ||
for await (var event of iterable) { | ||
assert.deepStrictEqual(event, expected.shift()); | ||
} | ||
} catch (err) { | ||
thrown = true; | ||
assert.strictEqual(err, _err); | ||
} | ||
assert.strictEqual(thrown, true); | ||
assert.strictEqual(expected.length, 0); | ||
assert.strictEqual(ee.listenerCount('foo'), 0); | ||
assert.strictEqual(ee.listenerCount('error'), 0); | ||
} | ||
|
||
async function run() { | ||
var funcs = [ | ||
basic, | ||
error, | ||
errorDelayed, | ||
throwInLoop, | ||
next, | ||
nextError, | ||
iterableThrow, | ||
]; | ||
|
||
for (var fn of funcs) { | ||
await fn(); | ||
} | ||
} | ||
|
||
module.exports = run(); |