Class: SiteMaps::Notification::Bus
- Inherits:
-
Object
- Object
- SiteMaps::Notification::Bus
- Defined in:
- lib/site_maps/notification/bus.rb
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#listeners ⇒ Object
readonly
Returns the value of attribute listeners.
Instance Method Summary collapse
- #attach(listener) ⇒ Object
-
#can_handle?(object_or_event_id) ⇒ Boolean
rubocop:enable Performance/RedundantEqualityComparisonBlock.
-
#initialize ⇒ Bus
constructor
A new instance of Bus.
- #publish(event_id, payload) ⇒ Object
- #subscribe(object_or_event_id, &block) ⇒ Object
-
#subscribed?(listener) ⇒ Boolean
rubocop:disable Performance/RedundantEqualityComparisonBlock.
- #unsubscribe(listener) ⇒ Object (also: #detach)
Constructor Details
#initialize ⇒ Bus
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
#events ⇒ Object (readonly)
Returns the value of attribute events.
6 7 8 |
# File 'lib/site_maps/notification/bus.rb', line 6 def events @events end |
#listeners ⇒ Object (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
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
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 |