我想将List的IObservables存储在容器中,并订阅这些可观察项的组合以检索合并的List。然后,我希望能够添加更多的Observable而不需要续订订阅,而仍然可以获得新结果。理想情况下,在向商店添加新的可观察物时也应触发。以下代码应详细说明:
using System;
using System.Collections.Generic;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Linq;
namespace dynamic_combine
{
class ObservableStuff
{
private List<IObservable<List<String>>> _listOfObservables = new List<IObservable<List<String>>>();
public ObservableStuff() { }
public void AddObservable(IObservable<List<String>> obs)
{
_listOfObservables.Add(obs);
}
public IObservable<IList<String>> GetCombinedObservable()
{
return Observable.CombineLatest(_listOfObservables)
.Select((all) =>
{
List<String> mergedList = new List<String>();
foreach(var list in all)
{
mergedList = mergedList.Concat(list).ToList();
}
return mergedList;
});
}
}
class Program
{
static void Main(string[] args)
{
ObservableStuff Stuff = new ObservableStuff();
BehaviorSubject<List<String>> A = new BehaviorSubject<List<String>>(new List<String>() { "a", "b", "c" });
BehaviorSubject<List<String>> B = new BehaviorSubject<List<String>>(new List<String>() { "d", "e", "f" });
BehaviorSubject<List<String>> C = new BehaviorSubject<List<String>>(new List<String>() { "x", "y", "z" });
Stuff.AddObservable(A.AsObservable());
Stuff.AddObservable(B.AsObservable());
Stuff.GetCombinedObservable().Subscribe((x) =>
{
Console.WriteLine(String.Join(",", x));
});
// Initial Output: a,b,c,d,e,f
A.OnNext(new List<String>() { "1", "2", "3", "4", "5" });
// Output: 1,2,3,4,5,d,e,f
B.OnNext(new List<String>() { "6", "7", "8", "9", "0" });
// Output: 1,2,3,4,5,6,7,8,9,0
Stuff.AddObservable(C.AsObservable());
// Wishful Output: 1,2,3,4,5,6,7,8,9,0,x,y,z
C.OnNext(new List<String>() { "y", "e", "a", "h" });
// Wishful Output: 1,2,3,4,5,6,7,8,9,0,y,e,a,h
Console.WriteLine("Press the any key...");
Console.ReadKey();
}
}
}
尽管示例在C#中,但最终需要在rxCpp中实现。同样,在Rx的其他变体中实现也将是一件令人不安的事情。
我已经建立了一个存储库来检查代码,并且可能会将其扩展到其他语言: https://gitlab.com/dwaldorf/rx-examples
BR, 丹尼尔
答案 0 :(得分:1)
首先,要进行一些更改,因为您的代码不那么容易阅读。 GetCombinedObservable
可以重写为:
public IObservable<IList<String>> GetCombinedObservable()
{
return Observable.CombineLatest(_listOfObservables)
.Select(l => l.SelectMany(s => s).ToList());
}
您的问题可以归结为两件事:您希望_listOfObservables
是动态的,这意味着将其从List<IObservable<T>>
更改为IObservable<IObservable<T>>
。但是,这样做的问题在于CombineLatest
不支持IObservable<IObservable<T>>
,因此我们必须创建一个。
这给我们带来了这个有趣的,丑陋的小功能(使用nuget包System.Collections.Immutable
):
public static class X
{
public static IObservable<List<T>> DynamicCombineLatest<T>(this IObservable<IObservable<T>> source)
{
return source
.SelectMany((o, i) => o.Select(item => (observableIndex: i, item: item)))
.Scan(ImmutableDictionary<int, T>.Empty, (state, t) => state.SetItem(t.observableIndex, t.item))
.Select(dict => dict.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).ToList());
}
}
现在我们可以更新您的课程:
class ObservableStuff
{
private ReplaySubject<IObservable<List<String>>> _subject = new ReplaySubject<IObservable<List<String>>>(int.MaxValue);
public ObservableStuff() { }
public void AddObservable(IObservable<List<String>> obs)
{
_subject.OnNext(obs);
}
public IObservable<IList<String>> GetCombinedObservable()
{
return _subject
.DynamicCombineLatest()
.Select(l => l.SelectMany(s => s).ToList());
}
}