poelstra / ts-stream

Type-safe object streams with seamless support for backpressure, ending, and error handling
MIT License
65 stars 13 forks source link

.map() should be on Readable interface #1

Closed rogierschouten closed 9 years ago

rogierschouten commented 9 years ago

It's currently not possible to call .map() on something that returns a Readable stream while that's a prime use case. Suggest to put .map() and .filter() on the Readable interface, as they create a Readable stream from a Readable stream effectively.

poelstra commented 9 years ago

For this reason, .map() et al don't return a Readable, but a Stream. So instead of returning a Readable from your own functions, you could return a Readable.

The idea behind the very 'lean' Readable interface, is that that is actually all you need to provide in order to create your own Readable stream. All other operations (like .map()) are implemented on top of .forEach(). This would allow easy composing of different implementations, like with Promises/A+. Compare SomePromise <-> Thenable with Stream <-> Readable(+Writable).

On the other hand, I fully agree it's nice for languages like TypeScript to be able to specify that one is only interested in the 'read-parts' of a generic Stream.

I suppose a good solution would be to have another sort of Readable interface (and same for Writable) called ReadableStream extending Readable, and having things like .map() etc, then make Stream implement ReadableStream and WritableStream.

poelstra commented 9 years ago

WIth 0.3.0, replace Readable by ReadableStream in your code, and you should be good.