Perform state reduction on main thread

This commit is contained in:
Moez Bhatti
2019-11-18 22:24:19 -05:00
parent 0e981da6f8
commit 9ea613bf39
2 changed files with 36 additions and 19 deletions
@@ -23,13 +23,26 @@ import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
abstract class QkPresenter<View : QkViewContract<State>, State>(initialState: State) {
val disposables = CompositeDisposable()
protected val disposables = CompositeDisposable()
protected val state: Subject<State> = BehaviorSubject.createDefault(initialState)
protected val state: BehaviorSubject<State> = BehaviorSubject.createDefault(initialState)
private val stateReducer: Subject<State.() -> State> = PublishSubject.create()
init {
// If we accidentally push a realm object into the state on the wrong thread, switching
// to mainThread right here should immediately alert us of the issue
disposables += stateReducer
.observeOn(AndroidSchedulers.mainThread())
.scan(initialState) { state, reducer -> reducer(state) }
.subscribe(state::onNext)
}
@CallSuper
open fun bindIntents(view: View) {
@@ -39,12 +52,8 @@ abstract class QkPresenter<View : QkViewContract<State>, State>(initialState: St
.subscribe(view::render)
}
protected fun newState(reducer: State.() -> State) {
state.value?.let { state.onNext(reducer(it)) }
}
protected fun newState(reducer: State.() -> State) = stateReducer.onNext(reducer)
open fun onCleared() {
disposables.dispose()
}
open fun onCleared() = disposables.dispose()
}
}
@@ -24,29 +24,37 @@ import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
abstract class QkViewModel<in View : QkView<State>, State>(initialState: State) : ViewModel() {
protected val state: BehaviorSubject<State> = BehaviorSubject.createDefault(initialState)
protected val disposables = CompositeDisposable()
protected val state: Subject<State> = BehaviorSubject.createDefault(initialState)
private val stateReducer: Subject<State.() -> State> = PublishSubject.create()
init {
// If we accidentally push a realm object into the state on the wrong thread, switching
// to mainThread right here should immediately alert us of the issue
disposables += stateReducer
.observeOn(AndroidSchedulers.mainThread())
.scan(initialState) { state, reducer -> reducer(state) }
.subscribe(state::onNext)
}
@CallSuper
open fun bindView(view: View) {
state
.observeOn(AndroidSchedulers.mainThread())
.autoDisposable(view.scope())
.subscribe { view.render(it) }
.subscribe(view::render)
}
protected fun newState(reducer: State.() -> State) {
state.value?.let { state.onNext(reducer(it)) }
}
protected fun newState(reducer: State.() -> State) = stateReducer.onNext(reducer)
override fun onCleared() {
super.onCleared()
disposables.dispose()
}
override fun onCleared() = disposables.dispose()
}