Created
June 26, 2017 13:06
-
-
Save kassim/9e0c0c19473e00d96f2d9efcc1a5ef35 to your computer and use it in GitHub Desktop.
an RxRelay BehaviorRelay wrapper that only allows a value to be added once
This file contains hidden or 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
package com.jakewharton.rxrelay2; | |
import io.reactivex.Observer; | |
public class SingleValueRelay<T> extends Relay<T> { | |
private final BehaviorRelay<T> behaviorRelay = BehaviorRelay.create(); | |
public SingleValueRelay() {} | |
@Override | |
public void accept(T value) { | |
if (!behaviorRelay.hasValue()) { | |
behaviorRelay.accept(value); | |
} | |
} | |
@Override | |
public boolean hasObservers() { | |
return behaviorRelay.hasObservers(); | |
} | |
@Override | |
protected void subscribeActual(Observer<? super T> observer) { | |
behaviorRelay.subscribeActual(observer); | |
} | |
public boolean hasValue() { | |
return behaviorRelay.hasValue(); | |
} | |
public T getValue() { | |
return behaviorRelay.getValue(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment