Skip to content

open-jumpco/kfsm-web

Repository files navigation

KFSM Turnstile Sample for Browser

88x31

A simple application to demonstrate implementing [KFSM](https://github.com/open-jumpco/kfsm) with the classic Turnstile FSM.

This implementation uses Kotlin code to manipulate the register event handlers on the buttons and manipulate the DOM to update the display state of the elements.

./gradlew build

All the files needed to run will be in build/dist

Or you can launch it using

./gradlew browserRun

This FSM has been updated to support a timeout.

The Turnstile will only remain unlocked for 3000ms and then the timeout will be triggered.

The FSM DSL is as follows:

asyncStateMachine(
    TurnstileState.values().toSet(),
    TurnstileEvent.values().toSet(),
    Turnstile::class
) {
    initialState { if (locked) TurnstileState.LOCKED else TurnstileState.UNLOCKED }
    default {
        action { _, _, _ ->
            alarm()
        }
    }
    whenState(TurnstileState.LOCKED) {
        onEvent(TurnstileEvent.COIN to TurnstileState.UNLOCKED) {
            unlock()
        }
    }
    whenState(TurnstileState.UNLOCKED) {
        onEvent(TurnstileEvent.PASS to TurnstileState.LOCKED) {
            lock()
        }
        onEvent(TurnstileEvent.COIN) {
            returnCoin()
        }
        timeout(LOCKED, 3000) {
            timeout()
        }
    }
}

Generated State Diagram

turnstile

To learn more about visualization visit kfsm-viz and kfsm-viz-plugin

Releases

No releases published

Packages

No packages published