Arm*_* P. 5 javascript node.js typescript
在转换函数中编写 NodeJS 转换流时,我如何知道块是最后一个还是没有任何新块到来。
_transform(chunk: any, encoding: string, callback: Function): void {
// accumulating chunks here to buffer
// so that I need to do some processing on the whole buffer
// and I need to understand when to do that
}
Run Code Online (Sandbox Code Playgroud)
所以我需要知道进入 Stream 的块何时结束,对由所有块组成的缓冲区进行一些处理,然后从流中推送处理后的数据。
在_transform你不能弄清楚是否会有更多的数据或没有。
根据您的用例,您可以监听end事件,或者使用_flush:
流: transform._flush(callback):
自定义转换实现可以实现该
transform._flush()方法。当没有更多的写入数据要消耗时,但在'end'发出事件以指示可读流结束之前,将调用此方法。在
transform._flush()实现中,该readable.push()方法可能被调用零次或多次,视情况而定。刷新操作完成后必须调用回调函数。
| 归档时间: |
|
| 查看次数: |
2980 次 |
| 最近记录: |