Created
November 6, 2017 23:25
-
-
Save jonathanrz/20b2fa29e8053a4460c6ac57deed696e to your computer and use it in GitHub Desktop.
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
| class DatabaseObservable<T>(private val generateData: () -> List<T>, private val mergeData: (cache: List<T>, data: T) -> List<T>) { | |
| private var bs: BehaviorSubject<List<T>>? = null | |
| fun cache(): Observable<List<T>> { | |
| if(bs == null) { | |
| bs = BehaviorSubject.create() | |
| AsyncTask.execute { bs?.onNext(generateData()) } | |
| } | |
| return bs!!.replay(1).autoConnect() | |
| } | |
| fun newData(data: T) { | |
| AsyncTask.execute { bs?.onNext(mergeData(bs!!.blockingFirst(), data)) } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment