简体   繁体   English

RX-开始缓冲onNext

[英]Rx - start buffering onNext

Using Observable.Buffer(TimeSpan timeSpan) Method , which splits stream into 10 minute chunks and returns as IList , that works fine 使用Observable.Buffer(TimeSpan timeSpan) Method ,该Observable.Buffer(TimeSpan timeSpan) Method将流分成10分钟的块并作为IList返回,效果很好

var stream = Observable.FromEventPattern<*>(*,*);
stream.Buffer(TimeSpan.FromSeconds(10));

Trying to implement more complex behavior 试图实现更复杂的行为

  • Start chunk (buffered events list) when new event is pushed into stream (instead of every 10 second) 新事件被推送到流中时 (而不是每10秒一次)启动块(缓冲事件列表
  • Continue buffering events until no event is pushed into stream for x seconds 继续缓冲事件,直到x秒内没有事件推送到流中为止

Try this: 尝试这个:

var query =
    stream.Publish(
        ps => ps.Window(
            () => ps.Delay(TimeSpan.FromSeconds(1.0)).Take(1)));

I am sure there are a quite a few ways you could do this. 我相信您可以通过多种方式来做到这一点。

I have a working tested example here 我这里有一个工作正常的示例

void Main()
{
    var scheduler = new TestScheduler();
    var stream = scheduler.CreateColdObservable(
        ReactiveTest.OnNext(1.Seconds(), 'A'),
        ReactiveTest.OnNext(2.Seconds(), 'B'),
        ReactiveTest.OnNext(13.Seconds(), 'C')
        );

    var observer = scheduler.CreateObserver<string>();

    var query = stream.Publish(s => {
        return s.Timeout(TimeSpan.FromSeconds(10), Observable.Empty<char>(), scheduler)
            .ToList()
            .Where(buffer=>buffer.Any())    
            //Project to string to make equality test easier for the example.       
            .Select(buffer=>string.Join(",", buffer))
            .Repeat();
    });

    query.Subscribe(observer);

    scheduler.AdvanceBy(100.Seconds());

    ReactiveAssert.AreElementsEqual(
        new []{
            ReactiveTest.OnNext(12.Seconds(), "A,B"),
            ReactiveTest.OnNext(23.Seconds(), "C")
        },
        observer.Messages);
}

// Define other methods and classes here
public static class TimeEx
{
    public static long Seconds(this int seconds)
    {
        return TimeSpan.FromSeconds(seconds).Ticks;
    }
}

Note here that I just make the Buffered list a string to make it easier to validate the equality. 请注意,在这里,我只是使Buffered列表成为字符串,以使其更容易验证相等性。 ie "A,B" instead of {'A', 'B'} 即用"A,B"代替{'A', 'B'}

Other options to consider are the Window or GroupJoin operators to do this - see http://www.introtorx.com/content/v1.0.10621.0/17_SequencesOfCoincidence.html . 要考虑的其他选项是WindowGroupJoin运算符-请参见http://www.introtorx.com/content/v1.0.10621.0/17_SequencesOfCoincidence.html And other operators I am sure can be stitched together like Switch , Select , Timer , Timeout etc to get your result. 我确信可以将其他运算符(如SwitchSelectTimerTimeout缝合在一起,以得到您的结果。

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

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