Class: PubSub::Hash

Inherits:
Container show all
Defined in:
lib/pub_sub/hash.rb

Instance Method Summary collapse

Constructor Details

#initializeHash

Returns a new instance of Hash.



7
8
9
# File 'lib/pub_sub/hash.rb', line 7

def initialize
  @container = ::RequestStore.store[:hash] = {}
end

Instance Method Details

#pub(event, args = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/pub_sub/hash.rb', line 11

def pub(event, args={})
  iterable_subscriber_list = @container[event]
  return false unless iterable_subscriber_list

  clazzes = retrieve_klasses(iterable_subscriber_list)
  fan_out(clazzes, args)
end

#sub(event, klazz_name) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/pub_sub/hash.rb', line 19

def sub(event, klazz_name)
  mutex = Mutex.new

  mutex.synchronize do
    @container[event] = [] unless @container[event]
    @container[event] << klazz_name unless @container[event].include?(klazz_name)
  end
  true
end

#unsub(event, klazz_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/pub_sub/hash.rb', line 29

def unsub(event, klazz_name)
  mutex = Mutex.new

  mutex.synchronize do
    return false unless @container[event]

    @container[event].delete(klazz_name)
    true
  end
end