A Writable stream that flushes before emitting finish
npm install flushwritableSponsored by Leadnomics.
_flush method that is called beforefinish event isFlushWritable is a drop-in replacement for stream.Writable that implements
a _flush call that behaves exactly how Transform._flush does. It's called
with a callback, waits for the callback to be called, and _then_ firesfinish (or error if an error was passed). No additional execution after
the finish event, no implementing nonstandard event types, no chaining a
shell Transform stream before the Writable to hijack its _flush call. And
it's fully futureproof against the Node.js team actually adding a _flush
method to the native stream.Writable in a later version of Node, so you don't
have to worry about your code breaking on upgrade.
finish be emitted. When that comes in, it blocks that event_flush if it's defined.The callback it passes to _flush will trigger finish to actually be
emitted. If that callback is called with a truthy first argument, error is
emitted instead. All other events pass right through and are emitted as
expected. If a future version of node adds a Writable.prototype._flush
method, the whole thing short-circuits and native functionality takes over.
npm install flushwritable --save
_flush(cb) function!``javascript
var FlushWritable = require('flushwritable'),
util = require('util');
function MyWriteStream(opts) {
FlushWritable.call(this, opts);
this._buffer = [];
}
util.inherits(MyWriteStream, FlushWritable);
MyWriteStream.prototype._flush = function(cb) {
writeBufferSomewhere(this._buffer, cb);
};
MyWriteStream.prototype._write = function(data, encoding, cb) {
this._buffer.push(data);
cb();
};
``