Class: Wire::MessageCallback

Inherits:
Object
  • Object
show all
Defined in:
lib/wire.rb

Instance Method Summary collapse

Constructor Details

#initialize(message_hash, message_id, callback, timeout_seconds) ⇒ MessageCallback

Returns a new instance of MessageCallback.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wire.rb', line 57

def initialize (message_hash, message_id, callback, timeout_seconds)
  @semaphore = Mutex.new
  @callback = callback
  @result = nil

  Thread.new do
    sleep(timeout_seconds)
    message_hash.delete(message_id)
    @semaphore.synchronize do
      if @result.nil?
        callback.timeout
      end
    end
  end
end

Instance Method Details

#set_result(result) ⇒ Object



73
74
75
76
77
78
# File 'lib/wire.rb', line 73

def set_result (result)
  @semaphore.synchronize do
    @result = result
    @callback.complete(@result)
  end
end