-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
stream: add Transform.by utility function #28501
Conversation
52dc5bd
to
e85b78d
Compare
edit: tests passed on a subsequent run (which was a docs fix), guessing its infrastructure failurethe two failing JS tests seem to be failing due to being run against a binary that doesn't have the compiled changes in... maybe it's a caching thing? |
Should we ship this as experimental to start? |
given that https://github.com/tc39/proposal-iterator-helpers is likely to hit stage 2 at the end of this month, and would allow something looking like this: someReadable[Symbol.asyncIterator]()
.map(async (x) => { whatever })
.forEach((item) => writable.push(item)); perhaps instead of adding this api we should just wait for the iterator methods to mature? |
@devsnek I think the two requirements are orthogonal. This PR is all about enabling async iterators and async functions to "play decently" with the Stream ecosystem of modules. In other term, it enables to "use The added support for the iterator helpers are needed to improve the language overall, but they won't help in improving the Stream ecosystem.
As a side note, I don't see anything specific to |
@mcollina if that is the case this should probably accept any function. (also, all the methods being added for sync iterables are also being added for async iterables, and they propogate errors to the end of the chain) |
@davidmarkclements: When I try to run this locally the test never finishes: out/Release/node test/parallel/test-transform-by.js |
This probably got broken by f8018f2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this can be implemented in just a few lines using Readble.from
, e.g. nxtedition#12. Passes all tests expect for a few ones I'm not sure are needed (e.g. invalid arg type checks should probably be moved into from
).
const from = require('internal/streams/from');
Transform.by = function by(asyncGeneratorFn, opts) {
let _resolve;
let _promise = new Promise((resolve) => _resolve = resolve);
return from(Duplex, asyncGeneratorFn(async function*() {
while (true) {
const { chunk, done, cb } = await _promise;
if (done) return cb();
yield chunk;
_promise = new Promise((resolve) => _resolve = resolve);
cb();
}
}()), {
objectMode: true,
autoDestroy: true,
...opts,
write: (chunk, encoding, cb) => _resolve({ chunk, done: false, cb }),
final: (cb) => _resolve({ done: true, cb })
});
};
Any reason why this is not simply called |
The
|
very cool - I noticed some tests were commented out. I think the one for encoding in particular needs to be supported. Can you speak more to why the others are commented out? The checks for whether an async function generator is being passed were added at the request of prior reviews, I'd rather honour those initial requests than vacillate on that. Also, I'd be interested in the performance impact of each approach. |
I need to better understand this one. I'll follow up with your comment on the PR.
I agree with them. But I think they should be consistent with and live in
Maybe a benchmark under /benchmark then? |
I made a benchmark but it doesn't work with the version in this PR for some reason? nxtedition@9fbb8d4 |
I'm not sure what the best course of action here is. @davidmarkclements would you be interested in cherry-picking my proposed changes into this PR? |
@mcollina: Is this something we still want considering the recent added functionality in |
I would say so, IMHO it serves a different purpose. |
@davidmarkclements are you still interested in continuing work on this PR or would you mind if I took over? |
8ae28ff
to
2935f72
Compare
Ping @davidmarkclements ... still want to do this? |
@jasnell I've just remembered I was doing this, can we get it in before v14 goes to Active? |
Yes we can! |
Hi, this looks super nice! Is there any plans to get this merged at some point? |
@safareli would you like to work on it? I think it just requires somebody willing to update this and do a fresh PR. I don't think @davidmarkclements will work on this. |
Closing in favor of #38696 |
Analogous to
Readable.from
,Transform.by
creates transform streamsfrom async function generators.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes