Class: SiteMaps::Notification::Bus

Inherits:
Object
  • Object
show all
Defined in:
lib/site_maps/notification/bus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBus

Returns a new instance of Bus.



8
9
10
11
# File 'lib/site_maps/notification/bus.rb', line 8

def initialize
  @listeners = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Array.new }
  @events = Concurrent::Hash.new
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



6
7
8
# File 'lib/site_maps/notification/bus.rb', line 6

def events
  @events
end

#listenersObject (readonly)

Returns the value of attribute listeners.



6
7
8
# File 'lib/site_maps/notification/bus.rb', line 6

def listeners
  @listeners
end

Instance Method Details

#attach(listener) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/site_maps/notification/bus.rb', line 23

def attach(listener)
  events.each do |id, event|
    method_name = event.listener_method
    next unless listener.respond_to?(method_name)

    listeners[id] << listener.method(method_name)
  end
  self
end

#can_handle?(object_or_event_id) ⇒ Boolean

rubocop:enable Performance/RedundantEqualityComparisonBlock

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/site_maps/notification/bus.rb', line 67

def can_handle?(object_or_event_id)
  case object_or_event_id
  when String, Symbol
    events.key?(object_or_event_id)
  else
    events
      .values
      .map(&:listener_method)
      .any? { |method_name| object_or_event_id.respond_to?(method_name) }
  end
end

#publish(event_id, payload) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/site_maps/notification/bus.rb', line 13

def publish(event_id, payload)
  raise UnregisteredEventError, event_id unless can_handle?(event_id)

  process(event_id, payload) do |event, listener|
    # Concurrent::Future.execute { listener.call(event) }
    listener.call(event)
  end
  self
end

#subscribe(object_or_event_id, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/site_maps/notification/bus.rb', line 43

def subscribe(object_or_event_id, &block)
  raise(InvalidSubscriberError, object_or_event_id) unless can_handle?(object_or_event_id)

  if block
    listeners[object_or_event_id] << block
  else
    attach(object_or_event_id)
  end

  self
end

#subscribed?(listener) ⇒ Boolean

rubocop:disable Performance/RedundantEqualityComparisonBlock

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
# File 'lib/site_maps/notification/bus.rb', line 56

def subscribed?(listener)
  listeners.values.any? { |value| value.any? { |func| func == listener } } ||
    (
      methods = events.values.map(&:listener_method)
        .select { |method_name| listener.respond_to?(method_name) }
        .map { |method_name| listener.method(method_name) }
      methods && listeners.values.any? { |value| (methods & value).size > 0 }
    )
end

#unsubscribe(listener) ⇒ Object Also known as: detach



33
34
35
36
37
38
39
40
# File 'lib/site_maps/notification/bus.rb', line 33

def unsubscribe(listener)
  listeners.each do |id, arr|
    arr.each do |func|
      listeners[id].delete(func) if func.receiver == listener
    end
  end
  self
end