我对node.js的新的streams2应用程序接口感到有点困惑。我尝试创建一个Writable流,但我找不到一种方法来定义"_end“函数。只有"_write"-function是我可以覆盖的。文档中也没有任何内容告诉我如何做。
我正在寻找一种方法来定义一个函数,在有人对它调用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);
}发布于 2013-04-02 23:37:15
您可以监听流上的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);
}https://stackoverflow.com/questions/15766475
复制相似问题