Class: Esse::Events::Bus Private

Inherits:
Object
  • Object
show all
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

API:

  • private

Instance Attribute Summary collapse

Instance Method Summary collapse

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

See Also:

Parameters:

  • (defaults to: {})

    A hash with events

  • (defaults to: Hash.new { |h, k| h[k] = [] })

    A hash with listeners

API:

  • private



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

#eventsHash (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.

Returns:

  • A hash with events registered within a bus

API:

  • private



12
13
14
# File 'lib/esse/events/bus.rb', line 12

def events
  @events
end

#listenersHash (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.

Returns:

  • A hash with event listeners registered within a bus

API:

  • private



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.

API:

  • private



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.

Returns:

API:

  • private



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.

API:

  • private



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.

API:

  • private



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.

API:

  • private



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.

Returns:

API:

  • private



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