Updated reactiv store code to suspend side effects

This commit is contained in:
Rohan Sircar 2020-12-21 13:17:15 +05:30
parent 338f7b16f9
commit 918b893fe8

View File

@ -7,6 +7,8 @@ import io.odin.Logger
import monix.bio.Task
import monix.reactive.OverflowStrategy
import monix.reactive.subjects.ConcurrentSubject
import monix.eval.Coeval
import monix.reactive.Observable
object Store {
def createL[A, M](
@ -21,8 +23,9 @@ object Store {
Task {
val subject = ConcurrentSubject.publish[A](overflowStrategy)
val fold: ((A, M), A) => (A, M) = {
case ((_, state), action) => {
val fold: ((A, M), A) => Coeval[(A, M)] = {
case ((_, state), action) =>
Coeval {
val (newState, effects) = reducer(state, action)
effects.subscribe(subject.onNext _)
@ -31,10 +34,14 @@ object Store {
}
}
val obs = subject
.scan[(A, M)](initialAction -> initialState)(fold)
val obs = Observable.suspend(
subject
.scanEval0F[Coeval, (A, M)](
Coeval.pure(initialAction -> initialState)
)(fold)
.behavior(initialAction -> initialState)
.refCount
)
val res = middlewares.foldLeft(obs) {
case (obs, middleware) => middleware(obs)