I have a stream of files, and at some point i need to pause my stream, wait until it finish and buffers, and then continue.
Example:
var eventStream = require('event-stream')
gulp.task('test', () => {
eventStream.readArray([1, 2, 3, 4, 5])
.pipe(gulpTap((data) => {
console.log('d1:', data);
}))
.pipe(gulpTap((data) => {
console.log('d2:', data);
}))
.on('end', function () {
console.log('ended');
});
})
prints:
d1 1
d2 1
d1 2
d2 2
d1 3
d2 3
ended
when i want it to be like:
d1 1
d1 2
d1 3
d2 1
d2 2
d2 3
The reason here is that i want to collect some data from all files in one object, and then provide it to other objects, so i need a some kind of sync in the middle of pipe chain
You can do this with the help of through2
, eg:
const eventStream = require('event-stream');
const through = require('through2');
gulp.task('test', () => {
const input = [];
eventStream.readArray([1, 2, 3, 4, 5])
.pipe(through.obj((data, enc, done) => {
// Save data and remove from stream
input.push(data);
console.log('d1:', data);
return done();
}, function(done) {
// Return "buffered" data back to stream
input.forEach(this.push.bind(this));
return done();
}))
.pipe(through.obj((data, enc, done) => {
console.log('d2:', data);
return done(null, data);
}))
.on('end', function () {
console.log('ended');
});
});
Real-life example: https://github.com/sirlantis/gulp-order
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.