Tinder / StateMachine

A Kotlin and Swift DSL for finite state machine
Other
1.96k stars 128 forks source link

State machine design: transitioning before effect? #19

Open ursusursus opened 5 years ago

ursusursus commented 5 years ago

Hi, I wanna bounce of you a design question, since im trying to rewrite "implicit" state machine to explicit one

Usually the code looks like this

fun foo() {
   if (currentState == Bar) {
    doSomething()
    currentState = Quax
   }
}

What I usually see is people having transition function, which according to input action changes state, but however it would do so before doSomething is ran, to me which is a problem since currentState will be observable, and subscribers expect doSomething to have ran if state is Quax

Do I need some PRE + POST_state pairs or something? Seems weird for synchronous code

What would be your solution?

ursusursus commented 5 years ago

Ok I have looked at the way you guys use it in Scarlet

https://github.com/Tinder/Scarlet/blob/5457f827884269a7e1a7c1c60d057d0ed5fa26b1/scarlet/src/main/java/com/tinder/scarlet/internal/connection/Connection.kt

So, you are baking the statemachine inside StateManager, which then you can run those sideffects before transitions, great

However, this messes up testing, as you can only test StateManager, and statemachine become implmentation details, which is maybe okay

But you lose ability to set initialState, unless you pass it in via StateManager ctor, which is bad, since youre leaking your implementation detail to call sites

So...I'd like your guidance on this please