我正在尝试将字符串流式传输到另一个流:
streamer = new stream.Transform objectMode: true
stringer = (string) ->
streamer._transform = (chunk, encoding, done) ->
@push string.split('').shift()
done()
return streamer
streamer.on 'readable', ->
console.log 'readable'
stringer('hello').pipe process.stdout
但是没有任何内容登录控制台。我究竟做错了什么?
正如您自己所说,您需要的是可读流而不是转换流。另外,您还有一个错误,因为string.split('')
总是返回相同的数组,然后.shift()
总是返回相同的字母。您的代码一旦重写如下:
'use strict'
Readable = require('stream').Readable
stringer = (string) ->
array = string.split('')
new Readable
read: (size) ->
@push array.shift()
return
readable = stringer('hello')
readable.on 'readable', ->
console.log 'readable'
return
readable.pipe process.stdout
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句