2013-04-02 1 views
3

node2.js의 새로운 streams2 API에 대해 다소 혼란 스럽습니다. 쓰기 가능한 스트림을 만들려고하지만 "_end"함수를 정의하는 방법을 찾을 수 없습니다. 무시할 수있는 "_write"기능 만 있습니다. 또한 문서 작성 방법을 알려주는 문서가 없습니다.streams2 쓰기 가능 - "_end"함수를 정의 할 수있는 방법이 있습니까?

누군가가 mystream.end()를 호출 한 후 스트림을 제대로 닫는 함수를 정의하는 방법을 찾고 있습니다.

내 스트림은 다른 스트림에 쓰고 스트림을 닫은 후에 모든 데이터를 보낸 후에 기본 스트림을 닫고 싶습니다.

어떻게하면됩니까?

처럼 보일 수있는 방법 : 당신은 당신의 스트림에 finish 이벤트를 수신하고 호출 할 수

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); 
} 

답변

5

_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); 
}