Skip to content

Commit

Permalink
rememberStateAndDispatch: wrap StateAndDispatch ctor with remember (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 authored Aug 21, 2023
1 parent 4794624 commit 1b6675b
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,21 @@ public data class StateAndDispatch<S : Any, A : Any>(
public fun <S : Any, A : Any> FlowReduxStateMachine<S, A>.rememberStateAndDispatch(): StateAndDispatch<S, A> {
val stateMachine = this
val scope = rememberCoroutineScope()
return StateAndDispatch(
state = stateMachine.rememberState(),
dispatchAction = remember(stateMachine) {
{
action: A ->
scope.launch {
stateMachine.dispatch(action)
}

val state = stateMachine.rememberState()
val dispatchAction = remember(scope, stateMachine) {
{ action: A ->
scope.launch {
stateMachine.dispatch(action)
}
},
)
Unit
}
}

return remember(state, dispatchAction) {
StateAndDispatch(
state = state,
dispatchAction = dispatchAction,
)
}
}

0 comments on commit 1b6675b

Please sign in to comment.