Class: RubyLLM::Chat::Subscription

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



15
16
17
18
19
20
21
# File 'lib/ruby_llm/chat.rb', line 15

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.



13
14
15
# File 'lib/ruby_llm/chat.rb', line 13

def tag
  @tag
end

Instance Method Details

#active? ⇒ Boolean

Checks if this subscription is still active.

Returns:

  • (Boolean) —

    true if still subscribed



37
38
39
40
41
# File 'lib/ruby_llm/chat.rb', line 37

def active?
  @monitor.synchronize do
    @active && @callback_list.include?(@callback)
  end
end

#inspect ⇒ Object



43
44
45
# File 'lib/ruby_llm/chat.rb', line 43

def inspect
  "#<#{self.class.name} tag=#{@tag.inspect} active=#{active?}>"
end

#unsubscribe ⇒ Boolean

Removes this subscription from the callback list.

Returns:

  • (Boolean) —

    true if successfully unsubscribed, false if already inactive



25
26
27
28
29
30
31
32
33
# File 'lib/ruby_llm/chat.rb', line 25

def unsubscribe # rubocop:disable Naming/PredicateMethod
  @monitor.synchronize do
    return false unless @active

    @callback_list.delete(@callback)
    @active = false
  end
  true
end