mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 21:46:48 +00:00

Adds the ability to for write streams to have an _final method which acts similarly to the _flush method that transform streams have but is called before the finish event is emitted and if asynchronous delays the stream from finishing. The `final` option may also be passed in order to set it. PR-URL: https://github.com/nodejs/node/pull/12828 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
25 lines
529 B
JavaScript
25 lines
529 B
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
|
|
const stream = require('stream');
|
|
let shutdown = false;
|
|
|
|
const w = new stream.Writable({
|
|
final: common.mustCall(function(cb) {
|
|
assert.strictEqual(this, w);
|
|
setTimeout(function() {
|
|
shutdown = true;
|
|
cb();
|
|
}, 100);
|
|
}),
|
|
write: function(chunk, e, cb) {
|
|
process.nextTick(cb);
|
|
}
|
|
});
|
|
w.on('finish', common.mustCall(function() {
|
|
assert(shutdown);
|
|
}));
|
|
w.write(Buffer.allocUnsafe(1));
|
|
w.end(Buffer.allocUnsafe(0));
|