やりたいこと
CombineLatestでまとめたObservableのどちらが流れてきたかを判断して、その片方のみで処理を行いたい。
普通にCombineLatest
var Subject1 = new Subject<string>();
var Subject2 = new Subject<string>();
Observable.CombineLatest(Subject1, Subject2, (subject1, subject2) =>
{
return new Tuple<string, string>(subject1, subject2);
})
.Subscribe(tuple => Debug.Log("subject1: " + tuple.Item1 + ", subject2: " + tuple.Item2));
Subject1.OnNext("1");
Subject2.OnNext("1");
Subject1.OnNext("2");
Subject2.OnNext("2");
Subject1.OnNext("3");
Subject2.OnNext("3");
subject1: 1, subject2: 1
subject1: 2, subject2: 1
subject1: 2, subject2: 2
subject1: 3, subject2: 2
subject1: 3, subject2: 3
両方流れてきた以降は片方が流れてくる度に処理される。
やりたいCombineLatest
var Subject1 = new Subject<string>();
var Subject2 = new Subject<string>();
Observable.CombineLatest(Subject1.Timestamp(), Subject2.Timestamp(), (subject1, subject2) =>
{
// TimeStampを見て、新しい方が今流れてきたと判断
var isUpdatedSubject1 = subject1.Timestamp > subject2.Timestamp;
return new Tuple<string, string, bool>(subject1.Value, subject2.Value, isUpdatedSubject1);
})
.Where(tuple => tuple.Item3)
.Subscribe(tuple => Debug.Log("subject1: " + tuple.Item1 + ", subject2: " + tuple.Item2));
Subject1.OnNext("1");
Subject2.OnNext("1");
Subject1.OnNext("2");
Subject2.OnNext("2");
Subject1.OnNext("3");
Subject2.OnNext("3");
subject1: 2, subject2: 1
subject1: 3, subject2: 2
両方流れてきた以降、片方(subject1)が流れてくる度に処理される。