jabez128 / stream-handbook

stream-handbook的完整中文版本

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

请帮忙看看为什么这里的read(0)没有把缓存里的东西都清理出来?

sakopqiu opened this issue · comments

var Readable = require('stream').Readable;
var rs = Readable();

var c = 97;

rs._read = function () {
    if (c >= 'c'.charCodeAt(0)) return rs.push(null);
    console.log("Pushing " + String.fromCharCode(c) + 'p')
    rs.push(String.fromCharCode(c++) + 'p');
};

rs.on('readable', function () {
    var buf = rs.read(1);
    if (buf) {
        console.dir(buf.toString())
    }
    rs.read(0);
});

rs.on('end', () => {
    console.log("end");
})

打印结果是:
Pushing ap
Pushing bp
'a'
'p'
'b'
也就是说最后在缓存里的一个'p'没有打出来