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

More than one SideEffect? #17

Closed
franciscoaleixo opened this issue Jun 13, 2019 · 1 comment
Closed

More than one SideEffect? #17

franciscoaleixo opened this issue Jun 13, 2019 · 1 comment

Comments

@franciscoaleixo
Copy link

Hi, so I was wondering if there's a recommended way to have more than one SideEffect for a transition? Is it simply repeating a transitionTo to the destination state but with different SideEffects?

And yes, I realize I could just simply have unique SideEffects for each transition, but that would lead to a lot of repeated code in my case. If I'm missing anything please let me know. Thanks!

For example (see // THIS comments):

val stateMachine = StateMachine.create<State, Event, SideEffect> {
    initialState(State.Solid)
    state<State.Solid> {
        on<Event.OnMelted> {
            transitionTo(State.Liquid, SideEffect.LogMelted)
            transitionTo(State.Liquid, SideEffect.LogOtherLiquidInfo) // THIS
        }
    }
    state<State.Liquid> {
        on<Event.OnFroze> {
            transitionTo(State.Solid, SideEffect.LogFrozen)
        }
        on<Event.OnVaporized> {
            transitionTo(State.Gas, SideEffect.LogVaporized)
        }
    }
    state<State.Gas> {
        on<Event.OnCondensed> {
            transitionTo(State.Liquid, SideEffect.LogCondensed)
            transitionTo(State.Liquid, SideEffect.LogOtherLiquidInfo)  // THIS
        }
    }
    onTransition {
        val validTransition = it as? StateMachine.Transition.Valid ?: return@onTransition
        when (validTransition.sideEffect) {
            SideEffect.LogMelted -> logger.log(ON_MELTED_MESSAGE)
            SideEffect.LogFrozen -> logger.log(ON_FROZEN_MESSAGE)
            SideEffect.LogVaporized -> logger.log(ON_VAPORIZED_MESSAGE)
            SideEffect.LogCondensed -> logger.log(ON_CONDENSED_MESSAGE)
            SideEffect.LogOtherLiquidInfo-> logger.log(ON_OTHER_LIQUID_INFO)  // THIS
        }
    }
}
@franciscoaleixo
Copy link
Author

franciscoaleixo commented Jun 13, 2019

Oh, nevermind. In this case we could simply check the destination state, i.e. validTransition.toState == State.Liquid in the transition. So I guess this would be:

val stateMachine = StateMachine.create<State, Event, SideEffect> {
    initialState(State.Solid)
    state<State.Solid> {
        on<Event.OnMelted> {
            transitionTo(State.Liquid, SideEffect.LogMelted)
        }
    }
    state<State.Liquid> {
        on<Event.OnFroze> {
            transitionTo(State.Solid, SideEffect.LogFrozen)
        }
        on<Event.OnVaporized> {
            transitionTo(State.Gas, SideEffect.LogVaporized)
        }
    }
    state<State.Gas> {
        on<Event.OnCondensed> {
            transitionTo(State.Liquid, SideEffect.LogCondensed)
        }
    }
    onTransition {
        val validTransition = it as? StateMachine.Transition.Valid ?: return@onTransition
        if (validTransition.toState == State.Liquid) {
            logger.log(ON_OTHER_LIQUID_INFO)
        }
        when (validTransition.sideEffect) {
            SideEffect.LogMelted -> logger.log(ON_MELTED_MESSAGE)
            SideEffect.LogFrozen -> logger.log(ON_FROZEN_MESSAGE)
            SideEffect.LogVaporized -> logger.log(ON_VAPORIZED_MESSAGE)
            SideEffect.LogCondensed -> logger.log(ON_CONDENSED_MESSAGE)
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant