spotify / mobius

A functional reactive framework for managing state evolution and side-effects.
https://spotify.github.io/mobius/
Apache License 2.0
1.23k stars 97 forks source link

Add "addProducer" function into CoroutinesSubtypeEffectHandlerBuilder #173

Closed juanmrivero closed 10 months ago

juanmrivero commented 10 months ago

This PR introduces the method addProducer into CoroutinesSubtypeEffectHandlerBuilder.

This method is used to account for the cases where an Effect is consumed but ignored, like in the addAction method. But unlike the addAction method, and like the addFunction method, this new method is expected to produce an Event

Here are all the methods illustrated:

    Mobius.loop(
            Update(...),
            MobiusCoroutines.subtypeEffectHandler<Effect, Event>()
              .addAction<Effect1> { ... }
              .addConsumer<Effect2> { effect -> ...  }
              .addProducer<Effect3> {  event  }
              .addFunction<Effect4> { effect -> event  }
              .addFlowProducer<Effect5> { effect ->
                  flow {
                      emit(startingEvent)
                      ...
                      emit(endEvent)
                  }
             }
             .build()
        )

The new method is particularly useful when you are using method references instead of lambdas. Previously if you wanted to produce an event, you were required to use addFunction and declare your function with an unused parameter.

    Mobius.loop(
            Update(...),
            MobiusCoroutines.subtypeEffectHandler<Effect, Event>()
              .addProducer<Effect1>(::onEffect1)
              .addFunction<Effect2>(::onEffect2)
              .build()
        )

    fun onEffect1(): Event { ... }
    fun onEffect2(effect: Effect2): Event { ... }

Also, as a secondary minor task. I simplified the use of the annotation @ExperimentalCoroutinesApi in tests by moving it from every individual test to the test class.

alz-ahm commented 10 months ago

It seems that I don't have write access for the repo :-( I will bring the issue to the team next week when more people are back from vacation