简体   繁体   English

当 OnNext 正在运行时,我想删除所有传入的通知,除了最新的

[英]While the OnNext is running, I want to drop all incoming notifications except from the latest

I have some observable, that produces items over time.我有一些可观察的,随着时间的推移会产生项目。 Intervals between items is almost random.项目之间的间隔几乎是随机的。 I subscribe to my observable and process item, each process can take a lot of time and when OnNext ends its work, many new items are produced.我订阅了我的可观察和流程项目,每个流程都可能花费很多时间,当OnNext结束其工作时,会产生许多新项目。 I need to take only last item and process it.我只需要拿走最后一项并进行处理。 So every time long OnNext operation completes I need do OnNext for latest item from items that was produced during previous OnNext run.因此,每次长OnNext操作完成时,我都需要对上一次OnNext运行期间生成的项目中的最新项目执行OnNext Can I do it with Rx?我可以用 Rx 做吗?

I tried Window(1).Switch() but seems like it executes immediately when item comes, not when OnNext completed.我尝试了Window(1).Switch()但似乎它在项目到来时立即执行,而不是在OnNext完成时执行。

Here is relatively simple custom DroppingDo operator, that probably does what you want.这是相对简单的自定义DroppingDo运算符,它可能可以满足您的需求。 It is somewhat similar with the built-in Do operator, with the difference that it invokes the action on the ThreadPool instead of the current thread, and that it ignores items that are received while a previous action is running.它与内置的Do运算符有些相似,不同之处在于它调用ThreadPool而不是当前线程上的操作,并且它忽略在前一个操作运行时接收到的项目。 The latest item is preserved though.虽然保留了最新的项目。

/// <summary>
/// Invokes an action sequentially for each element in the observable sequence,
/// on the specified scheduler, skipping and dropping elements that are received
/// during the execution of a previous action, except from the latest element.
/// </summary>
public static IObservable<TSource> DroppingDo<TSource>(
    this IObservable<TSource> source,
    Action<TSource> action,
    IScheduler scheduler = null)
{
    // Arguments validation omitted
    scheduler ??= Scheduler.Default;
    return Observable.Defer(() =>
    {
        Tuple<TSource> latest = null;
        return source
            .Select(item =>
            {
                var previous = Interlocked.Exchange(ref latest, Tuple.Create(item));
                if (previous != null) return Observable.Empty<TSource>();
                return Observable.Defer(() =>
                {
                    var current = Interlocked.Exchange(ref latest, null);
                    Debug.Assert(current != null);
                    var unboxed = current.Item1;
                    return Observable.Start(
                        () => { action(unboxed); return unboxed; }, scheduler);
                });
            })
            .Concat();
    });
}

Usage example.使用示例。 Just replace your code that probably looks like this:只需替换可能如下所示的代码:

someObservable
    .Subscribe(x => Process(x), ex => HandleError(ex));

With this:有了这个:

someObservable
    .DroppingDo(x => Process(x))
    .Subscribe(_ => { }, ex => HandleError(ex));

暂无
暂无

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

相关问题 使用Rx,当我的Subscribe方法运行时,如何忽略除最新值以外的所有值 - With Rx, how do I ignore all-except-the-latest value when my Subscribe method is running 如何忽略可观察序列中除错误以外的所有通知? - How to ignore all notifications from an observable sequence except errors? 如何从下拉列表的ItemList中删除具有特定值的所有ListItem? - How can I remove all ListItems except the one having a particular value from an ItemList of a drop-down? 我想使用iTextSharp从PDF获取除文本对象以外的所有对象作为图像 - I want to get all objects except text object as an image from PDF using iTextSharp 我想通过将输入数组的所有项目相乘来计算,但除了第 i 个项目 - I want to calculated by multiplying all of the items of the input array, but except for example i-th item 我想在程序运行 c# 时将成员添加到 class - I want to add a Member to a class while program is running c# 我想禁止除正常字母(a-z)以外的所有特殊字符,但不起作用 - i want to disallow all of the special characters except for normal letters( a- z) but not working 我想从文本文件的顶部删除行(末尾的 7000 行除外) - i want to delete lines from the top of the text file (Except 7000 lines from the End) 使用PushSharp排队通知时,什么也没有发生 - While queueing notifications with PushSharp nothing happens at all 想要删除单词中除第一个字符外的所有字符 - Want to Remove all the Characters Except First character in the word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM