1
我創建了自己的readstream。但是我想知道_read()何時被調用?如果我不添加listerner,_read()將不會被調用。爲什麼?_read()的Node Readable Stream何時調用
var data = [{"id":0,"name":"object 0","value":3}],
Readable = require('stream').Readable,
util = require('util');
var ReadStream = function() {
Readable.call(this, {objectMode: true});
this.data = data;
this.curIndex = 0;
};
util.inherits(ReadStream, Readable);
ReadStream.prototype._read = function() {
if (this.curIndex === this.data.length)
return this.push(null);
var data = this.data[this.curIndex++];
//console.log('read: ' + JSON.stringify(data));
this.push(data);
};
var stream = new ReadStream();
stream.on('data', function(record) {
console.log('received: ' + JSON.stringify(record));
});
stream.on('end', function() {
console.log('done111');
});
添加一個「數據」的事件處理程序將切換到流動模式?源代碼在哪裏?我也發現它與process.nextTick有關? – jason
https://github.com/nodejs/node/blob/be68b68d4863f0d389cc46fdf6f1cbcd1b241d0a/lib/_stream_readable.js#L677 –