Skip to content

Instantly share code, notes, and snippets.

@czyrux
Last active October 30, 2016 19:11
Show Gist options
  • Save czyrux/f46d818d65cbeee4e80de7ba4504252e to your computer and use it in GitHub Desktop.
Save czyrux/f46d818d65cbeee4e80de7ba4504252e to your computer and use it in GitHub Desktop.
Store concept using RxJava subject
import com.jakewharton.rxrelay.BehaviorRelay;
import com.jakewharton.rxrelay.Relay;
import rx.Observable;
public class Store<T> {
private final Relay<T, T> storeSubject;
public Store() {
this.storeSubject = BehaviorRelay.<T>create().toSerialized();
}
public Store(T defaultValue) {
this.storeSubject = BehaviorRelay.create(defaultValue).toSerialized();
}
public Observable<T> observe() {
return storeSubject.asObservable()
.distinctUntilChanged();
}
public void publish(T value) {
storeSubject.call(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment