Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Action start a sub statemachine #300

Merged
merged 34 commits into from
May 20, 2022
Merged

On Action start a sub statemachine #300

merged 34 commits into from
May 20, 2022

Conversation

sockeqwe
Copy link
Collaborator

@sockeqwe sockeqwe commented May 12, 2022

First implementation for #269

It adds the possibility to start a (sub) state machine from an action. Most of the code changes in this PR are actually on updating the sample app 🙈

public api will look very similar to stateMachine()

inState<Foo> {
        onActionStartStateMachine<SomeAction, SubStateMachineState>(
             factory = { SomeStateMachine() }
         ) { parentStateMachineState, subStateMachineState ->  ... }
 }

Internally FlowRedux keeps track of the Action that has started the sub Statemachine. if the same action (checked with == equals operator) is received and a substatemachine is currently running, then the previous sub statemachine will be stopped and a new instance starts again with initial state (so the behavior is kind of FlatMap.Latest or ExecutionPolicy.CANCEL_PREVIOUS). It was an intentional decision to not use ExecutionPolicy because it not only makes the internal implementation quite complex but it also is confusing for the consumer if multiple actions with same == are received and how it should then work in this case. Thus for now at least I would keep it simple with just a FlatMap.Latest or ExecutionPolicy.CANCEL_PREVIOUS behavior. Please note that since it uses == equals to check against actions the type of an action only is not the deciding factor.

example:

data class MyAction(val i : Int) : Action

then MyAction(1) and MyAction(2) are for FlowRedux 2 different actions that start 2 different StateMachines but if you dispatch MyAction(3) followed by another MyAction(3) then the first state machine started by MyAction(3) will be canceled because the second MyAction(3) stars a new sub statemachine

Next steps but out of scope of this PR:

  • stateMachine() should be renamed to onEnterStartStateMachine()
  • I think it makes sense to add a way to stop a substatemachine but I did not want to include it in this PR right now. Something like this:
inState<Foo> {
        onActionStartStateMachine<SomeAction, SubStateMachineState>(
             factory = { SomeStateMachine() },
            stopCondition { parentStateMachineState, subStateMachineState -> false } 
         ) { parentStateMachineState, subStateMachineState ->  ... }
 }

stopCondition will be invoked on every state change from parent and sub statemachine. By returning true the SubStatemachine will be stoped. Otherwise the SubStateMachine will only be stopped when surrounding inState<State> condition doesn't hold anymore. I could think of use cases where an additional stopCondition could be useful but doesn't necessary need to be implemented right now.

@sockeqwe sockeqwe requested a review from gabrielittner May 12, 2022 14:12
…dux/sample/shared/PaginationStateMachine.kt

Co-authored-by: Gabriel Ittner <[email protected]>
@sockeqwe sockeqwe merged commit 188c586 into main May 20, 2022
@sockeqwe sockeqwe deleted the action_start_statemachine branch May 20, 2022 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants