Class: Puppeteer::CDPSession
- Inherits:
-
Object
- Object
- Puppeteer::CDPSession
- Includes:
- DebugPrint, EventCallbackable
- Defined in:
- lib/puppeteer/cdp_session.rb,
sig/puppeteer/cdp_session.rbs
Overview
rbs_inline: enabled
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#connection ⇒ Puppeteer::Connection?
readonly
: Puppeteer::Connection?.
-
#parent_session ⇒ Puppeteer::CDPSession?
readonly
: Puppeteer::CDPSession?.
-
#target ⇒ Puppeteer::Target?
: Puppeteer::Target?.
Instance Method Summary collapse
- #async_send_message(method, params = {}) ⇒ Async::Promise[Hash[String, untyped]]
- #callback?(id) ⇒ Boolean
- #detach ⇒ void
- #handle_closed ⇒ void
- #handle_message(message) ⇒ void
- #id ⇒ String
-
#initialize(connection, target_type, session_id, parent_session: nil) ⇒ CDPSession
constructor
A new instance of CDPSession.
- #mark_ready ⇒ void
- #on(event_name, &block) ⇒ void
- #once(event_name, &block) ⇒ void
- #send_message(method, params = {}) ⇒ Hash[String, untyped]
- #wait_for_ready ⇒ Boolean
Methods included from EventCallbackable
#add_event_listener, #emit_event, #observe_first, #off, #on_event, #remove_event_listener
Methods included from DebugPrint
Constructor Details
#initialize(connection, target_type, session_id, parent_session: nil) ⇒ CDPSession
Returns a new instance of CDPSession.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/puppeteer/cdp_session.rb', line 15 def initialize(connection, target_type, session_id, parent_session: nil) @callbacks = {} @callbacks_mutex = Mutex.new @connection = connection @target_type = target_type @session_id = session_id @parent_session = parent_session @ready_promise = Async::Promise.new @target = nil end |
Instance Attribute Details
#connection ⇒ Puppeteer::Connection? (readonly)
: Puppeteer::Connection?
31 32 33 |
# File 'lib/puppeteer/cdp_session.rb', line 31 def connection @connection end |
#parent_session ⇒ Puppeteer::CDPSession? (readonly)
: Puppeteer::CDPSession?
32 33 34 |
# File 'lib/puppeteer/cdp_session.rb', line 32 def parent_session @parent_session end |
#target ⇒ Puppeteer::Target?
: Puppeteer::Target?
33 34 35 |
# File 'lib/puppeteer/cdp_session.rb', line 33 def target @target end |
Instance Method Details
#async_send_message(method, params = {}) ⇒ Async::Promise[Hash[String, untyped]]
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/puppeteer/cdp_session.rb', line 67 def (method, params = {}) if !@connection raise Puppeteer::TargetCloseError.new( method: method, error_message: "Session closed. Most likely the #{@target_type} has been closed.", ) end @connection.ensure_command_allowed!(method) promise = Async::Promise.new @connection.generate_id do |id| @callbacks_mutex.synchronize do @callbacks[id] = Puppeteer::Connection::MessageCallback.new(method: method, promise: promise) end @connection.raw_send(id: id, message: { sessionId: @session_id, method: method, params: params }) end promise end |
#callback?(id) ⇒ Boolean
91 92 93 |
# File 'lib/puppeteer/cdp_session.rb', line 91 def callback?(id) @callbacks_mutex.synchronize { @callbacks.key?(id) } end |
#detach ⇒ void
This method returns an undefined value.
132 133 134 135 136 137 |
# File 'lib/puppeteer/cdp_session.rb', line 132 def detach if !@connection raise Error.new("Session already detarched. Most likely the #{@target_type} has been closed.") end @connection.('Target.detachFromTarget', sessionId: @session_id) end |
#handle_closed ⇒ void
This method returns an undefined value.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/puppeteer/cdp_session.rb', line 140 def handle_closed callbacks = @callbacks_mutex.synchronize do @callbacks.values.tap { @callbacks.clear } end callbacks.each do |callback| callback.reject( Puppeteer::TargetCloseError.new( method: callback.method, error_message: 'Target closed')) end @ready_promise.reject(Error.new("Session closed")) unless @ready_promise.resolved? @connection = nil emit_event(CDPSessionEmittedEvents::Disconnected) end |
#handle_message(message) ⇒ void
This method returns an undefined value.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/puppeteer/cdp_session.rb', line 97 def () if ['id'] if callback = @callbacks_mutex.synchronize { @callbacks.delete(['id']) } (callback, ) else raise Error.new("unknown id: #{['id']}") end else emit_event(['method'], ['params']) end end |
#id ⇒ String
27 28 29 |
# File 'lib/puppeteer/cdp_session.rb', line 27 def id @session_id end |
#mark_ready ⇒ void
This method returns an undefined value.
36 37 38 |
# File 'lib/puppeteer/cdp_session.rb', line 36 def mark_ready @ready_promise.resolve(true) unless @ready_promise.resolved? end |
#on(event_name, &block) ⇒ void
This method returns an undefined value.
158 159 160 |
# File 'lib/puppeteer/cdp_session.rb', line 158 def on(event_name, &block) add_event_listener(event_name, &block) end |
#once(event_name, &block) ⇒ void
This method returns an undefined value.
165 166 167 |
# File 'lib/puppeteer/cdp_session.rb', line 165 def once(event_name, &block) observe_first(event_name, &block) end |
#send_message(method, params = {}) ⇒ Hash[String, untyped]
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/puppeteer/cdp_session.rb', line 48 def (method, params = {}) protocol_timeout = @connection&.protocol_timeout if protocol_timeout && protocol_timeout > 0 begin Puppeteer::AsyncUtils.async_timeout(protocol_timeout, (method, params)).wait rescue Async::TimeoutError raise Puppeteer::Connection::ProtocolError.new( method: method, error_message: "Timeout #{protocol_timeout}ms exceeded", ) end else (method, params).wait end end |
#wait_for_ready ⇒ Boolean
41 42 43 |
# File 'lib/puppeteer/cdp_session.rb', line 41 def wait_for_ready @ready_promise.wait end |