Merging (or converting between) changesets from a SourceList and changesets from a SourceCache #1020
-
Hello, I am wondering how to merge changesets from SourceList and SourceCache. I know that if both collections are of the same type (e.g. both are SourceLists or both are SourceCaches) I can use various operators like My actual use case is also a little more complicated. I have a public interface IViewModel
{
Guid Id { get; }
}
public class MyViewModel : IViewModel
{
public Guid Id { get; } = Guid.NewGuid();
}
public class MyOtherViewModel : IViewModel
{
private SourceList<ChildViewModel> _childrenSource = new();
public Guid Id { get; } = Guid.NewGuid();
public IObservable<IChangeSet<ChildViewModel>> ConnectChildren() => _childrenSource.Connect();
}
public class ChildViewModel : IViewModel
{
public Guid Id { get; } = Guid.NewGuid();
} The way how I am trying to merge them is as follows: SourceCache<MyViewModel, Guid> _firstCache = new(x => x.Id);
SourceCache<MyViewModel, Guid> _secondCache = new(x => x.Id);
SourceCache<MyOtherViewModel, Guid> _thirdCache = new(x => x.Id);
var mergedChangeSets =
_firstCache.Connect().Transform(x => x as IViewModel)
.Or(_secondCache.Connect().Transform(x => x as IViewModel))
// this will not compile because it produces IObservable<IChangeSet<IViewModel>> instead of IObservable<IChangeSet<IViewModel, Guid>>
.Or(_thirdCache.Connect().MergeManyChangeSets(x => x.ConnectChildren().Transform(x => x as IViewModel))); There is no overload of Alternatively, I don't actually care if I might be able to get something that compiles if I use I also know that I could turn the SourceList in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Seems like you're looking to collect ALL of your view models, of all different types, into one central collection? Perhaps to dispose them all? Since you're using GUIDs, you're correctly interpreting that you can just merge all the streams together with native RX, as there's no real risk of key overlap. (At least, I THINK that's what the var mergedChangeSets =_firstCache.Connect()
.Transform(x => x as IViewModel)
.Or(_secondCache.Connect()
.Transform(x => x as IViewModel))
.Or(_thirdCache.Connect()
.MergeManyChangeSets(x => x.ConnectChildren()
.AddKey(x => x.Id)
.Transform(x => x as IViewModel))); |
Beta Was this translation helpful? Give feedback.
Seems like you're looking to collect ALL of your view models, of all different types, into one central collection? Perhaps to dispose them all?
Since you're using GUIDs, you're correctly interpreting that you can just merge all the streams together with native RX, as there's no real risk of key overlap. (At least, I THINK that's what the
.Or()
operator does, right? It's equivalent to a.Merge()
?) Anyway, there's an operator you can use to just convert a list stream into a keyed stream,.AddKey()
, since you HAVE a usable key.