Last active
December 18, 2015 18:19
-
-
Save jbrisbin/5825012 to your computer and use it in GitHub Desktop.
Sample StateMachine usage for Reactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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