GithubHelp home page GithubHelp logo

Comments (6)

sockeqwe avatar sockeqwe commented on August 22, 2024

Hey Bodo,
happy to hear that you like it.
You would make this information part of your State ( and use GetState to retrieve the state)

In general it's a good rule of thumb to make all information needed to restore the full state part of your State (then you get state restoration almost for free, i.e. restoring loading state from Bundle would then trigger onEnter again and load screen).

I.e. I think you should have it as part of your Loading State:

data class Loading(
   val refresh : Boolean, 
   internal val ref : Ref  // make this private or internal so it is only accessible for your state machine, but not for your View
)


suspend fun loadScreen(getState: GetState<ActivityViewState>, setState: SetState<ActivityViewState>) {
   val state = getState() as LoadingState
   val ref = state.ref

   loadData(ref)

   ...
}

Does that help?

from flowredux.

Bodo1981 avatar Bodo1981 commented on August 22, 2024

Hey Hannes,

thanks for the hint to put it in the state!

whats best to the data (e.g. load the feed with other parameters, e.g. to the elements sorted desc). i think i will do it like this:

init {
  spec {
    inState<Loading> {
      onEnter(block = ::loadScreen)
    }
    inState<Content<Any>> {
      on<ActivityReloadAction<Ref>> { _, _, setState ->
        setState { Loading(refresh = true, ref = newRef) }
      }
    }
    inState<Error> {
      on<ActivityReloadAction<Ref>> { _, _, setState ->
        setState { Loading(refresh = false, ref = newRef) }
      }
    }
  }
}

-> that means i will pass the new ref with the updated request data via the ActivityReloadAction to the Loading ViewState

from flowredux.

sockeqwe avatar sockeqwe commented on August 22, 2024

I'm sorry, I don't fully understand the relationship between other parameters, i.e. sort order, and Ref and ActivityReloadAction. Does ref contain that parameters (i.e. sort order parameters)?

from flowredux.

Bodo1981 avatar Bodo1981 commented on August 22, 2024

So for example the inital load will be done with the following request:
http://www.foo.com?sort=asc
-> Ref(sort = "asc")

When the user wants to sort the list desc he has to call the feed with another parameter, so i dispatch the ActivityReloadAction(ref = Ref(sort = "desc")
http://www.foo.com?sort=desc

inState<Content<Any>> {
    on<ActivityReloadAction<Ref>> { _, _, setState ->
        setState { Loading(refresh = true, ref = newRef) }
    }
}

so the action contains the ref with the new url parameters to load the feed

from flowredux.

sockeqwe avatar sockeqwe commented on August 22, 2024

Yes, you can do it like that.

One Anti-Pattern that you should avoid (not sure if it related to your example) is to not leak any state related information in via Actions. For example, let's do a simple pagination example:

data class NextPageAction(
   val nextPage : Int   // Don't do this!
) : Action


inState<Content> {
    on<NextPageAction> { action, _, setState ->
        setState { Loading(page = action.nextPage) }
    }
}

The "anti pattern" here is that essentially an Action can "override" completely the state of your state machine. For example at some point, most likely in your UI, you have to create a NextPageAction(nextPage = currentPage + 1) that then gets dispatched to the State machine. The problem is that now you have some business logic currentPage + 1 outside of your state machine (most likely in your UI). So if you have a bug here, i.e. NextPageAction(nextPage = currentPage + 10) then your state machine would suddenly skip 9 pages in between.

Instead the current page should be part of your State:

object NextPageAction : Action

sealed class State {
   abstract val page : Int  
   data class Content( override page : Int) : State
   data class Loading( override page : Int) : State
}

inState<Content> {
    on<NextPageAction> { action, getState, setState ->
        setState { Loading(page = getState().page + 1) } // Now state machine drives the state
    }
}

It sounds a little bit that Ref could lead you into this "antipattern". It might be still ok to do that as long as you are aware of the downsides (i.e. passing a wrong Ref in could lead the state machine into a totally unwanted state transition because that Ref is basically overruling the state machine internal business rules or jump to a state that you didnt want to actually).

Does that help?

from flowredux.

Bodo1981 avatar Bodo1981 commented on August 22, 2024

Yes this was very helpful, thanks.

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.