我有一个问题A cache serving updates and new values as “DistinctLatest” and full cache contents upon subscription,社区很好地处理了这个问题。有人提出一个问题,即可以使用.DistinctLatest
运算符来定义缓存和替换上述问题中定义的值的实际目标。
OK!似乎没有多少关于这样的运营商的讨论。在搜索和思考时,我找到了ReactiveX: Group and Buffer only last item in each group,这有点接近。为了模仿原始问题,我尝试将缓存操作符编写为
/// <summary>
/// A cache that keeps distinct elements where the elements are replaced by the latest.
/// </summary>
/// <typeparam name="T">The type of the result</typeparam>
/// <typeparam name="TKey">The type of the selector key for distinct results.</typeparam>
/// <param name="newElements">The sequence of new elements.</param>
/// <param name="seedElements">The seed elements when the cache is started.</param>
/// <param name="replacementSelector">The replacement selector to choose distinct elements in the cache.</param>
/// <returns>The cache contents upon first call and changes thereafter.</returns>
public static IObservable<T> Cache<T, TKey>(this IObservable<T> newElements, IEnumerable<T> seedElements, Func<T, TKey> replacementSelector)
{
var s = newElements.StartWith(seedElements).GroupBy(replacementSelector).Select(groupObservable =>
{
var replaySubject = new ReplaySubject<T>(1);
groupObservable.Subscribe(value => replaySubject.OnNext(value));
return replaySubject;
});
return s.SelectMany(i => i);
}
但做一些似乎无法做到的测试。看起来如果在开始时订阅初始值并且观察到更新(和新值)。如果最后订阅,则只记录替换的种子值。
现在,我想知道一个普通的DistinctLast
运算符,我认为这个运算符,但它不起作用,然后这个#&#34;缓存&#34;添加是种子值和组的扁平化,但这不是测试所说的。我也尝试过分组和.TakeLast()
的一些事情,但没有骰子。
如果有人指点或思考这个问题,我很高兴,希望这会成为一些普遍有用的东西。
答案 0 :(得分:1)
@LeeCampbell为此完成了大部分工作。请参阅其他参考问题。无论如何,这里是代码:
public static class RxExtensions
{
public static IObservable<T> DistinctLatest<T, TKey>(this IObservable<T> newElements, IEnumerable<T> seedElements, Func<T, TKey> replacementSelector)
{
return seedElements.ToObservable()
.Concat(newElements)
.GroupBy(i => replacementSelector)
.SelectMany(grp => grp.Replay(1).Publish().RefCount());
}
}