Class: SwarmSDK::Agent::ChatHelpers::EventEmitter::Subscription
- Inherits:
-
Object
- Object
- SwarmSDK::Agent::ChatHelpers::EventEmitter::Subscription
- Defined in:
- lib/swarm_sdk/agent/chat_helpers/event_emitter.rb
Overview
Represents an active subscription to a callback event. Returned by #subscribe and can be used to unsubscribe later.
Instance Attribute Summary collapse
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Checks if this subscription is still active.
-
#initialize(callback_list, callback, monitor:, tag: nil) ⇒ Subscription
constructor
A new instance of Subscription.
- #inspect ⇒ Object
-
#unsubscribe ⇒ Boolean
Removes this subscription from the callback list.
Constructor Details
#initialize(callback_list, callback, monitor:, tag: nil) ⇒ Subscription
Returns a new instance of Subscription.
18 19 20 21 22 23 24 |
# File 'lib/swarm_sdk/agent/chat_helpers/event_emitter.rb', line 18 def initialize(callback_list, callback, monitor:, tag: nil) @callback_list = callback_list @callback = callback @monitor = monitor @tag = tag @active = true end |
Instance Attribute Details
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
16 17 18 |
# File 'lib/swarm_sdk/agent/chat_helpers/event_emitter.rb', line 16 def tag @tag end |
Instance Method Details
#active? ⇒ Boolean
Checks if this subscription is still active.
40 41 42 43 44 |
# File 'lib/swarm_sdk/agent/chat_helpers/event_emitter.rb', line 40 def active? @monitor.synchronize do @active && @callback_list.include?(@callback) end end |
#inspect ⇒ Object
46 47 48 |
# File 'lib/swarm_sdk/agent/chat_helpers/event_emitter.rb', line 46 def inspect "#<#{self.class.name} tag=#{@tag.inspect} active=#{active?}>" end |
#unsubscribe ⇒ Boolean
Removes this subscription from the callback list.
28 29 30 31 32 33 34 35 36 |
# File 'lib/swarm_sdk/agent/chat_helpers/event_emitter.rb', line 28 def unsubscribe # rubocop:disable Naming/PredicateMethod @monitor.synchronize do return false unless @active @callback_list.delete(@callback) @active = false end true end |