Skip to content

Instantly share code, notes, and snippets.

@kassim
Created June 26, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kassim/9e0c0c19473e00d96f2d9efcc1a5ef35 to your computer and use it in GitHub Desktop.
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
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