我如何实现类似" bufferWhile"在RxJs?

时间:2017-05-01 07:49:24

标签: javascript rxjs reactive-programming rxjs5

我使用RxJs处理从文件重放的数据。每个数据项都包含timereceived属性。在重放时,我想创建缓冲区,其中包含最初在给定时间跨度x内收到的所有数据项。换句话说:我想将所有项目添加到当前缓冲区,而收到的第一个缓冲区元素和收到的当前元素之间的时间跨度小于时间跨度x。

示例测试:

it('should be able to create buffers based on time received', function() {
    // given
    let source = require('rx').Observable.from([
        {val: 1, timereceived: 10},
        {val: 2, timereceived: 20},
        {val: 3, timereceived: 100},
        {val: 4, timereceived: 110},
        {val: 5, timereceived: 120}
    ]);

    // when
    let actual = app.bufferWithTimeReceived(source, 105).toArray();

    // then
    console.log(actual);
    assert.equal(actual.length, 2); // first contains val 1-3, second val 4-5
})

如果我不重播文件中的所有数据但只是实时接收,我可以使用bufferWithTime,并且没问题。

使用其他示例更新

    // given
    let source = require('rxjs/Rx').Observable.from([
        {val: 1, timereceived: 10},
        {val: 2, timereceived: 20},
        {val: 3, timereceived: 100},
        {val: 4, timereceived: 110},
        {val: 5, timereceived: 120},
        {val: 6, timereceived: 9920},
        {val: 7, timereceived: 9930}
    ]);

    // when
    app.bufferWithTimeReceived(source, 30).subscribe(console.log);


    // then
    // expected output would be [val 1-2][val 3-5][val 6-7] (empty arrays in between would be ok)

更新结束

现在我玩弄了不同的方法。我的最后一个是:

exports.bufferWithTimeReceived = (source, timespan) => {
    return Rx.Observable.defer(() => Rx.Observable.create(function (observer) {
        let currBuffer = [];
        source.subscribe(x => {
            if (currBuffer.length == 0)
                currBuffer = [x];
            else {
                if (x.timereceived-currBuffer[0].timereceived < timespan)
                    currBuffer.push(x);
                else {
                    observer.onNext(currBuffer);
                    currBuffer = [x];
                }
            }
        },
        (err)=>observer.onError(err),
        ()=>observer.onCompleted());
    }));
};

不幸的是,这只会导致oArrayObservable { source: Defer { _f: [Function] } }作为错误消息,这不是很有用。我也想知道Rx - Divide stream into segments (lists) by condition可能对我有什么帮助?!

奖金问题:任何提示如何使此缓冲区重叠?

2 个答案:

答案 0 :(得分:1)

我这样做但请注意这是RxJS 5.但是,mergeAll也应该在RxJS 4中可用(可能是不同的名字)。

const THRESHOLD = 105;

const data = [
  {val: 1, timereceived: 10},
  {val: 2, timereceived: 20},
  {val: 3, timereceived: 100},
  {val: 4, timereceived: 110},
  {val: 5, timereceived: 120}
];
const source = Observable.from(data)
  .groupBy(item => parseInt(item.timereceived / THRESHOLD))
  .map(observable => observable.toArray())
  .mergeAll()
  .subscribe(console.log);

groupBy运算符为parseInt(item.timereceived / THRESHOLD)返回的每个键创建一个新的Observable。然后我用toArray()链接它,因为我想在重新发送之前收集所有项目,并且mergeAll()订阅所有Observables(在这种情况下为2)并重新发送它们的项目,每个项目始终是一个数组来源可观察。

这会产生以下输出:

[ { val: 1, timereceived: 10 }, { val: 2, timereceived: 20 }, { val: 3, timereceived: 100 } ]
[ { val: 4, timereceived: 110 }, { val: 5, timereceived: 120 } ]

请参阅:

答案 1 :(得分:0)

感谢马丁的回答,我再次想到了groupBy,这似乎对我有用:

exports.bufferWithTimeReceived = (source, timespan) => {
        let currTime;
        return source.groupBy(x => {
            if (!currTime)
                currTime = x.timereceived;
            if (x.timereceived-currTime > timespan)
                currTime = x.timereceived;
            return currTime;            
        })
        .map(observable => observable.toArray())
        .mergeAll()
};

但我想知道bufferWhen能否提供更优雅的解决方案?!