繁体   English   中英

RxJs zip运算符在xstream中是否等效?

[英]RxJs zip operator equivalent in xstream?

您好,我想弄清楚xstream中是否有与RxJs运算符zip等效的东西,或者至少是一种获得相同行为的方式。 如果有人需要澄清区别,下面的大理石图将显示。

 zip in rxjs
    |---1---2---3-----------5->
    |-a------b------c---d----->
            "zip"
    |-1a----2b------3c-----5d->


 whereas 'combineLatest' aka 'combine' in xstream does 

    |---1---2----------4---5->
    |----a---b---c---d------->
            "combine"
    |-1a----2a-2b-2c-2d-4d-5d>

感谢任何帮助,因为我是流编程的新手。 先感谢您!

对于xstream,我还需要一个zip运算符。 因此,我从现有的运营商创建了自己的公司。 压缩需要任意数量的流。

function zip(...streams) {
  // Wrap the events on each stream with a label
  // so that we can seperate them into buckets later.
  const streamsLabeled = streams
    .map((stream$, idx) => stream$.map(event => ({label: idx + 1, event: event})));
  return (event$) => {
    // Wrap the events on each stream with a label
    // so that we can seperate them into buckets later.
    const eventLabeled$ = event$.map(event => ({label: 0, event: event}));
    const labeledStreams = [eventLabeled$, ...streamsLabeled];

    // Create the buckets used to store stream events
    const buckets = labeledStreams.map((stream, idx) => idx)
      .reduce((buckets, label) => ({...buckets, [label]: []}), {});

    // Initial value for the fold operation
    const accumulator = {buckets, tuple: []};

    // Merge all the streams together and accumulate them
    return xs.merge(...labeledStreams).fold((acc, event) => {
      // Buffer the events into seperate buckets
      acc.buckets[event.label].push(event);

      // Does the first value of all the buckets have something in it?
      // If so, then there is a complete tuple.
      const tupleComplete = Object.keys(acc.buckets)
        .map(key => acc.buckets[key][0])
        .reduce((hadValue, value) => value !== undefined
          ? true && hadValue
          : false && hadValue,
        true);

      // Save completed tuple and remove it from the buckets
      if (tupleComplete) {
        acc.tuple = [...Object.keys(acc.buckets).map(key => acc.buckets[key][0].event)];
        Object.keys(acc.buckets).map(key => acc.buckets[key].shift());
      } else {
        // Clear tuple since all columns weren't filled
        acc.tuple = [];
      }

      return {...acc};
    }, accumulator)

    // Only emit when we have a complete tuple
    .filter(buffer => buffer.tuple.length !== 0)

    // Just return the complete tuple
    .map(buffer => buffer.tuple);
  };
}

可以与compose一起使用。

foo$.compose(zip(bar$)).map(([foo, bar]) => doSomething(foo, bar))

暂无
暂无

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

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