Rx IObservable buffering to smooth out bursts of events

前端 未结 1 1039
盖世英雄少女心
盖世英雄少女心 2020-12-02 16:11

I have an Observable sequence that produces events in rapid bursts (ie: five events one right after another, then a long delay, then another quick burst of events, etc.). I

相关标签:
1条回答
  • 2020-12-02 16:46

    This is actually a duplicate of A way to push buffered events in even intervals, but I'll include a summary here (the original looks quite confusing because it looks at a few alternatives).

    public static IObservable<T> Buffered<T>(this IObservable<T> source, TimeSpan minDelay)
    {
        return source.Drain(x => 
            Observable.Empty<int>()
                .Delay(minDelay)
                .StartWith(x)
        );
    }
    

    My implementation of Drain works like SelectMany, except it waits for the previous output to finish first (you could think of it as ConactMany, whereas SelectMany is more like MergeMany). The built-in Drain does not work this way, so you'll need to include the implementation below:

    public static class ObservableDrainExtensions
    {
        public static IObservable<TOut> Drain<TSource, TOut>(
            this IObservable<TSource> source, 
            Func<TSource, IObservable<TOut>> selector)
        {
            return Observable.Defer(() =>
            {
                BehaviorSubject<Unit> queue = new BehaviorSubject<Unit>(new Unit());
    
                return source
                    .Zip(queue, (v, q) => v)
                    .SelectMany(v => selector(v)
                        .Do(_ => { }, () => queue.OnNext(new Unit()))
                    );
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题