class Synapse::Command::FutureCallback

Callback that provides a deferred result or exception from the execution of a command

Public Class Methods

new() click to toggle source
# File lib/synapse/command/callbacks/future.rb, line 5
def initialize
  @mutex = Mutex.new
  @condition = ConditionVariable.new

  @dispatched = false
end

Public Instance Methods

on_failure(exception) click to toggle source

@param [Exception] exception The cause of the failure @return [undefined]

# 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) click to toggle source

@param [Object] result The result from the command handler @return [undefined]

# 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) click to toggle source

@raise [Exception] If an exception occured during command execution @param [Float] timeout @return [Object] The result from the command handler

# File lib/synapse/command/callbacks/future.rb, line 15
def result(timeout = nil)
  @mutex.synchronize do
    unless @dispatched
      @condition.wait @mutex, timeout
    end

    raise @exception if @exception

    @result
  end
end