我有一个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 }
...
...