Last active
February 1, 2017 00:18
-
-
Save Protonz/14149b69d28ccced331f83fdc6fe8f69 to your computer and use it in GitHub Desktop.
Merging multiple ReactiveProperties into a single output signal when any one of them changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void MergePropertyTest() { | |
// Create an IEnumerable list of reactive proeprties | |
List<ReactiveProperty<int>> rxPropertyList = new List<ReactiveProperty<int>>(); | |
// Add a couple reactive properties | |
rxPropertyList.Add(new ReactiveProperty<int>(1)); | |
rxPropertyList.Add(new ReactiveProperty<int>(2)); | |
// Convert the IEnumerable list to an observable stream | |
IObservable<ReactiveProperty<int>> rxPropertyStream = rxPropertyList.ToObservable(); | |
// Merge all the ReactiveProperty streams together (but only observe changes) | |
IObservable<int> rxPropertyChangedStream = rxPropertyStream.SelectMany(rxProperty=> rxProperty.DistinctUntilChanged() ); | |
// Convert to a simple signal (Unit) | |
IObservable<Unit> anyChangedStream = rxPropertyChangedStream.Select(xs => Unit.Default); | |
var recordStreamOutput = new List<string>(); | |
anyChangedStream.Materialize().Subscribe(xs => recordStreamOutput.Add(xs.ToString())); | |
// Check (each reactive property sends its first value) | |
recordStreamOutput.IsCollection("OnNext(())", "OnNext(())"); | |
// Do | |
rxPropertyList[0].Value = 3; | |
// Check | |
recordStreamOutput.IsCollection("OnNext(())", "OnNext(())", "OnNext(())" ); | |
// Do | |
rxPropertyList[1].Value = 4; | |
rxPropertyList[1].Value = 5; | |
rxPropertyList[1].Value = 5; // Ignored by DistintUntilChanged | |
rxPropertyList[1].Value = 5; // Ignored by DistintUntilChanged | |
rxPropertyList[1].Value = 5; // Ignored by DistintUntilChanged | |
// Check | |
recordStreamOutput.IsCollection("OnNext(())", "OnNext(())", "OnNext(())", "OnNext(())", "OnNext(())"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment