Skip to content

Instantly share code, notes, and snippets.

@abhimuktheeswarar
Last active August 6, 2021 06:15
Show Gist options
  • Select an option

  • Save abhimuktheeswarar/515a11b950c3fdd4d6087a5725ae0f82 to your computer and use it in GitHub Desktop.

Select an option

Save abhimuktheeswarar/515a11b950c3fdd4d6087a5725ae0f82 to your computer and use it in GitHub Desktop.

Revisions

  1. abhimuktheeswarar revised this gist Aug 6, 2021. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions counterStateMachine.kt
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    fun CoroutineScope.counterStateMachine(
    initialState: CounterState,
    mutableStateFlow: MutableStateFlow<CounterState>,
    mutableActions: MutableSharedFlow<CounterMessage>,
    mutableMessages: MutableSharedFlow<CounterMessage>,
    ) =
    actor<CounterMessage> {
    var state: CounterState = initialState
    @@ -10,12 +10,12 @@ fun CoroutineScope.counterStateMachine(
    is IncrementCounter -> {
    state = state.copy(count = state.count + 1)
    mutableStateFlow.emit(state)
    mutableActions.emit(message)
    mutableMessages.emit(message)
    }
    is DecrementCounter -> {
    state = state.copy(count = state.count - 1)
    mutableStateFlow.emit(state)
    mutableActions.emit(message)
    mutableMessages.emit(message)
    }
    is GetCounterState -> message.deferred.complete(state)
    }
    @@ -26,12 +26,12 @@ fun CoroutineScope.counterStateMachine(
    class CounterStateStore(initialState: CounterState, val scope: CoroutineScope) {

    private val mutableStateFlow = MutableStateFlow<CounterState>(initialState)
    private val mutableActions = MutableSharedFlow<CounterMessage>()
    private val mutableMessages = MutableSharedFlow<CounterMessage>()
    private val stateMachine =
    scope.counterStateMachine(initialState, mutableStateFlow, mutableActions)
    scope.counterStateMachine(initialState, mutableStateFlow, mutableMessages)

    val stateFlow: Flow<CounterState> = mutableStateFlow
    val actionsFlow: Flow<CounterMessage> = mutableActions
    val messagesFlow: Flow<CounterMessage> = mutableMessages

    fun dispatch(message: CounterMessage) {
    stateMachine.trySend(message)
  2. Abhi Muktheeswarar revised this gist Aug 1, 2021. 1 changed file with 24 additions and 1 deletion.
    25 changes: 24 additions & 1 deletion counterStateMachine.kt
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,27 @@ fun CoroutineScope.counterStateMachine(
    is GetCounterState -> message.deferred.complete(state)
    }
    }
    }
    }


    class CounterStateStore(initialState: CounterState, val scope: CoroutineScope) {

    private val mutableStateFlow = MutableStateFlow<CounterState>(initialState)
    private val mutableActions = MutableSharedFlow<CounterMessage>()
    private val stateMachine =
    scope.counterStateMachine(initialState, mutableStateFlow, mutableActions)

    val stateFlow: Flow<CounterState> = mutableStateFlow
    val actionsFlow: Flow<CounterMessage> = mutableActions

    fun dispatch(message: CounterMessage) {
    stateMachine.trySend(message)
    }

    suspend fun getState(): CounterState {
    val completableDeferred = CompletableDeferred<CounterState>()
    dispatch(GetCounterState(completableDeferred))
    return completableDeferred.await()
    }

    }
  3. Abhi Muktheeswarar created this gist Aug 1, 2021.
    23 changes: 23 additions & 0 deletions counterStateMachine.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    fun CoroutineScope.counterStateMachine(
    initialState: CounterState,
    mutableStateFlow: MutableStateFlow<CounterState>,
    mutableActions: MutableSharedFlow<CounterMessage>,
    ) =
    actor<CounterMessage> {
    var state: CounterState = initialState
    channel.consumeEach { message ->
    when (message) {
    is IncrementCounter -> {
    state = state.copy(count = state.count + 1)
    mutableStateFlow.emit(state)
    mutableActions.emit(message)
    }
    is DecrementCounter -> {
    state = state.copy(count = state.count - 1)
    mutableStateFlow.emit(state)
    mutableActions.emit(message)
    }
    is GetCounterState -> message.deferred.complete(state)
    }
    }
    }