如何检索使用接收一个序列发生器的生命周期操作? [英] How to retrieve lifetime operations of a sequence generator using Rx?

查看:118
本文介绍了如何检索使用接收一个序列发生器的生命周期操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的方式来获取一个序列pviously产生的所有对象$ P $,沿着新的,当我收到通知就可以改变的,用接收。我想,这是罕见的,因为大多数的系统将只是希望未处理的条目,但我的情况需要一整套价值观,新与旧,上班的。

I need way to retrieve all the objects previously generated by a sequence, alongside the new ones, when I'm notified of changes on it, using Rx. I think this is uncommon, since most systems will just want the unprocessed entries, but my situation needs the whole set of values, old and new, to work.

是否有可能以某种方式实现这一目标?我找不到任何类似的例子,也没有方法似乎做到这一点。

Is it possible to achieve this somehow? I couldn't find any similar examples and no method seem to do that.

推荐答案

您可以使用的 扫描扩展方法这一点。

You can use the Scan extension method for this.

IObservable<T> source = ...;
IObservable<List<T>> history = source
    .Scan(new List<T>(), (list, item) => { list.Add(item); return list; });

如果在发出的标记 A B C
那么历史发出的标记 [A] [AB] [A,B,C] 分别。

If the source emits the tokens A, B, C,
then the history emits the tokens [A], [A B], [A, B, C] respectively.

如果您不希望从历史上所有排放值是相同名单,其中,T&GT; ,你可以稍微修改调用扫描

If you don't want all emitted values from history to be the same List<T>, you can slightly modify the call to Scan:

IObservable<List<T>> history = source
    .Scan(new List<T>(), (list, item) => list.Concat(new[]{ item }).ToList());

这篇关于如何检索使用接收一个序列发生器的生命周期操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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