Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Last active December 18, 2015 18:19
Show Gist options
  • Save jbrisbin/5825012 to your computer and use it in GitHub Desktop.
Save jbrisbin/5825012 to your computer and use it in GitHub Desktop.
Sample StateMachine usage for Reactor
// reactor-fsm: github.com/jbrisbin/reactor-fsm
// reactor: github.com/reactor/reactor
StateMachine ioMachine = new StateMachine.Spec().
sync().
using('idle', 'wait', 'send-request', 'response-ready').
when('send-request') { prev ->
def msg = requests.peek()
send(msg)
'wait'
}.
when('response-ready') { prev ->
Promise p = requests.pop().promise
def msg = responses.pop()
p.set msg
requests.empty ? 'idle' : 'send-request'
}.
get()
// when sending
requests << new Message(req)
if(ioMachine.current() == 'idle') {
// currently waiting to send since nothing is inflight
ioMachine.state('send-request')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment