Class: Ollama::Events
- Inherits:
-
Object
- Object
- Ollama::Events
- Defined in:
- lib/ollama/events.rb
Overview
Event system for the Ollama client. Provides pub/sub for request lifecycle events.
Instance Method Summary collapse
-
#clear(event) ⇒ Integer
Clear all subscribers for an event.
-
#clear_all ⇒ Object
Clear all subscribers for all events.
-
#initialize ⇒ Events
constructor
A new instance of Events.
-
#publish(event, payload = {}) ⇒ Array
Publish an event to all subscribers.
-
#subscribe(event, &block) ⇒ Proc
Subscribe to an event.
-
#unsubscribe(event, block) ⇒ Boolean
Unsubscribe from an event.
Constructor Details
#initialize ⇒ Events
Returns a new instance of Events.
7 8 9 |
# File 'lib/ollama/events.rb', line 7 def initialize @subscribers = Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#clear(event) ⇒ Integer
Clear all subscribers for an event
44 45 46 47 48 |
# File 'lib/ollama/events.rb', line 44 def clear(event) count = @subscribers[event].size @subscribers[event].clear count end |
#clear_all ⇒ Object
Clear all subscribers for all events
51 52 53 |
# File 'lib/ollama/events.rb', line 51 def clear_all @subscribers.clear end |
#publish(event, payload = {}) ⇒ Array
Publish an event to all subscribers
32 33 34 35 36 37 38 39 |
# File 'lib/ollama/events.rb', line 32 def publish(event, payload = {}) @subscribers[event].map do |subscriber| subscriber.call(payload) rescue StandardError => e warn "[Ollama::Events] Subscriber error for #{event}: #{e.}" nil end end |
#subscribe(event, &block) ⇒ Proc
Subscribe to an event
15 16 17 18 |
# File 'lib/ollama/events.rb', line 15 def subscribe(event, &block) @subscribers[event] << block block end |
#unsubscribe(event, block) ⇒ Boolean
Unsubscribe from an event
24 25 26 |
# File 'lib/ollama/events.rb', line 24 def unsubscribe(event, block) @subscribers[event].delete(block) end |