top | item 42514396

(no title)

euph0ria | 1 year ago

Would love to understand this better. Is there any example you could point to?

discuss

order

casenmgreen|1 year ago

    init -> success -> red
    init -> failure -> cleanup

    red -> success -> red_yellow
    red -> failure -> cleanup

    red_yellow -> success -> green
    red_yellow -> failure -> cleanup

    green -> success -> yellow
    green -> failure -> cleanup

    yellow -> success -> red
    yellow -> failure -> cleanup

    cleanup -> done -> finish
init/red/etc are states.

success/failure/etc are events.

Each state is a function. The function red() for example, waits for 20 seconds, then returns success (assuming nothing went wrong).

To start the state machine, initializes state to "init", and enter a loop, in the loop you call the function for the current state (which makes that state actually happen and do whatever it does), and that function returns its event for whatever happen when it was run, and you then call a second function, which updates state based on the event which just occurred. Keep doing that, until you hit state "finish", then you're done.

euph0ria|1 year ago

Got it, thanks. But it seemed from your original post that you tend to write state machines a lot more than the usual engineer does, would that be correct? Would you use this in a crud rest API for example?