The other feature is that this is all in node core. Isaacs 0.9 streams will
have the notion of a transform stream.
We can also petition node core to make fs.createReadStream(dirname) work
because a directory is just a fd with a list of files so it should work.
Then the program becomes
```
var fs = require("fs")
, d = require("domain").create()
, Transform = require("stream").Transform
d.run(function () {
var onlyJsFiles = new Transform()
, mapToFiles = new Transform()
, addNewLine = new Transform()
onlyJsFiles._transform = function(fileName, output, end) {
if (fileName.slice(-3) === ".js" && (fileName !== "lib.js")) {
output(fileName)
}
end()
}
mapToFiles._transform = function(fileName, output, end) {
fs.readFile(fileName, end)
}
addNewLine._transform = function (file, output, end) {
end(null, file + "\n")
}
fs.createReadStream(__dirname)
.pipe(onlyJsFiles)
.pipe(mapToFiles)
.pipe(addNewLine)
.pipe(fs.createWriteStream(__dirname + "/lib.js"))
})
d.on("error", function ignoreIt() {})
```
On Wed, Oct 3, 2012 at 9:39 AM, Jake Verbaten <[email protected]> wrote:
> Streams are the better abstraction for that.
>
> Notice that with the streams version you can actually start writing to the
> disk once you've successfully loaded a single file.
>
> ```
> var chain = require("chain-stream")
> , fs = require("fs")
> , DirStream = require("dir-stream") // to be written
> , d = require("domain").create()
>
> d.run(function () {
> chain(DirStream(__dirname))
> .filter(function (fileName) {
> return (file.slice(-3) === ".js" && (file !== "lib.js"))
> })
> .map(readFile)
> .pipe(fs.createWriteStream(__dirname + "/lib.js"))
> })
>
> d.on("error", function ignoreIt() {})
> ```
>
> On Wed, Oct 3, 2012 at 1:11 AM, Mariusz Nowak <[email protected]> wrote:
>
>> Raynos, I have a feeling that you are comparing apples and oranges.
>> Streams are awesome, Promises are awesome but it's not the same, and not
>> every use case can be addressed on equal level by both of them.
>>
>> From my experience there can be promises that are also a streams, but
>> among them are also promises that fulfill before stream ends. There can
>> also be promises that are not streams, of course you may represent them as
>> stream but if they bring no value and just emit 'end', it doesn't fit well
>> stream concept, does it?
>>
>> Same way you can just use callbacks instead of promises, but the point is
>> that promises gives you better abstraction, so it's easier to construct
>> complicated flow with it and maintain it. See this:
>> https://github.com/medikoo/deferred#promises-approach How would you
>> write it with streams instead? Would it be more friendly than callbacks
>> version: https://github.com/medikoo/deferred#plain-nodejs ?
>>
>> On Wednesday, October 3, 2012 6:40:02 AM UTC+2, Raynos wrote:
>>>
>>> https://github.com/Raynos/**promise-stream<https://github.com/Raynos/promise-stream>
>>>
>>> If you want to turn a stream into a promise you can.
>>>
>>> This mainly illustrates how streams and promises are similar for those
>>> that like promises.
>>>
>>> It also shows a comparison between how you flow async code through
>>> promises and streams
>>>
>>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en