简体   繁体   English

Highland.js和缓冲区(或队列)

[英]Highland.js and buffer(or queue)

So, I have a code bellow to model the issue: 所以,我有下面的代码来模拟这个问题:

const H = require('highland');
const Promise = require('bluebird');

let i = 0
const stream = H(function(push, next) {
  console.log('read', i)
  push(null, i)
  i++;
  Promise.delay(100).then(() => next())
})


stream
  .flatMap(function(x) {
    console.log('start writing', x)
    return H(Promise.delay(2000, 'y').tap(() => console.log('finish writing', x)))
  })
  .done()

which produces output like this: 产生这样的输出:

read 0
start writing 0
finish writing 0
read 1
start writing 1

the problem: I want to have a buffer of some size where I will stack the data from producer. 问题:我希望有一个一定大小的缓冲区,在该缓冲区中可以堆叠生产者的数据。 So, with the buffer of size 1, output should look like: 因此,使用大小为1的缓冲区,输出应如下所示:

read 0
start writing 0
read 1
finish writing 0
start writing 1
read 2

so, I want to buffer value from producer if the "producer" is "busy". 因此,如果“生产者”很忙,我想缓冲生产者的价值。 Is it possible to do such thing with highland? 可以在高地做这样的事情吗?

No. It's not possible (not by promises, however yes - with callbacks) and they did state that in this longish issue thread 不。这是不可能的(不是通过诺言,而是通过回调实现),而且他们确实在这个冗长的问题线程中声明了这一点

One of the reasons why I decided to write scramjet . 我决定写scramjet的原因之一。

A simple case like yours: 像您这样的简单案例:

let i = 0;
const stream = new (require("scramjet").DataStream)({read() { this.push(i++); })

stream.map(
    (x) => Promise.delay(2000, 'y').tap(() => console.log('finish writing', x))
).accumulate(
    () => 0
).then(
    () => console.log("done")
)

It just works as you want it. 它只是按您想要的方式工作。 :) :)

The answer is in my arcticle 答案在我的北极

The idea here – buffering is a part of stream api. 这里的想法–缓冲是流api的一部分。 Highland just manipulate streams 高地只是操纵溪流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM