2017-02-21 7 views
0

안녕하세요, 저는 xstream에 RxJs 연산자 zip에 해당하는 것이 있는지, 또는 적어도 동일한 동작을 얻는 방법이 있는지 알아 내려고하고 있습니다. 누구든지 차이점에 대한 설명이 필요하면 아래의 대리석 다이어그램이 표시됩니다.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> 

스트림을 사용한 프로그래밍에 익숙하지 않으셔서 감사드립니다. 미리 감사드립니다.

답변

1

또한 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); 
    }; 
} 

작성과 함께 사용할 수 있습니다.

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