Browse Source

Updated reactiv store code to suspend side effects

master
Rohan Sircar 3 years ago
parent
commit
918b893fe8
  1. 27
      src/main/scala/nova/monadic_sfx/util/reactive/store/Store.scala

27
src/main/scala/nova/monadic_sfx/util/reactive/store/Store.scala

@ -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,20 +23,25 @@ object Store {
Task {
val subject = ConcurrentSubject.publish[A](overflowStrategy)
val fold: ((A, M), A) => (A, M) = {
case ((_, state), action) => {
val (newState, effects) = reducer(state, action)
val fold: ((A, M), A) => Coeval[(A, M)] = {
case ((_, state), action) =>
Coeval {
val (newState, effects) = reducer(state, action)
effects.subscribe(subject.onNext _)
effects.subscribe(subject.onNext _)
action -> newState
}
action -> newState
}
}
val obs = subject
.scan[(A, M)](initialAction -> initialState)(fold)
.behavior(initialAction -> initialState)
.refCount
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)

Loading…
Cancel
Save