mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 01:40:07 +01:00
Add RxStore and StoryViewerPage forward navigation.
This commit is contained in:
committed by
Cody Henthorne
parent
11c3ea769e
commit
3e42c044b8
@@ -0,0 +1,30 @@
|
||||
package org.thoughtcrime.securesms.util.rx
|
||||
|
||||
import io.reactivex.rxjava3.core.Flowable
|
||||
import io.reactivex.rxjava3.processors.BehaviorProcessor
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import io.reactivex.rxjava3.subjects.PublishSubject
|
||||
|
||||
/**
|
||||
* Rx replacement for Store.
|
||||
* Actions are run on the computation thread.
|
||||
*/
|
||||
class RxStore<T : Any>(defaultValue: T) {
|
||||
|
||||
private val behaviorProcessor = BehaviorProcessor.createDefault(defaultValue)
|
||||
private val actionSubject = PublishSubject.create<(T) -> T>().toSerialized()
|
||||
|
||||
val state: T get() = behaviorProcessor.value!!
|
||||
val stateFlowable: Flowable<T> = behaviorProcessor
|
||||
|
||||
init {
|
||||
actionSubject
|
||||
.observeOn(Schedulers.computation())
|
||||
.scan(defaultValue) { v, f -> f(v) }
|
||||
.subscribe { behaviorProcessor.onNext(it) }
|
||||
}
|
||||
|
||||
fun update(transformer: (T) -> T) {
|
||||
actionSubject.onNext(transformer)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user