-
Notifications
You must be signed in to change notification settings - Fork 19
Expose read and on('readable') #5
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
Conversation
Are you guys open to using purescript-spec for the tests? I think we can improve the coverage and readability a lot by going down that route. |
src/Node/Stream.js
Outdated
exports.onReadable = function(s) { | ||
return function(f) { | ||
return function() { | ||
s.on('readable', function() { |
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 think just s.on('readable', f)
should work here, right?
Sorry for the nitpicks, this looks good 👍 just checking, this doesn't break anything, right? I'd rather not depend on |
src/Node/Stream.purs
Outdated
data Chunk | ||
data Data = BufferData Buffer | ||
| StringData String | ||
| UnknownData Chunk |
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.
What do you think about using Foreign
here?
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.
Instead of defining a new type Chunk
, I mean.
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 don't mind, but it might make the code a bit harder to read because of the ambiguity of Foreign
.
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.
My impression is that object mode lets you put any javascript value through a stream (apart from null
which has a special meaning for the end of the stream). Given this, I think Foreign
is appropriate; that is what the type represents, and you also get plenty of useful functions for turning Foreign
values into data types that you can do stuff with.
Why do you say Foreign
is more ambiguous than your Chunk
?
Looking at the node API again though, if we were going to support object mode, I think I would rather have an entirely separate data type for streams in object mode anyway, especially since no Node APIs use them. I don't want to have to worry about whether a stream is in object mode when it ought to be possible to rule that out at compile time.
What about it is WIP? |
Thank you for your valuable feedback. I checked it in before heading to work today so I knew there would be some rough edges (like e.g. forgetting to match |
Cool :) Also, do you have a use case for the |
Yeah I was thinking of objectMode, but I agree with what you are saying about guessing usecases. |
Alright, I forced pushed my fixes. I have made all changes as per review and added another test. Lucky I did, it caught another problem. If you are happy with this version, I am happy to take away the "WIP" from the title and have it merged. |
Actually now that we disregard object mode, we are back to two constructors, and could use |
Great :) I was just thinking the same thing about |
I agree. I removed the ADT and used a type alias. |
I see a problem though between |
src/Node/Stream.js
Outdated
return function(f) { | ||
return function() { | ||
r.on('data', function(data) { | ||
f(readChunk(data)); |
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 think this is missing a ()
at the end of the line.
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.
Good catch... bummer the tests did not pick up on it.
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.
Did the tests catch this? If not, could you add one please?
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.
Hmmm, actually not sure how to test this, since we are not using Aff
or ContT
. In other words, if no callback is called, Eff
don't care.
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.
Oh, good point. Ok, let's not worry then.
I'd like |
src/Node/Stream.purs
Outdated
onDataEither :: forall r eff. Readable r (err :: EXCEPTION | eff) -> (Either String Buffer -> Eff (err :: EXCEPTION | eff) Unit) -> Eff (err :: EXCEPTION | eff) Unit | ||
onDataEither r cb = onData' r cb | ||
|
||
-- | Listen for `data` events, returning |
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.
Maybe just inline this definition into onDataEither
now?
Alright so |
src/Node/Stream.purs
Outdated
-- | A duplex (readable _and_ writable stream) | ||
type Duplex = Stream (read :: Read, write :: Write) | ||
|
||
type Data = Either String Buffer |
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.
What does this type synonym (and also ChunkReader
) give us over writing out the real types? I generally tend to avoid type synonyms like these, as then there's more to keep in your head when you're reading the code.
src/Node/Stream.purs
Outdated
case v of | ||
Nothing -> pure Nothing | ||
Just (Left _) -> throw "Stream encoding should not be set" | ||
Just (Right buf) -> pure <$> (unsafeInterleaveEff $ Buffer.toString enc buf) |
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.
How would you feel about using Just
rather than pure
here?
Alright, I'll do proper commits now until we're done. Then I'll squash and then we can merge |
Reason being it's easier for us to review how we got here. |
Ok, absolute last thing and then we're done - the comment on |
Also, if you are going to just update it, I think it ought to go on the function itself, rather than the |
Sweet, I just removed it. It's also squashed. |
Looks great, thanks very much! Sorry this was such an ordeal, it's just that it's quite high up in the dependency tree of purescript-node packages so I think it pays to be extra careful. 🎉 |
No worries, I enjoyed working through this and learned a thing or two. |
Sill WIP, but feedback welcome.