Class: JSCommander::InProcessBroker

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

Instance Method Summary collapse

Constructor Details

#initializeInProcessBroker

Returns a new instance of InProcessBroker.



3
4
5
# File 'lib/jscmd/inprocessbroker.rb', line 3

def initialize
  @subscribers = {}
end

Instance Method Details

#send(name, msg) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jscmd/inprocessbroker.rb', line 17

def send(name, msg)
  if @subscribers[name]
    @subscribers[name].each do |sub|
      Thread.start do
        begin
          sub.call(msg)
        rescue Exception
          $stderr.puts $!
        end
      end
    end
  end
end

#subscribe(name, &block) ⇒ Object



7
8
9
10
11
# File 'lib/jscmd/inprocessbroker.rb', line 7

def subscribe(name, &block)
  @subscribers[name] ||= []
  @subscribers[name] << block
  block
end

#unsubscribe(name, subscriber) ⇒ Object



13
14
15
# File 'lib/jscmd/inprocessbroker.rb', line 13

def unsubscribe(name, subscriber)
  @subscribers[name].delete(subscriber)
end