Class: SwarmSDK::Agent::ChatHelpers::EventEmitter::Subscription

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

Instance Method Summary collapse

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

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

Returns:

  • (Boolean)

    true if still subscribed



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

#inspectObject



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

#unsubscribeBoolean

Removes this subscription from the callback list.

Returns:

  • (Boolean)

    true if successfully unsubscribed, false if already inactive



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