Class: ToggleCraft::SharedSSEConnection
- Inherits:
-
Object
- Object
- ToggleCraft::SharedSSEConnection
- Defined in:
- lib/togglecraft/shared_sse_connection.rb
Overview
Shared SSE connection wrapper Manages a single SSE connection that can be shared by multiple SDK clients
Instance Attribute Summary collapse
-
#pool_key ⇒ Object
readonly
Returns the value of attribute pool_key.
Instance Method Summary collapse
-
#add_client(client) ⇒ void
Add a client to this shared connection.
-
#client_count ⇒ Integer
Get the number of connected clients.
-
#connect(client_to_add = nil) ⇒ void
Connect to the SSE server.
-
#connected? ⇒ Boolean
Check if connected.
-
#disconnect ⇒ void
Disconnect from the SSE server.
-
#force_disconnect ⇒ void
Force disconnect (used by pool cleanup).
-
#initialize(config, pool_key) ⇒ SharedSSEConnection
constructor
A new instance of SharedSSEConnection.
-
#on_empty(&callback) ⇒ void
Set callback for when connection becomes empty.
-
#reconnect ⇒ void
Reconnect to the SSE server.
-
#remove_client(client) ⇒ void
Remove a client from this shared connection.
-
#set_debug(enabled) ⇒ void
Enable or disable debug logging.
Constructor Details
#initialize(config, pool_key) ⇒ SharedSSEConnection
Returns a new instance of SharedSSEConnection.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 9 def initialize(config, pool_key) @config = config @pool_key = pool_key @clients = Concurrent::Set.new @connection = nil @last_payload = nil @is_connecting = Concurrent::AtomicBoolean.new(false) @connection_promise = nil @debug = config[:debug] || false @on_empty_callback = nil @mutex = Mutex.new end |
Instance Attribute Details
#pool_key ⇒ Object (readonly)
Returns the value of attribute pool_key.
7 8 9 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 7 def pool_key @pool_key end |
Instance Method Details
#add_client(client) ⇒ void
This method returns an undefined value.
Add a client to this shared connection
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 25 def add_client(client) log "Adding client to shared connection (#{@clients.size + 1} total clients)" @clients.add(client) # If connection is already established, simulate initialization for new client return unless connected? && @last_payload log 'Connection already established - simulating initialization for new client' # Simulate connection event client.handle_connect if client.respond_to?(:handle_connect) # Send cached flags client.handle_flags_update(@last_payload) if client.respond_to?(:handle_flags_update) end |
#client_count ⇒ Integer
Get the number of connected clients
125 126 127 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 125 def client_count @clients.size end |
#connect(client_to_add = nil) ⇒ void
This method returns an undefined value.
Connect to the SSE server
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 59 def connect(client_to_add = nil) # If already connecting, wait for completion if @is_connecting.value log 'Connection already in progress, waiting...' # Just add the client, connection will complete add_client(client_to_add) if client_to_add return end # If already connected, just add the client if connected? log 'Already connected' add_client(client_to_add) if client_to_add return end @is_connecting.make_true begin create_connection add_client(client_to_add) if client_to_add ensure @is_connecting.make_false end end |
#connected? ⇒ Boolean
Check if connected
119 120 121 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 119 def connected? @connection&.connected? end |
#disconnect ⇒ void
This method returns an undefined value.
Disconnect from the SSE server
87 88 89 90 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 87 def disconnect log 'Disconnect called on shared connection' @connection&.disconnect end |
#force_disconnect ⇒ void
This method returns an undefined value.
Force disconnect (used by pool cleanup)
94 95 96 97 98 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 94 def force_disconnect log 'Force disconnecting shared connection' @connection&.disconnect @clients.clear end |
#on_empty(&callback) ⇒ void
This method returns an undefined value.
Set callback for when connection becomes empty
132 133 134 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 132 def on_empty(&callback) @on_empty_callback = callback end |
#reconnect ⇒ void
This method returns an undefined value.
Reconnect to the SSE server
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 102 def reconnect log 'Reconnecting shared connection' # Notify all clients about reconnection @clients.each do |client| client.emit(:reconnecting) if client.respond_to?(:emit) end if @connection @connection.reconnect else connect end end |
#remove_client(client) ⇒ void
This method returns an undefined value.
Remove a client from this shared connection
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 44 def remove_client(client) log "Removing client from shared connection (#{@clients.size - 1} remaining)" @clients.delete(client) # If no more clients, clean up the connection return unless @clients.empty? log 'No more clients, closing shared connection' cleanup @on_empty_callback&.call end |
#set_debug(enabled) ⇒ void
This method returns an undefined value.
Enable or disable debug logging
139 140 141 142 143 144 145 |
# File 'lib/togglecraft/shared_sse_connection.rb', line 139 def set_debug(enabled) @debug = enabled return unless @connection @connection.instance_variable_set(:@options, @connection.instance_variable_get(:@options).merge(debug: enabled)) end |