Skip to content

Instantly share code, notes, and snippets.

@thealmikey
Last active January 21, 2020 02:52
Show Gist options
  • Save thealmikey/b6dce376fb194878ccb2c9ab76ce03bb to your computer and use it in GitHub Desktop.
Save thealmikey/b6dce376fb194878ccb2c9ab76ce03bb to your computer and use it in GitHub Desktop.
import com.tinder.StateMachine
sealed class HumanState {
object PureBliss : HumanState()
object Happy : HumanState()
object Bored : HumanState()
object Sad : HumanState()
}
sealed class HumanAction {
object Entertain : HumanAction()
object GiveLecture : HumanAction()
object Annoy : HumanAction()
object GiveTherapy : HumanAction()
}
sealed class UseResult {
object ReportHappiness : UseResult()
object ReportSadness : UseResult()
object ReportBoredness : UseResult()
object ReportGoodWork : UseResult()
object ReportVibeKiller : UseResult()
}
class Human(var name: String) {
val emotionalState = StateMachine.create<HumanState, HumanAction, UseResult> {
initialState(HumanState.Bored)
state<HumanState.Bored> {
on<HumanAction.Entertain> {
transitionTo(HumanState.Happy, UseResult.ReportHappiness)
}
on<HumanAction.GiveTherapy> {
transitionTo(HumanState.Happy, UseResult.ReportGoodWork)
}
on<HumanAction.Annoy> {
transitionTo(HumanState.Sad, UseResult.ReportSadness)
}
}
state<HumanState.PureBliss> {
on<HumanAction.Annoy> {
transitionTo(HumanState.Happy, UseResult.ReportHappiness)
}
on<HumanAction.GiveLecture> {
transitionTo(HumanState.Happy, UseResult.ReportGoodWork)
}
}
state<HumanState.Happy> {
on<HumanAction.GiveTherapy> {
transitionTo(HumanState.PureBliss, UseResult.ReportGoodWork)
}
on<HumanAction.GiveLecture> {
transitionTo(HumanState.Bored, UseResult.ReportBoredness)
}
on<HumanAction.Annoy> {
transitionTo(HumanState.Sad, UseResult.ReportSadness)
}
}
state<HumanState.Sad> {
on<HumanAction.Entertain> {
transitionTo(HumanState.Happy, UseResult.ReportGoodWork)
}
on<HumanAction.GiveTherapy> {
transitionTo(HumanState.Bored, UseResult.ReportGoodWork)
}
on<HumanAction.Annoy> {
transitionTo(HumanState.Bored, UseResult.ReportGoodWork)
}
}
onTransition {
val validTransition = it as? StateMachine.Transition.Valid ?: return@onTransition
when (validTransition.sideEffect) {
UseResult.ReportHappiness -> println("Hapiness message") //ourService.log("Hapiness message")
UseResult.ReportBoredness -> println("BoredNess message") //anotherService.log("BoredNess message")
UseResult.ReportSadness -> println("Bad vibes message") //ourService.log("Bad vibes message")
UseResult.ReportGoodWork -> println("Goodwork report") //anotherService.log("Goodwork report")
UseResult.ReportVibeKiller -> println("Please don't kill my vibe")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment