Class: Synapse::Command::FutureCallback
- Inherits:
-
CommandCallback
- Object
- CommandCallback
- Synapse::Command::FutureCallback
- Defined in:
- lib/synapse/command/callbacks/future.rb
Overview
Callback that provides a deferred result or exception from the execution of a command
Instance Method Summary collapse
-
#initialize ⇒ FutureCallback
constructor
A new instance of FutureCallback.
- #on_failure(exception) ⇒ undefined
- #on_success(result) ⇒ undefined
-
#result(timeout = nil) ⇒ Object
The result from the command handler.
Constructor Details
#initialize ⇒ FutureCallback
Returns a new instance of FutureCallback.
5 6 7 8 |
# File 'lib/synapse/command/callbacks/future.rb', line 5 def initialize @mutex = Mutex.new @condition = ConditionVariable.new end |
Instance Method Details
#on_failure(exception) ⇒ undefined
40 41 42 43 44 45 46 47 |
# File 'lib/synapse/command/callbacks/future.rb', line 40 def on_failure(exception) @mutex.synchronize do @dispatched = true @exception = exception @condition.broadcast end end |
#on_success(result) ⇒ undefined
29 30 31 32 33 34 35 36 |
# File 'lib/synapse/command/callbacks/future.rb', line 29 def on_success(result) @mutex.synchronize do @dispatched = true @result = result @condition.broadcast end end |
#result(timeout = nil) ⇒ Object
Returns The result from the command handler.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/synapse/command/callbacks/future.rb', line 13 def result(timeout = nil) @mutex.synchronize do unless @dispatched @condition.wait @mutex, timeout end if @exception raise @exception end @result end end |