Class: Synapse::Command::FutureCallback

Inherits:
CommandCallback show all
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

Constructor Details

#initializeFutureCallback

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

Parameters:

  • exception (Exception)

    The cause of the failure

Returns:

  • (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

Parameters:

  • result (Object)

    The result from the command handler

Returns:

  • (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.

Parameters:

  • timeout (Float) (defaults to: nil)

Returns:

  • (Object)

    The result from the command handler

Raises:

  • (Exception)

    If an exception occured during command execution



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