Van*_*ing 3 javascript node.js streams2
我对node.js的新streams2 API有点困惑.我尝试创建一个Writable流,但我找不到定义"_end"函数的方法.我只能覆盖"_write"函数.文档中也没有任何内容可以告诉我如何做到这一点.
我正在寻找一种方法来定义一个函数来正确关闭流,之后有人调用mystream.end().
我的流写入另一个流,在关闭我的流之后,我还想在发送所有数据后关闭底层流.
我该怎么做?
它看起来如何:
var stream = require("stream");
function MyStream(basestream){
this.base = basestream;
}
MyStream.prototype = Object.create(stream.Writable);
MyStream.prototype._write = function(chunk,encoding,cb){
this.base.write(chunk,encoding,cb);
}
MyStream.prototype._end = function(cb){
this.base.end(cb);
}
Run Code Online (Sandbox Code Playgroud)
您可以finish在流上收听事件并拨打电话_end:
function MyStream(basestream) {
stream.Writable.call(this); // I don't think this is strictly necessary in this case, but better be safe :)
this.base = basestream;
this.on('finish', this._end.bind(this));
}
MyStream.prototype._end = function(cb){
this.base.end(cb);
}
Run Code Online (Sandbox Code Playgroud)