0

I just read that if a stream uses 'data' or 'end' listeners it switches to "classic" mode and stream-handbook says:

Note that whenever you register a "data" listener, you put the stream into compatability mode so you lose the benefits of the new streams2 api

So what's the best way to use benefits of new streams api? If I'm currently doing this:

gulp.src(["./src/server/**/*.coffee"])
    .pipe(coffee  bare: true ).on("error", gutil.log)
    .pipe(gulp.dest "./bin/server")
    .on 'end',-> gutil.log "successfully compiled server coffeescript"

How can I do the same thing without registering 'end' listener

Charles
  • 50,943
  • 13
  • 104
  • 142
iLemming
  • 34,477
  • 60
  • 195
  • 309

1 Answers1

2

Only calling .resume()/.pause() or adding a 'data' listener will switch the streams2 stream to a streams1 stream. You can listen for 'end' without affecting anything.

In your particular example, even if it did switch it would not affect you since you are simply piping, which works in both streams1 and streams2 modes.

Additionally, you may not see an 'end' event if that last stream (gulp.dest "./bin/server") is not a Duplex stream or you do not consume data from it. If it is only a Writable stream, you should listen for 'finish' instead ('end' is only emitted on Readable streams).

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • 1
    Yes, but what do I do if a stream doesn't have `'end'` event at all? Is there a way to detect the end of it? – iLemming Jun 23 '14 at 23:01
  • If that last stream (`gulp.dest "./bin/server"`) is only a Writable stream, then you should listen instead for 'finish'. 'end' is only for Readable streams. – mscdex Jun 23 '14 at 23:02
  • changing `on 'end'` to `on 'finish'` didn't work for me – iLemming Jun 23 '14 at 23:08
  • 1
    Try appending `.resume()` after your `.on 'end'`/`.on 'finish'` handler. It turns out that `gulp.dest` returns a Duplex stream (to allow you to pipe to other gulp streams), which will need to be drained in order to get the events you're looking for, and calling `resume()` is an easy way to do that. – mscdex Jun 24 '14 at 01:47