sindresorhus / gulp-filter

Filter files in a `vinyl` stream
MIT License
315 stars 37 forks source link

Cannot filter->restore several times with the same filter #70

Closed Tobl4 closed 8 years ago

Tobl4 commented 8 years ago

Hi,

I originally encountered this problem when I tried to declare all my filters at the beginning of the gulpfile. Imagine something like this:

filters = {
    js = gulpFilter("*.js", {restore:true}),
    css = gulpFilter("*.css", {restore:true})
}

While the gulp ran fine the first time, as soon as the gulp.watch fired I got an error (pretty new to gulp, I don't really know how to get useful error messages. If someone links a howto I'll gladly provide logs). However, through trial and error I'm almost certain that the error is with trying to either save or retrieve a restore-stream more than once.

In my situation, this can be avoided by putting the filter definition in the function call, creating a new instance each time (which feels kinda bad to me tbh) However, there is another case where this can not be avoided (pseudocode):

.pipe(filterJS)
.pipe(doA)
.pipe(filterJS.restore)
.pipe(doB)
.pipe(filterJS)
.pipe(doC)
.pipe(filterJS.restore)

I understand that this is an edgecase. At the same time, it seems pretty fixable (as fixable as code can be anyway) by clearing out a potentially used restore-cache before saving to it.

sindresorhus commented 8 years ago

That's how streams work. You can only consume them once. Factory function is the way to go.

Tobl4 commented 8 years ago

I was about to ask why it can't be done, but maybe stream references are required to be present until everything in that chain has been processed, not just until it is returned out of the filter function? (If you can't tell, I'm very inexperienced with streams. Really just trying to get gulp to build everything the way I want.) Doesn't prevent my first example, but it would make sense of why the second one can't be done. (and the first is already possible, just in a slightly different style).