【问题标题】:Rx.Net Window function extension to support count, time AND sizeRx.Net Window 函数扩展,支持计数、时间和大小
【发布时间】:2019-11-07 14:28:08
【问题描述】:

实际上,我有一个IObservable<byte[]>,并且希望不仅在数组计数和时间范围内,而且在通过当前窗口的数组的整体大小上对其进行窗口化。

请向我提供这些技巧

【问题讨论】:

    标签: c# observable reactive rx.net


    【解决方案1】:

    这是一个自定义 Window 运算符,它尝试复制内置 Window 重载的功能,该重载接受 timeSpan 和 count 参数,同时还支持按整体大小的窗口。

    public static IObservable<IObservable<TSource>> Window<TSource>(
        this IObservable<TSource> source, TimeSpan timeSpan, int count,
        int size, Func<TSource, int> sizeSelector, IScheduler scheduler = null)
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        if (timeSpan < TimeSpan.Zero && timeSpan != Timeout.InfiniteTimeSpan)
            throw new ArgumentOutOfRangeException(nameof(timeSpan));
        if (count < 1) throw new ArgumentOutOfRangeException(nameof(count));
        if (size < 0) throw new ArgumentOutOfRangeException(nameof(size));
        if (sizeSelector == null) throw new ArgumentNullException(nameof(sizeSelector));
        scheduler = scheduler ?? Scheduler.Default;
    
        return Observable.Create<IObservable<TSource>>(observer =>
        {
            Subject<TSource> currentSubject = null;
            IStopwatch stopwatch = null;
            int itemCounter = 0;
            int currentSize = 0;
    
            return source.Subscribe(item =>
            {
                if (currentSubject == null)
                {
                    currentSubject = new Subject<TSource>();
                    observer.OnNext(currentSubject);
                }
                if (stopwatch == null && timeSpan != Timeout.InfiniteTimeSpan)
                {
                    stopwatch = scheduler.StartStopwatch();
                }
    
                currentSubject.OnNext(item);
                itemCounter++;
                currentSize += sizeSelector(item);
    
                if (itemCounter == count
                    || (stopwatch != null && stopwatch.Elapsed >= timeSpan)
                    || currentSize >= size)
                {
                    currentSubject.OnCompleted();
                    currentSubject = null;
                    if (stopwatch != null) stopwatch = scheduler.StartStopwatch();
                    itemCounter = 0;
                    currentSize = 0;
                }
            }, ex =>
            {
                if (currentSubject != null) currentSubject.OnError(ex);
                observer.OnError(ex);
            }, () =>
            {
                if (currentSubject != null) currentSubject.OnCompleted();
                observer.OnCompleted();
            });
        });
    }
    

    使用示例:

    observable.Window(timeSpan: TimeSpan.FromMilliseconds(5000), count: 10,
        size: 100, sizeSelector: x => x.Length);
    

    每次当前窗口已满,或经过给定的时间量,或累积大小大于给定大小时,都会创建一个新窗口。每次发出窗口时都会重新启动计时器。

    【讨论】:

    • 添加了对用户定义的IScheduler 的支持,用于测量timeSpan 的持续时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多