Class: Dry::Events::Bus Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/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

Instance Method Summary collapse

Constructor Details

#initialize(events: EMPTY_HASH, listeners: LISTENERS_HASH.dup) ⇒ 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

Parameters:

  • events (Hash) (defaults to: EMPTY_HASH)

    A hash with events

  • listeners (Hash) (defaults to: LISTENERS_HASH.dup)

    A hash with listeners



27
28
29
30
# File 'lib/dry/events/bus.rb', line 27

def initialize(events: EMPTY_HASH, listeners: LISTENERS_HASH.dup)
  @listeners = listeners
  @events = events
end

Instance Attribute Details

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



15
16
17
# File 'lib/dry/events/bus.rb', line 15

def events
  @events
end

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



19
20
21
# File 'lib/dry/events/bus.rb', line 19

def listeners
  @listeners
end

Instance Method Details

#attach(listener, filter) ⇒ 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.



51
52
53
54
55
56
57
58
59
# File 'lib/dry/events/bus.rb', line 51

def attach(listener, filter)
  events.each do |id, event|
    meth = event.listener_method

    if listener.respond_to?(meth)
      listeners[id] << [listener.method(meth), filter]
    end
  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:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dry/events/bus.rb', line 90

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.



62
63
64
65
66
67
68
69
# File 'lib/dry/events/bus.rb', line 62

def detach(listener)
  listeners.each do |id, memo|
    memo.each do |tuple|
      current_listener, _ = tuple
      listeners[id].delete(tuple) if current_listener.receiver.equal?(listener)
    end
  end
end

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



33
34
35
36
37
38
39
40
41
# File 'lib/dry/events/bus.rb', line 33

def process(event_id, payload)
  listeners[event_id].each do |listener, filter|
    event = events[event_id].payload(payload)

    if filter.(payload)
      yield(event, 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.



44
45
46
47
48
# File 'lib/dry/events/bus.rb', line 44

def publish(event_id, payload)
  process(event_id, payload) do |event, listener|
    listener.(event)
  end
end

#subscribe(event_id, filter, &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.



72
73
74
75
# File 'lib/dry/events/bus.rb', line 72

def subscribe(event_id, filter, &block)
  listeners[event_id] << [block, filter]
  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:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
# File 'lib/dry/events/bus.rb', line 78

def subscribed?(listener)
  listeners.values.any? do |value|
    value.any? do |block, _|
      case listener
      when Proc   then block.equal?(listener)
      when Method then listener.owner == block.owner && listener.name == block.name
      end
    end
  end
end