【发布时间】:2017-03-09 11:18:56
【问题描述】:
我有一个IObservable<T>,其中 T 看起来像
public class Notification
{
public int Id { get; set; }
public int Version { get; set; }
}
通知以不同的时间间隔和不同的通知生成,其中版本号随着每个通知 ID 的更新而递增。
在特定时间段内限制可观察对象然后接收具有最新版本字段的不同通知的正确方法是什么?
到目前为止,我想出了这个用于限制和分组的方法,但无法弄清楚如何实际返回 IObservable<Notification>。
public static IObservable<int> ThrottledById(this IObservable<Notification> observable)
{
return observable
.GroupByUntil(n => n.Id, x => Observable.Timer(TimeSpan.FromSeconds(1)))
.Select(group => group.Key);
}
编辑: 样本输入/输出(油门延迟:3):
1. { id: 1, v: 1 }
2. { id: 1, v: 2 } { id: 2, v: 1 }
3. { id: 1, v: 3 }
-----------------------------------> notify { id:1, v: 3 }, notify { id:2, v: 1 }
4.
5. { id: 2, v: 2 }
6.
-----------------------------------> notify { id:2, v: 2 }
7. { id: 1, v: 4 }
8. { id: 1, v: 5 } { id: 2, v: 3 }
9. { id: 1, v: 6 }
-----------------------------------> notify { id:1, v: 6 }, notify { id: 2, v: 3 }
...
...
【问题讨论】:
-
您可以添加一些具有所需输出的示例输入吗?
标签: c# .net system.reactive reactive-programming