[英]Highland add to stream before being completely consumed?
Exist a way to add to an stream of highland an ended message with a full description of our needs? 是否存在一种方法,可以在高地流中添加一条完整的信息,以完整地描述我们的需求?
Less suppose we have the following situation: 少假设我们有以下情况:
const stream = high([1,4,6,7])
Then with this stream, I want to count each one of the values being processed and say 然后,使用此流,我要计算正在处理的每个值,然后说
sink.drain(stream.pipe(4))
Being 4 the number of elements of the array. 为4的数组元素数。 Consider that could be thousends of objects in an stream and I need to consume from the stream in order to be able to count.
考虑到这可能是流中对象的数量之多,为了能够计数,我需要从流中进行消耗。
I cannot say array.length because it is a source that could come with any information, and that information is being processed with the stream... How can I add to the stream a Message End with the description of what was consumed? 我不能说array.length,因为它是可能包含任何信息的源,并且该信息正在与流一起处理...如何向流中添加消息末尾以及所消耗内容的描述?
It sounds like you want to set up some state around your values, but without hindering the consumption of your values. 听起来您想在值周围建立一些状态,但又不妨碍值的使用。 I would suggest looking at some solutions that involve
h.through
. 我建议查看一些涉及
h.through
解决方案。
You could split a stream with fork
or observe
and reduce some state from the values: 您可以使用
fork
拆分流,或者observe
并从中减少某些状态:
h([1, 2, 3, 4])
.through(stream => {
const length = stream.observe()
.reduce(0, m => m + 1)
.map(length => ({ length }))
return h([stream, length])
.sequence()
})
.errors(err => console.error(err))
.each(x => console.log(x))
You could create some state and return a new stream based on the events of the source stream: 您可以创建一些状态并根据源流的事件返回一个新的流:
const h = require('highland')
h([1, 2, 3, 4])
.through(stream => {
let length = 0
return h(push => stream
.tap(() => ++length)
.errors(err => push(err))
.each(x => push(null, x))
.done(() => {
push(null, `length: ${length}`)
push(null, h.nil)
}))
})
.errors(err => console.error(err))
.each(x => console.log(x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.