从 IObservable 列表创建一个 IObservableList [英] Create a IObservableList from a list of IObservable

查看:34
本文介绍了从 IObservable 列表创建一个 IObservableList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个实现 IObservable> and IList>.

I'm looking for a reactive object that implement IObservable<IReadOnlyList<T>> and IList<IObservable<T>>.

就是这样,我希望能够写:

That's it, I would like to be able to write :

var list = new MyReactiveList<int>();
var item = new new Subject<int>();
list.Subscribe(values => Console.WriteLine($"[{string.Join(", ", values)}]"));
list.Add(item);
item.OnNext(1); // Will print out [1]

推荐答案

首先,您在问题中发布的代码无法编译.我已尽我所能修复它:

First up, your code that you posted in your question doesn't compile. I have fixed it the best I can:

var list = new MyReactiveList<int>();
var item = new Subject<int>();
list.Subscribe(values => Console.WriteLine($"[{string.Join(", ", values)}]"));
list.Add(item);
item.OnNext(1); // Will print out [1]

现在的练习只是实现 class MyReactiveList;:IObservable、IList.这相当简单,但唯一的问题是以某种方式将可变的 List> 转换为 IObservable> 使得 observable列表更改时自行更新.

Now the exercise is just to implement class MyReactiveList<T> : IObservable<IReadOnlyList<T>>, IList<IObservable<T>>. That's fairly simply, but the only problem is to somehow turn a mutable List<IObservable<T>> into an IObservable<IReadOnlyList<T>> such that the observable updates itself when the list changes.

这是:

public class MyReactiveList<T> : IObservable<IReadOnlyList<T>>, IList<IObservable<T>>
{
    private List<IObservable<T>> _list = new List<IObservable<T>>();

    private Subject<Unit> _update = new Subject<Unit>();

    public IDisposable Subscribe(IObserver<IReadOnlyList<T>> observer) =>
        _update
            .Select(_ => _list.CombineLatest().Select(x => new ReadOnlyList<T>(x)))
            .Switch()
            .Subscribe(observer);

    public IObservable<T> this[int index]
    {
        get => _list[index];
        set
        {
            _list[index] = value;
            _update.OnNext(Unit.Default);
        }
    }

    public int Count => _list.Count;

    public bool IsReadOnly => false;

    public void Add(IObservable<T> item)
    {
        _list.Add(item);
        _update.OnNext(Unit.Default);
    }

    public void Clear()
    {
        _list.Clear();
        _update.OnNext(Unit.Default);
    }

    public bool Contains(IObservable<T> item) => _list.Contains(item);

    public void CopyTo(IObservable<T>[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public IEnumerator<IObservable<T>> GetEnumerator() => _list.GetEnumerator();

    public int IndexOf(IObservable<T> item) => _list.IndexOf(item);

    public void Insert(int index, IObservable<T> item)
    {
        _list.Insert(index, item);
        _update.OnNext(Unit.Default);
    }

    public bool Remove(IObservable<T> item)
    {
        var removed = _list.Remove(item);
        _update.OnNext(Unit.Default);
        return removed;
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
        _update.OnNext(Unit.Default);
    }

    IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator();
}

public class ReadOnlyList<T> : IReadOnlyList<T>
{
    public ReadOnlyList(IEnumerable<T> items) { _list.AddRange(items); }

    private List<T> _list = new List<T>();

    public T this[int index] => _list[index];

    public int Count => _list.Count;

    public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator();
}

注意:实现自己的 observable 并不是一个好主意 - 很容易弄错它们并创建不能很好地处理并发的代码.

NB: It's not a great idea to implement your own observables - it's easy to get them wrong and create code that doesn't play well with the concurrency.

这篇关于从 IObservable 列表创建一个 IObservableList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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