Skip to content

Commit dff563f

Browse files
committed
Streams: Add _flush capability to writable streams
1 parent b1a44df commit dff563f

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

lib/_stream_transform.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,7 @@ function Transform(options) {
126126
this._readableState.sync = false;
127127

128128
this.once('prefinish', function() {
129-
if (util.isFunction(this._flush))
130-
this._flush(function(er) {
131-
done(stream, er);
132-
});
133-
else
134-
done(stream);
129+
done(stream);
135130
});
136131
}
137132

lib/_stream_writable.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,14 @@ function needFinish(stream, state) {
434434
function prefinish(stream, state) {
435435
if (!state.prefinished) {
436436
state.prefinished = true;
437-
stream.emit('prefinish');
437+
if (util.isFunction(stream._flush)) {
438+
stream._flush(function (err) {
439+
if (err) return stream.emit('error', err)
440+
stream.emit('prefinish')
441+
})
442+
} else {
443+
stream.emit('prefinish');
444+
}
438445
}
439446
}
440447

test/simple/test-stream2-writable.js

+22
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,25 @@ test('finish is emitted if last chunk is empty', function(t) {
403403
w.write(Buffer(1));
404404
w.end(Buffer(0));
405405
});
406+
407+
test('write flush', function(t) {
408+
var w = new W();
409+
var writtenData = [];
410+
w._write = function(chunk, e, cb){
411+
writtenData.push(chunk.toString());
412+
cb();
413+
}
414+
w._flush = function(cb){
415+
writtenData.push('flushed');
416+
cb();
417+
}
418+
419+
w.write('start');
420+
w.end();
421+
422+
w.on('finish', function() {
423+
t.equal(writtenData[0], 'start');
424+
t.equal(writtenData[1], 'flushed');
425+
t.end();
426+
});
427+
});

0 commit comments

Comments
 (0)