Last active
October 30, 2016 19:11
-
-
Save czyrux/f46d818d65cbeee4e80de7ba4504252e to your computer and use it in GitHub Desktop.
Store concept using RxJava subject
This file contains 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
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