[英]Circular data flow in highlandjs
我只是在受到NoFlo.js的启发后学习了highland.js。 我希望能够以递归方式运行流。 在这个设计的例子中,我将提供一个乘以2的数字,我们过滤结果<= 512.一旦数字相乘,它就会被反馈到系统中。 我的代码有效,但如果我在管道中取出doto函数,它不会处理任何数字。 我怀疑我正在将数据错误地发送回returnPipe。 有没有更好的方法将数据传回系统? 我错过了什么?
###
input>--m--->multiplyBy2>---+
| |
| |
+---<returnPipe<----+
###
H = require('highland')
input = H([1])
returnPipe = H.pipeline(
H.doto((v)->console.log(v))
)
H.merge([input,returnPipe])
.map((v)-> return v * 2)
.filter((v)-> return v <= 512)
.pipe(returnPipe)
从文档中: doto
在重新发出 doto
旋转流 。 这意味着就管道而言,有一个函数仍在通过它传递流。 如果您取出doto
,原始流不会在下一次迭代时通过返回流返回。
如果要使用管道,则必须向其传递一个获取流并发出流的方法。 例如,您可以在对doto
使用H.map((v)=>{console.log(v); return v;})
H.pipeline
方法,因为该方法会消耗流并发出一个流 ,当流在.pipe(returnPipe)
回它时它将继续流动
编辑:要回答你的问题,当你声明let input = H([1])
你实际上是在那里创建一个流。 您可以删除对管道和returnPipe的任何引用,并使用以下代码生成相同的输出:
let input = H([1]);
input.map((v)=> {
return v * 2;
})
.filter((v)=> {
if (v <= 512) {
console.log(v);
}
return v <= 512;
})
.pipe(input);
我最初的意图是在highland.js中编写一个递归文件阅读器。 我发布到了highland.js github问题列表,Victor Vu帮我把这个放在一起,写了一篇精彩的文章。
H = require('highland')
fs = require('fs')
fsPath = require('path')
###
directory >---m----------> dirFilesStream >-------------f----> out
| |
| |
+-------------< returnPipe <--------------+
legend: (m)erge (f)ork
+ directory has the initial file
+ dirListStream does a directory listing
+ out prints out the full path of the file
+ directoryFilter runs stat and filters on directories
+ returnPipe the only way i can
###
directory = H(['someDirectory'])
mergePoint = H()
dirFilesStream = mergePoint.merge().flatMap((parentPath) ->
H.wrapCallback(fs.readdir)(parentPath).sequence().map (path) ->
fsPath.join parentPath, path
)
out = dirFilesStream
# Create the return pipe without using pipe!
returnPipe = dirFilesStream.observe().flatFilter((path) ->
H.wrapCallback(fs.stat)(path).map (v) ->
v.isDirectory()
)
# Connect up the merge point now that we have all of our streams.
mergePoint.write directory
mergePoint.write returnPipe
mergePoint.end()
# Release backpressure.
out.each H.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.