c# - 我们如何创建一个 IObservableList来自 IObservable>?
问题描述
问题
我们如何从一个IObservable<IReadOnlyList<T>>
到一个IObservableList<T>
(从DynamicData)?
语境
我在我的项目中同时使用Reactive 扩展和DynamicData。
我目前有一个随时间变化的字符串列表。我有一个IObservable<IReadOnlyList<string>>
. 它产生如下更新:
["A", "C", "F"]
["A", "B", "C", "F"]
["A", "B", "C"]
我想将其转换为IObservableList<string>
会产生如下更新的:
- 在索引 0 处添加“A”、“C”、“F”
- 在索引 1 处添加“B”
- 从索引 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]
。
谢谢!
解决方案
我对 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
,但当前输出如下:
AddRange. 3 changes
3 Changes
Replace. Current: B, Previous: C
Replace. Current: C, Previous: F
Add. Current: F, Previous: <None>
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();
}
}
}
AddRange. 3 changes
Add. Current: B, Previous: <None> Index 1
Remove. Current: C, Previous: <None> Index 2
推荐阅读
- reactjs - 随着道具的变化,打字稿中另一个道具的类型也会发生变化
- javascript - 如果 input[type="file"] 上传了文件,JS / React 中有没有办法执行代码
- docker - 自定义 Databricks 集群中缺少工作区目录
- flutter - 尝试添加初始化表达式,或初始化它的生成构造函数,或将其标记为“迟到”
- google-colaboratory - 如何在 Google Colab 中重新加载 github 存储库
- html - 删除 iFrame 上的垂直滚动
- flutter - 导航栏颤振应用程序不显示屏幕上的任何位置
- javascript - JavaScript 中的 /\\/ 对象是什么?
- python - 在 Win7 上安装 PyTorch 而不安装 Python 3.9
- wordpress - WP-API 的 JWT 身份验证:“rest_no_route”,“找不到与 URL 和请求方法匹配的路由”,