Class: Puppeteer::Bidi::Connection
- Inherits:
-
Object
- Object
- Puppeteer::Bidi::Connection
- Defined in:
- lib/puppeteer/bidi/connection.rb,
sig/puppeteer/bidi/connection.rbs
Overview
Connection manages BiDi protocol communication Handles command sending, response waiting, and event dispatching
Defined Under Namespace
Classes: ProtocolError, TimeoutError
Constant Summary collapse
- DEFAULT_TIMEOUT =
: Integer -- 30 seconds in milliseconds
30_000
Instance Method Summary collapse
-
#async_send_command(method, params = {}, timeout: DEFAULT_TIMEOUT) ⇒ Async::Task[Hash[String, untyped]]
Send a BiDi command and wait for response.
-
#close ⇒ void
Close the connection.
- #closed? ⇒ Boolean
- #handle_event(message) ⇒ void
- #handle_message(message) ⇒ void
- #handle_response(message) ⇒ void
-
#initialize(transport) ⇒ Connection
constructor
A new instance of Connection.
- #next_id ⇒ Integer
-
#off(event, &block) ⇒ void
Unsubscribe from BiDi events.
-
#on(event) {|arg0| ... } ⇒ Connection
Subscribe to BiDi events.
- #setup_transport_handlers ⇒ void
Constructor Details
#initialize(transport) ⇒ Connection
Returns a new instance of Connection.
19 20 21 22 23 24 25 26 27 |
# File 'lib/puppeteer/bidi/connection.rb', line 19 def initialize(transport) @transport = transport @next_id = 1 @pending_commands = {} #: Hash[Integer, Hash[Symbol, untyped]] @event_listeners = {} #: Hash[String, Array[^(untyped) -> void]] @closed = false setup_transport_handlers end |
Instance Method Details
#async_send_command(method, params = {}, timeout: DEFAULT_TIMEOUT) ⇒ Async::Task[Hash[String, untyped]]
Send a BiDi command and wait for response
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/puppeteer/bidi/connection.rb', line 34 def async_send_command(method, params = {}, timeout: DEFAULT_TIMEOUT) raise ProtocolError, 'Connection is closed' if @closed id = next_id command = { id: id, method: method, params: params } # Create promise for this command promise = Async::Promise.new @pending_commands[id] = { promise: promise, method: method, sent_at: Time.now } # Debug output if ENV['DEBUG_BIDI_COMMAND'] puts "[BiDi] Request #{method}: #{command.inspect}" end Async do # Send command through transport @transport.(command).wait # Wait for response with timeout begin result = AsyncUtils.async_timeout(timeout, promise).wait # Debug output if ENV['DEBUG_BIDI_COMMAND'] puts "[BiDi] Response for #{method}: #{result.inspect}" end unless result.is_a?(Hash) && result.key?('type') raise ProtocolError, "Protocol Error. Message is not in BiDi protocol format: #{result.inspect}" end case result['type'] when 'success' result['result'] when 'error' # BiDi error format: { "type": "error", "error": "...", "message": "...", ... } error_type = result['error'] || 'unknown error' = result['message'] || error_type raise ProtocolError, "BiDi error (#{method}): #{error_message}" else raise ProtocolError, "Protocol Error. Unexpected BiDi message type: #{result['type'].inspect}" end rescue Async::TimeoutError raise TimeoutError, "Timeout waiting for #{method} (#{timeout}ms)" end ensure # A send that raises, an encoding failure for instance, would otherwise leave # the command and its promise pending for the life of the connection. @pending_commands.delete(id) end end |
#close ⇒ void
This method returns an undefined value.
Close the connection
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/puppeteer/bidi/connection.rb', line 123 def close return if @closed @closed = true # Reject all pending commands @pending_commands.each_value do |pending| pending[:promise].reject(ProtocolError.new('Connection closed')) end @pending_commands.clear @transport.close end |
#closed? ⇒ Boolean
138 139 140 |
# File 'lib/puppeteer/bidi/connection.rb', line 138 def closed? @closed end |
#handle_event(message) ⇒ void
This method returns an undefined value.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/puppeteer/bidi/connection.rb', line 193 def handle_event() method = ['method'] params = ['params'] || {} if ENV['DEBUG_BIDI_COMMAND'] puts "[BiDi] Event #{method}: #{params.inspect}" end listeners = @event_listeners[method] return unless listeners # Call all registered listeners for this event listeners.each do |listener| begin listener.call(params) rescue => e warn "Error in event listener for #{method}: #{e.message}" end end end |
#handle_message(message) ⇒ void
This method returns an undefined value.
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/puppeteer/bidi/connection.rb', line 164 def () # Response to a command (has 'id' field) if ['id'] handle_response() # Event (has 'method' but no 'id') elsif ['method'] handle_event() else warn "Unknown BiDi message format: #{message}" end end |
#handle_response(message) ⇒ void
This method returns an undefined value.
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/puppeteer/bidi/connection.rb', line 178 def handle_response() id = ['id'] pending = @pending_commands.delete(id) unless pending warn "Received response for unknown command id: #{id}" return end # Resolve the promise with the response pending[:promise].resolve() end |
#next_id ⇒ Integer
145 146 147 148 149 |
# File 'lib/puppeteer/bidi/connection.rb', line 145 def next_id id = @next_id @next_id += 1 id end |
#off(event, &block) ⇒ void
This method returns an undefined value.
Unsubscribe from BiDi events
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/puppeteer/bidi/connection.rb', line 110 def off(event, &block) return self unless @event_listeners[event] if block @event_listeners[event].delete(block) else @event_listeners.delete(event) end self end |
#on(event) {|arg0| ... } ⇒ Connection
Subscribe to BiDi events
100 101 102 103 104 |
# File 'lib/puppeteer/bidi/connection.rb', line 100 def on(event, &block) @event_listeners[event] ||= [] @event_listeners[event] << block self end |
#setup_transport_handlers ⇒ void
This method returns an undefined value.
152 153 154 155 156 157 158 159 160 |
# File 'lib/puppeteer/bidi/connection.rb', line 152 def setup_transport_handlers @transport. do || () end @transport.on_close do close end end |