With Rx, how do I ignore all-except-the-latest value when my Subscribe method is running

后端 未结 9 1573
暖寄归人
暖寄归人 2020-11-29 03:47

Using Reactive Extensions, I want to ignore messages coming from my event stream that occur while my Subscribe method is running. I.e. it sometimes takes me lon

9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 04:13

    With Rx 2.0 RC you can use Chunkify to get an IEnumerable of lists, each containing what was observed since the last MoveNext.

    You can then use ToObservable to convert that back to an IObservable and only pay attention to the last entry in each non-empty list.

    var messages = Observable.Interval(TimeSpan.FromMilliseconds(100));
    
    messages.Chunkify()
            .ToObservable(Scheduler.TaskPool)
            .Where(list => list.Any())
            .Select(list => list.Last())
            .Subscribe(n =>
            {
              Thread.Sleep(TimeSpan.FromMilliseconds(250));
              Console.WriteLine(n);
            });
    

提交回复
热议问题