首页 > 解决方案 > 我们如何创建一个 IObservableList来自 IObservable>?

问题描述

问题

我们如何从一个IObservable<IReadOnlyList<T>>到一个IObservableList<T>(从DynamicData)?

语境

我在我的项目中同时使用Reactive 扩展DynamicData

我目前有一个随时间变化的字符串列表。我有一个IObservable<IReadOnlyList<string>>. 它产生如下更新:

  1. ["A", "C", "F"]
  2. ["A", "B", "C", "F"]
  3. ["A", "B", "C"]

我想将其转换为IObservableList<string>会产生如下更新的:

  1. 在索引 0 处添加“A”、“C”、“F”
  2. 在索引 1 处添加“B”
  3. 从索引 3 中删除“F”

我试过的

我试过使用ToObservableChangeSet扩展,但它没有适合我的情况的正确行为。

代码
Subject<IReadOnlyList<string>> source = new Subject<IReadOnlyList<string>>();
IObservable<IEnumerable<string>> observableOfList = source;
IObservable<IChangeSet<string>> observableOfChangeSet = source.ToObservableChangeSet<string>();

observableOfChangeSet
    .Bind(out ReadOnlyObservableCollection<string> boundCollection)
    .Subscribe();

((INotifyCollectionChanged)boundCollection).CollectionChanged += OnCollectionChanged;

source.OnNext(new[] { "A", "C", "F" });
source.OnNext(new[] { "A", "B", "C" });

void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    Debug.WriteLine($"[{string.Join(", ", (IEnumerable<string>)sender)}] (operation: {e.Action} at index {e.NewStartingIndex})");
}
输出
[A, C, F] (operation: Reset at index -1)
[A, C, F, A] (operation: Add at index 3)
[A, C, F, A, B] (operation: Add at index 4)
[A, C, F, A, B, C] (operation: Add at index 5)

正如我们所看到[A, C, F, A, B, C]的不一样[A, B, C]


我也尝试过使用EditDiff,但这并不能保留列表的顺序。

代码
Subject<IReadOnlyList<string>> source = new Subject<IReadOnlyList<string>>();
IObservable<IEnumerable<string>> observableOfList = source;
IObservable<IChangeSet<string>> observableOfChangeSet = ObservableChangeSet.Create<string>(list =>
{
    return observableOfList
        .Subscribe(items => list.EditDiff(items, EqualityComparer<string>.Default));
});

observableOfChangeSet
    .Bind(out ReadOnlyObservableCollection<string> boundCollection)
    .Subscribe();

((INotifyCollectionChanged)boundCollection).CollectionChanged += OnCollectionChanged;

source.OnNext(new[] { "A", "C", "F" });
source.OnNext(new[] { "A", "B", "C" });

void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    Debug.WriteLine($"[{string.Join(", ", (IEnumerable<string>)sender)}] (operation: {e.Action} at index {e.NewStartingIndex})");
}
输出
[A, C, F] (operation: Reset at index -1)
[A, C] (operation: Remove at index -1)
[A, C, B] (operation: Add at index 2)

正如我们所看到[A, C, B]的不一样[A, B, C]

谢谢!

标签: c#.netsystem.reactivereactivereactiveui

解决方案


我对 DynamicData 不太熟悉,但我相信这会给出接近预期结果的结果:

using DynamicData;
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Reactive.Subjects;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Subject<IReadOnlyList<string>> source = new Subject<IReadOnlyList<string>>();

            var list = ObservableChangeSet.Create<string>(sourceList =>
            {
                return source
                    .Subscribe(newItems =>
                    {
                        sourceList.Edit(items =>
                        {
                            for (var i = 0; i < newItems.Count; i++)
                            {
                                if (items.Count <= i) items.Add(newItems[i]);
                                else
                                {
                                    if (items[i] != newItems[i])
                                    {
                                        items[i] = newItems[i];
                                    }
                                }
                            }
                            while (items.Count > items.Count) items.RemoveAt(items.Count - 1);
                        });
                    });
            }).AsObservableList();

            source.OnNext(new[] { "A", "C", "F" });
            source.OnNext(new[] { "A", "B", "C", "F" });
            source.OnNext(new[] { "A", "B", "C" });

            Console.ReadLine();
        }
    }
}

不幸的是,我没有弄清楚如何获取Add "B" at index 1,但当前输出如下:

  1. AddRange. 3 changes
  2. 3 Changes
    • Replace. Current: B, Previous: C
    • Replace. Current: C, Previous: F
    • Add. Current: F, Previous: <None>
  3. Remove. Current: C, Previous: <None>

编辑:以下给出了您想要的确切输出,但我没有测试每个的性能(对于较大的列表,插入可能会变得昂贵):

using DynamicData;
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Reactive.Subjects;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Subject<IReadOnlyList<string>> source = new Subject<IReadOnlyList<string>>();

            var list = ObservableChangeSet.Create<string>(sourceList =>
            {
                return source
                    .Subscribe(newItems =>
                    {
                        sourceList.Edit(items =>
                        {
                            for (var i = 0; i < newItems.Count; i++)
                            {
                                if (items.Count <= i) items.Add(newItems[i]);
                                else
                                {
                                    if (items[i] != newItems[i])
                                    {
                                        items.Insert(i, newItems[i]);
                                    }
                                }
                            }
                            while (items.Count > newItems.Count) items.RemoveAt(items.Count - 1);
                        });
                    });
            }).AsObservableList();

            source.OnNext(new[] { "A", "C", "F" });
            source.OnNext(new[] { "A", "B", "C", "F" });
            source.OnNext(new[] { "A", "B", "C" });

            Console.ReadLine();
        }
    }
}

  1. AddRange. 3 changes
  2. Add. Current: B, Previous: <None> Index 1
  3. Remove. Current: C, Previous: <None> Index 2

推荐阅读