1

I have following case of Reducer:

    class XYZ Reducer(private val state: BehaviorRelay<State>) {

     override fun invoke(events: Observable<Any>): Observable<State>  =   
              events.ofType(Event::class.java).map { event ->
              handleFieldClicked(event, state.value)
    }

As reducer requires 2 info:

  1. New event
  2. Previous state

I am passing behavior relay of states to the reducer. I tried to find a way to have access to previous state and current event in map{}, like scan, all I found was scan() that gives current event and previous event as arguments.

Do you know any better way (an operator?) than passing behaviorRelay as states directly?

dWinder
  • 11,597
  • 3
  • 24
  • 39
Orbite
  • 505
  • 4
  • 8

1 Answers1

1

You could combine flatMap() with take(1):

class XYZ Reducer(private val state: BehaviorRelay<State>) {
    override fun invoke(events: Observable<Any>): Observable<State>  =   
        events.ofType(Event::class.java).flatMap { event ->
            state.take(1).map { state ->
                handleFieldClicked(event, state)
            }
        }
}
tynn
  • 38,113
  • 8
  • 108
  • 143