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

Add support for Effects that dont change state #129

Closed
sockeqwe opened this issue Jun 18, 2021 · 6 comments
Closed

Add support for Effects that dont change state #129

sockeqwe opened this issue Jun 18, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@sockeqwe
Copy link
Collaborator

At the moment you cannot do a piece of work without the need of returning a ChangeState object.

This proposal is about adding "effects". A typical use case for an Effect is triggering navigation without changing the state of the statemachine itself or doing some analytics tracking calls.

Proposed Syntax

inState<MyState> {
  onEnterEffect { stateSnapshot : MyState ->
       // Do something.
       Unit // return unit, no state change
   }

  onActionEffect<MyAction> action : MyAction, stateSnapshot : MyState ->
       // Do something.
       Unit // return unit, no state change
   }

  collectWhileInStateEffect(flowOf(1,2)) { value : Int, 
stateSnapshot : MyState ->
       // Do something.
       Unit // return unit, no state change
   }
}

Specs:

  • Effects are suspending: i.e. signature lambda for onEnterEffect is suspend (State) -> Unit
  • Effects dont get canceled when state transition happening.
  • Effects are per default canceled when StateMachine CoroutineScope gets canceled. If you want to escape you can always use withContext(NonCancelable)
@sockeqwe sockeqwe added the enhancement New feature or request label Jun 18, 2021
@gabrielittner
Copy link
Member

Why do we need the explicit Unit that the effect returns?

@sockeqwe
Copy link
Collaborator Author

sockeqwe commented Jul 6, 2021

@gabrielittner you mean why it would not just return Any? instead of Unit or what do you mean exactly?

@gabrielittner
Copy link
Member

More like, why does the lambda need to return anything at all?

@sockeqwe
Copy link
Collaborator Author

sockeqwe commented Jul 6, 2021

Hm, maybe I misunderstand you but what is the signature of such a lambda? What would you propose (can you give an example)?

@gabrielittner
Copy link
Member

The signature would still be fun onEnterEffect(effect: (stateSnapshot : MyState) -> Unit), but the usage like this

onEnterEffect { stateSnapshot : MyState ->
   // Do something.
   // no Unit here
}

Effectively the same, I guess I was just confused by the explicit Unit as last line in your examples.

@sockeqwe
Copy link
Collaborator Author

sockeqwe commented Jul 6, 2021

👍 got it. Then we are talking about the same thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants