Class: Synapse::Command::FutureCallback

Inherits:
CommandCallback show all
Defined in:
lib/synapse/command/command_callback.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeFutureCallback

Returns a new instance of FutureCallback.



20
21
22
23
# File 'lib/synapse/command/command_callback.rb', line 20

def initialize
  @mutex = Mutex.new
  @condition = ConditionVariable.new
end

Instance Method Details

#on_failure(exception) ⇒ undefined

Parameters:

  • exception (Exception)

    The cause of the failure

Returns:

  • (undefined)


55
56
57
58
59
60
61
62
# File 'lib/synapse/command/command_callback.rb', line 55

def on_failure(exception)
  @mutex.synchronize do
    @dispatched = true
    @exception = exception

    @condition.broadcast
  end
end

#on_success(result) ⇒ undefined

Parameters:

  • result (Object)

    The result from the command handler

Returns:

  • (undefined)


44
45
46
47
48
49
50
51
# File 'lib/synapse/command/command_callback.rb', line 44

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.

Parameters:

  • timeout (Float) (defaults to: nil)

Returns:

  • (Object)

    The result from the command handler

Raises:

  • (Exception)

    If an exception occured during command execution



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/synapse/command/command_callback.rb', line 28

def result(timeout = nil)
  @mutex.synchronize do
    unless @dispatched
      @condition.wait @mutex, timeout
    end

    if @exception
      raise @exception
    end

    @result
  end
end