Closed robbywashere-zz closed 2 years ago
@robbywashere Any reason why this was closed? Just curious...
@grantwwu Thanks for the interest. It didn't pass the build tests, and also I made a mistake in that it only worked with single events.
This is sort of the gist of what I have so far. This does work with multiple events, but that setImmediate(rs)
is a bit ugly. Any thoughts?
Also : iterator.return()
is not currently removing the registered listener(s)
const { EventEmitter } = require('events');
async function* streamify(
emitter,
events
) {
const eventsArray = [].concat(events);
let q = [];
for (let event of eventsArray) emitter.on(event,arg => q.push(arg));
while (true) {
await new Promise(rs=>setImmediate(rs));
while (q.length) {
yield q.shift();
}
}
}
The codebase has changed quite a bit since this PR was opened, so this isn't something we can accept at this time. Thanks very much for the suggestion!
This PR greatly reduces the complexity of
src/event-emitter-to-async-iterator
but relies on changes tosrc/test/test.js
and could use additional input on how to handle the unsubscription of events on the given event emitter.return()
should otherwise unsubscribe the listener but does not in this PR.