将最后一项推入“可观察的"(序列) [英] Take the last item pushed to an Observable (Sequence)

查看:87
本文介绍了将最后一项推入“可观察的"(序列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中有一个IObservable<Item>,我想公开一个只读属性,该属性提供在给定时间推送到可观察对象的最后一项.因此它将提供单个值Item.

I have an IObservable<Item> inside a class and I want to expose a read-only property that provides the last item pushed to the observable at a given time. So it will provide a single value of Item.

如果未推送任何值,则必须返回默认值.

If no value has been pushed, then it will have to return a default value.

我如何做到这一点而不必订阅可观察的内容并拥有后备字段"?

How can I do this without having to subscribe to the observable and having a "backing field"?

推荐答案

仅在这里补充@Asti的答案,也许可以帮助您避免沮丧:

Just to supplement @Asti's answer a bit here, and perhaps help you with your frustration:

可观察的事物不是物理的事物",而是更具逻辑性的概念.通常将Rx与LINQ进行比较,并且在很多时候,这是一个公平的比较.但是,当您开始谈论数据结构时,它就会崩溃:出于学习目的,LINQ的可枚举与Lists足够相似.

An observable isn't a physical 'thing', it's more a logical concept. Rx is often compared to LINQ, and it's a fair comparison much of the time. It breaks down though when you start talking data structures: LINQ's enumerables are similar enough to Lists for learning purposes.

但是,在Rx端,根本没有与List相当的产品.一个可观察的是一个瞬态数据结构,所有运算符都处理这种瞬态状态.如果您要寻找永久状态,就离开Rx.

However, on the Rx side, there's simply no good equivalent to List. An observable is a transient data structure, all operators deal with this transient state. If you're looking for a permanent state, you're leaving Rx.

已经说过,将可观察到的状态转换为某种状态是一个常见的问题,并且有一些程序包可能会对您有所帮助:ReactiveUI可能是最著名的. ReactiveProperty是另一个.这两个软件包都有缺陷,但可能会对您有所帮助.

Having said that, converting an observable to some sort of state is a common problem, and there are some packages that may help you: ReactiveUI is perhaps the most known. ReactiveProperty is another. Both of these packages are flawed, but may help you.

如果您只是在寻找一种简单的方法来获得背景字段,而无需锅炉电镀背景字段,那么这将起作用:

If you're simply looking for an easier way to get a backing field, without boiler-plating out a backing field, this will work:

public static class ReactivePropertyExtensions
{
    public static ReactiveProperty<T> ToReactiveProperty<T>(this IObservable<T> source)
    {
        return new ReactiveProperty<T>(source);
    }

    public static ReactiveProperty<T> ToReactiveProperty<T>(this IObservable<T> source, T defaultValue)
    {
        return new ReactiveProperty<T>(source, defaultValue);
    }
}

public class ReactiveProperty<T> : IDisposable
{
    private IObservable<T> Source { get; }
    private IDisposable Subscription { get; }
    public T Value { get; private set; }

    public ReactiveProperty(IObservable<T> source)
        : this(source, default(T)) { }

    public ReactiveProperty(IObservable<T> source, T defaultValue)
    {
        Value = defaultValue;
        Source = source;
        Subscription = source.Subscribe(t => Value = t);
    }

    public void Dispose()
    {
        Subscription.Dispose();
    }
}

示例用法:

var ticker = Observable.Interval(TimeSpan.FromSeconds(1))
    .Publish().RefCount();

var latestTickerValue = ticker.ToReactiveProperty();
Console.WriteLine(latestTickerValue.Value);
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine(latestTickerValue.Value);
await Task.Delay(TimeSpan.FromSeconds(3));
Console.WriteLine(latestTickerValue.Value);

这篇关于将最后一项推入“可观察的"(序列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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