Commit ad4ffb81 authored by Simeon Anfinrud's avatar Simeon Anfinrud Committed by Commit Bot

[Chromecast] Refactor Observable#andThen().

We can express the sequencing operation by leveraging the
sequenced order that observers are notified and remove the need
for a stateful inner class.

Bug: None
Test: cast_base_junit_tests, cast_shell_junit_tests
Change-Id: I48988d4f82e0ad0b77c085123173b67c5acc6930
Reviewed-on: https://chromium-review.googlesource.com/1129376Reviewed-by: default avatarLuke Halliwell <halliwell@chromium.org>
Commit-Queue: Simeon Anfinrud <sanfin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#573553}
parent a663e7fd
......@@ -69,7 +69,13 @@ public abstract class Observable<T> {
* @param <U> The activation data type of the other Observable.
*/
public final <U> Observable<Both<T, U>> andThen(Observable<U> other) {
return new SequenceStateObserver<>(this, other).asObservable();
Controller<U> otherAfterThis = new Controller<>();
other.watch((U value) -> {
otherAfterThis.set(value);
return otherAfterThis::reset;
});
watch(ScopeFactories.onEnter(x -> otherAfterThis.reset()));
return and(otherAfterThis);
}
/**
......@@ -114,32 +120,4 @@ public abstract class Observable<T> {
});
return opposite;
}
// Owns a Controller that is activated only when the Observables are activated in order.
private static class SequenceStateObserver<A, B> {
private final Controller<Both<A, B>> mController = new Controller<>();
private A mA = null;
private SequenceStateObserver(Observable<A> stateA, Observable<B> stateB) {
stateA.watch((A a) -> {
mA = a;
return () -> {
mA = null;
mController.reset();
};
});
stateB.watch((B b) -> {
if (mA != null) {
mController.set(Both.both(mA, b));
}
return () -> {
mController.reset();
};
});
}
private Observable<Both<A, B>> asObservable() {
return mController;
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment