GithubHelp home page GithubHelp logo

Comments (2)

gabrielittner avatar gabrielittner commented on August 22, 2024 1

This is happening because of the general behavior of StateFlow. Since StateFlow has its own initial value that will get emitted immediately to Turbine. So the awaitItem is handled and dispatch is called. The issue is that that doesn't mean that stateIn has started collecting the upstream state yet and because StateFlow has a builtIn distinctUntilChanged there is also no way to wait for the actual first emission of FlowReduxStateMachine. There are a few solutions for this:

  • use state instead of stateFlow in tests, that way the first emission for awaitItem comes from FlowReduxStateMachine and the on/dispatch is guaranteed to work
  • replace StateFlow with SharedFlow by using shareIn(..., replay = 1), similar to the above but lets you use the shared flow for tests so that the test is closer to a real consumer
  • have a different initial state for the StateFlow and the StateMachine, probably not a nice solution

I would also generally recommend one of the first 2 in tests. StateFlow has some things like conflation of values that can easily make tests flaky since you might not get in between emissions sometimes.

from flowredux.

JBokMan avatar JBokMan commented on August 22, 2024 1

Thank you so much!

We chose the second solution. In the example code the change is just from

val stateFlow: StateFlow<MachineState> = state.stateIn(
    scope = CoroutineScope(Dispatchers.Default),
    started = SharingStarted.Eagerly,
    initialValue = initialState
)

to

val stateFlow: SharedFlow<MachineState> = state.shareIn(
    scope = CoroutineScope(Dispatchers.Default),
    started = SharingStarted.Eagerly,
    replay = 1
)

from flowredux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.