Class: Datadog::Event
- Inherits:
-
Object
- Object
- Datadog::Event
- Defined in:
- lib/ddtrace/event.rb
Overview
A simple pub-sub event model for components to exchange messages through.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
Instance Method Summary collapse
-
#initialize(name) ⇒ Event
constructor
A new instance of Event.
- #publish(*args) ⇒ Object
- #subscribe(key, &block) ⇒ Object
- #unsubscribe(key) ⇒ Object
- #unsubscribe_all! ⇒ Object
Constructor Details
#initialize(name) ⇒ Event
Returns a new instance of Event.
10 11 12 13 14 |
# File 'lib/ddtrace/event.rb', line 10 def initialize(name) @name = name @subscriptions = {} @mutex = Mutex.new end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ddtrace/event.rb', line 6 def name @name end |
#subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
6 7 8 |
# File 'lib/ddtrace/event.rb', line 6 def subscriptions @subscriptions end |
Instance Method Details
#publish(*args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ddtrace/event.rb', line 38 def publish(*args) @mutex.synchronize do subscriptions.each do |key, block| begin block.call(*args) rescue StandardError => e Datadog.logger.debug("Error while handling '#{key}' for '#{name}' event: #{e.}") end end true end end |
#subscribe(key, &block) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/ddtrace/event.rb', line 16 def subscribe(key, &block) raise ArgumentError, 'Must give a block to subscribe!' unless block @mutex.synchronize do subscriptions[key] = block end end |
#unsubscribe(key) ⇒ Object
24 25 26 27 28 |
# File 'lib/ddtrace/event.rb', line 24 def unsubscribe(key) @mutex.synchronize do subscriptions.delete(key) end end |
#unsubscribe_all! ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/ddtrace/event.rb', line 30 def unsubscribe_all! @mutex.synchronize do subscriptions.clear end true end |