-
Notifications
You must be signed in to change notification settings - Fork 147
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
Implement asNodeWritable. #359
base: master
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,21 @@ | |||
var Writable = require('stream').Writable, |
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.
This won't be compatible with pre-0.10 Nodes, do we support those?
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.
We're not running any of our tests on Node 0.8 so I guess by virtue of that we don't.
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.
Once upon a time we did (back in 1.x according to git history). I think we dropped it because one of our devDependencies
is no longer compatible with the version of npm
that comes with 0.8 on travis.
If you're concerned we could make asNodeWritable
return this
if require('stream').Writable
doesn't exist. Thoughts?
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.
Well, there's always readable-stream, but I'm not too concerned. I think we're fine ignoring old Nodes, as long as we make it clear what we support.
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.
Ok. We'll keep using Writable
then.
Any thoughts on the The only reason to have it is for slightly more compatibility with |
My case was passing a stream to a third-party library which passed in a callback in the third argument. The callback called "result()" of a promise wrapper, so needed to get called. For me it would be great if, whether or not you accepted a callback as the 2nd argument, if the 2nd argument wasn't a function you'd treat the 3rd argument as a callback (if present). (Also might not you sometimes wrap a node stream which could make use of the encoding? Doesn't matter for me but just a thought.) Thanks for the quick work here! (PS end() with a callback in third argument possible would also be great :)) |
* @param x - the value to write to the Stream | ||
* @param {any} x - the value to write to the Stream | ||
* @param {String} encoding - (optional) Ignored. This parameter is only here to | ||
* match the Writable signature. |
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.
(suggested edit -- see below): If it is callable, it is taken as the callback argument.
My point is that you can always use
We don't wrap node streams. We consume from them. Even the
You should be able to get this behavior from var s = _();
var writable = s.asNodeWritable();
writable.write(1);
writable.end(2, null, function () {
console.log('finished');
});
s.take(1).each(_.log);
// => 1
// Won't see the finished. |
Ok -- thats fine. (One might say that having "write" at all already muddies the message. :)) The laziness you describe seems ok in your example, but "end" is called explicitly on "s" shouldn't "writable" listen for that and call the callback then, as the data has been explicitly discarded?
UPDATE: Probably the answer is "no", as the data is still available. End just means "no more data". I guess there is no "shutdown" interface expect to read all the data?
|
This brings up an interesting question. What should happen here? var s = _();
var writable = s.asNodeWritable();
writable.write(1, null, callback);
writable.write(2, null, callback);
writable.write(3, null, callback);
s.destroy(); Should those callbacks be called? I see three choices
|
By the way, |
Ok ... I wouldn't expect the data would be read. But I would expect a stream whose sink has hung up on it would say something --either "finish" event with an error (though the node documentation doesn't say anything about this) or an "error" event. After all, if your task is to send a message, you can wait patiently (maybe forever unless you timeout) until its read but might want to take some action if its guaranteed not to be read. I would think that |
The event is exposed in v3.0 (in development) as
In general, I agree. Here's my concern. In 3.0, consumers will automatically destroy the parent once it's done. This allows the source to clean up any resources that it is holding. Consumers may hang up on their source for reasons other than an error, and the source should respond by simply stopping to generate data. There is no equivalent concept for Writables; the only way to signal that a Writable won't be accepting more data is with an error. For example, consider this var s = _();
libraryCall(s.asNodeWritable());
s.take(1).each(_.log);
function libraryCall(writable) {
writable.write(1);
writable.end(2);
} In the above example, So...
Now that I think about it, the approach of least surprise is probably (2) even though it doesn't preserve laziness. In the end, we're exposing a Highland stream via an different API that doesn't support the same semantics, so something has to give. |
Implements
asNodeWritable
as discussed in #358.This PR also adds a callback argument to
write
that gets called when the value written is sent downstream.Advice needed
Currently,
write
has the signaturewrite(token, encoding, callback)
to match theWritable#write
signature. Since we don't care about the encoding, and since Highland streams are not Writable anyway, should we even have theencoding
argument? We haveasNodeWritable
, so we should be deprecating the use of writable Highland streams as a Writable anyway.