Node.js/stream/Readable:修订间差异
来自康健生活
| 第2行: | 第2行: | ||
=== 事件 === | === 事件 === | ||
==== Data ==== | ==== Data ==== | ||
=== 模式 === | |||
可读流有流动和暂停两种模式,可读流能够流动起来的驱动力在其resume方法,如: | |||
<syntaxhighlight lang="javascript"> | |||
//伪代码 | |||
function flow(stream){ | |||
const state=stream._readableState; | |||
state.flowing=true; | |||
while(state.flowing && stream.read() !== null); | |||
} | |||
Readable.prototype.resume=function(){ | |||
//... | |||
flow(this); | |||
//... | |||
} | |||
//伪代码 | |||
function fromList(n,state){ | |||
var ret; | |||
ret= /*从缓存池state.buffe中取数据*/ | |||
return ret; | |||
} | |||
Readable.prototype.read=function(n){ | |||
var state=this._readableState; | |||
//... | |||
this._read(state.highWaterMark); | |||
//... | |||
var ret=fromList(n,state); | |||
if(ret===null){ | |||
//... | |||
}else{ | |||
//标记消耗量 | |||
state.length-=n; | |||
} | |||
//触发事件,发送数据 | |||
if(ret !==null) this.emit('data',ret); | |||
return data; | |||
};</syntaxhighlight> | |||
=== Readable === | === Readable === | ||
什么时机会从文件读取数据到可读流内部缓存中? | 什么时机会从文件读取数据到可读流内部缓存中? | ||