Comments (6)
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.
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.
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.
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.
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.
Yes this was very helpful, thanks.
from flowredux.
Related Issues (20)
- Cannot dispatch action because state Flow of this FlowReduxStateMachine is not collected yet HOT 2
- ChangedState reduce() type mismatch HOT 3
- [Question] Detail page implementation help HOT 6
- DSL: Add a way to work with"Identity" of states HOT 3
- 2.0 todo list
- Action Required: Fix Renovate Configuration
- FlowRedux and Flow from usecases HOT 1
- Support null values for identity? HOT 1
- Improve hierarchical state machines
- [Question] Search Implementation HOT 3
- [Idea] Generate a state transition graph in compile time HOT 2
- [Question] How to dispatch actions in one coroutine? HOT 1
- [Question] Dead lock in dispatching multiple actions to ORDERED OnAction statement HOT 4
- Cannot dispatch action because state Flow of this FlowReduxStateMachine is not collected yet HOT 7
- 1.2.0 seems to be broken regarding iOS HOT 2
- Unify state (snapshot) access and parameters in DSL
- [Question] State Collection On Multiple Screens HOT 1
- Cannot dispatch action because state Flow of this FlowReduxStateMachine is not collected yet HOT 11
- More unique Exceptions HOT 3
- Cannot inline bytecode built with JVM target 11 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flowredux.