如果上次回调尚未完成,则忽略传入的流更新 [英] Ignore incoming stream updates if last callback hasn't finished yet

查看:25
本文介绍了如果上次回调尚未完成,则忽略传入的流更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的代码.

IPruduceDemUpdates.Subscribe(update => DoUpdate(update));

但我想做的就是这样.

IPruduceDemUpdates.Subscribe(update => if(NoDoUpadteIsRunning) DoUpdate(update));

所以它会忽略传入的更新,如果更新方法已经在运行.此外,它应该始终执行最后一次更新.无论是流的最后一次更新还是一段时间内的最后一次更新.这是一个示例时间线

So it ignores incoming updates, if the update method is already running. In addition, it should always execute the last update. No matter if it is the last update of the stream or the last for a period of time. Here an example timeline

  • 更新 1 开始
  • 更新 2 被忽略
  • 更新 3 被忽略
  • 更新 4 被忽略
  • 更新 1 已完成
  • 更新 4 开始
  • 更新 4 完成

我有跳过的解决方案

        IPruduceDemUpdates.Subscribe(update =>
        {
            if (_task == null || _task.IsCompleted || _task.IsCanceled || _task.IsFaulted)
                _task = DoUpdate(update);
        });

但我不知道如何确定上次更新是否会处理.

But I don't know how to be sure, that the last update will process.

推荐答案

如果 DoUpdate 是同步的(在本例中似乎是这样),您可以使用 BufferIntrospective来自

If DoUpdate is synchronous (which it appears to be in this case), you can use BufferIntrospective from Rxx. It does exactly what you want:

IProduceDemUpdates
    .BufferIntrospective()
    .Where(items => items.Count > 0) // ignore empty buffers
    .Select(items => items[items.Count - 1]) // ignore all but last item in buffer
    .Subscribe(DoUpdate);

这篇关于如果上次回调尚未完成,则忽略传入的流更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆