从 IObservable 列表创建一个 IObservableList

编程入门 行业动态 更新时间:2024-10-26 06:26:16
本文介绍了从 IObservable 列表创建一个 IObservableList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在寻找一个实现 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:22:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   列表   IObservable   IObservableList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!