Class: Esse::Events::Bus Private
- Inherits:
-
Object
- Object
- Esse::Events::Bus
- Defined in:
- lib/esse/events/bus.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Event bus
An event bus stores listeners (callbacks) and events
Instance Attribute Summary collapse
-
#events ⇒ Hash
readonly
private
A hash with events registered within a bus.
-
#listeners ⇒ Hash
readonly
private
A hash with event listeners registered within a bus.
Instance Method Summary collapse
- #attach(listener) ⇒ Object private
- #can_handle?(object_or_event_id) ⇒ Boolean private
- #detach(listener) ⇒ Object private
-
#initialize(events: {}, listeners: Hash.new { |h, k| h[k] = [] }) ⇒ Bus
constructor
private
Initialize a new event bus.
- #publish(event_id, payload) ⇒ Object private
- #subscribe(event_id, &block) ⇒ Object private
- #subscribed?(listener) ⇒ Boolean private
Constructor Details
#initialize(events: {}, listeners: Hash.new { |h, k| h[k] = [] }) ⇒ Bus
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new event bus
33 34 35 36 |
# File 'lib/esse/events/bus.rb', line 33 def initialize(events: {}, listeners: Hash.new { |h, k| h[k] = [] }) @listeners = listeners @events = events end |
Instance Attribute Details
#events ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns A hash with events registered within a bus.
12 13 14 |
# File 'lib/esse/events/bus.rb', line 12 def events @events end |
#listeners ⇒ Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns A hash with event listeners registered within a bus.
15 16 17 |
# File 'lib/esse/events/bus.rb', line 15 def listeners @listeners end |
Instance Method Details
#attach(listener) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
46 47 48 49 50 51 52 53 |
# File 'lib/esse/events/bus.rb', line 46 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 end |
#can_handle?(object_or_event_id) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/esse/events/bus.rb', line 79 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?(&object_or_event_id.method(:respond_to?)) end end |
#detach(listener) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
56 57 58 59 60 61 62 |
# File 'lib/esse/events/bus.rb', line 56 def detach(listener) listeners.each do |id, arr| arr.each do |func| listeners[id].delete(func) if func.receiver == listener end end end |
#publish(event_id, payload) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
39 40 41 42 43 |
# File 'lib/esse/events/bus.rb', line 39 def publish(event_id, payload) process(event_id, payload) do |event, listener| listener.call(event) end end |
#subscribe(event_id, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
65 66 67 68 |
# File 'lib/esse/events/bus.rb', line 65 def subscribe(event_id, &block) listeners[event_id] << block self end |
#subscribed?(listener) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 74 75 76 |
# File 'lib/esse/events/bus.rb', line 71 def subscribed?(listener) listeners.values.any? { |value| value.any? { |func| func == listener } } || ( methods = events.values.map(&:listener_method).select(&listener.method(:respond_to?)).map(&listener.method(:method)) methods && listeners.values.any? { |value| (methods & value).size > 0 } ) end |