我希望只有传回给_flush的回拨 完成后,才会触发'完成'回调,但在_flush完成前触发。为什么?为什么在_flush完成之前,“完成”事件在Node.js转换流中触发?
var stream = require('stream').Transform();
// Nothing interesting here
stream._transform = function (chunk, enc, next) {
next();
};
// _flush makes an async call.
stream._flush = function (callback) {
console.log("Expecting 1st: Entering _flush ");
return process.nextTick(function() {
console.log("Expecting 2nd: Done with _flush");
callback();
});
};
stream.on('finish',function() {
console.log("Expecting 3rd: Entering finish callback-- should be flushed now.");
});
stream.end("chunk");
的node.js stream documentation用于精加工说,当所有数据已刷新到底层系统事件时才会触发。
看着它看起来像意图source code for Node's Transform stream implementation是完成将触发只有当_flush
回调被称为:
this.once('prefinish', function() {
if (util.isFunction(this._flush))
this._flush(function(er) {
done(stream, er);
});
else
done(stream);
});
无论我错过了一些东西或者这是与节点的变换流的错误。我认为这是前者,但没有发现我的问题。谢谢你的帮助!
更新:需要注意的是,如果我替换调用process.nextTick()
一起sleep
同步调用,为sleep
模块提供,那么问题就会消失。该问题仅在_flush
内由异步调用触发。
谢谢!我将对0.11进行测试,然后考虑打开一个问题,因为目前的行为似乎与文档不匹配。 –
我向Node提交了拉取请求/错误报告。我们将看看会发生什么:https://github.com/joyent/node/pull/7612 –