Class: Unmagic::Browser::Driver::Connection
- Inherits:
-
Object
- Object
- Unmagic::Browser::Driver::Connection
- Defined in:
- lib/unmagic/browser/driver/connection.rb
Overview
One live browser, and the driver's promise to shut it down properly.
Every Session owns exactly one of these — that's what "a kept session holds a real browser, and on Cloudflare one of a limited number of concurrent slots, until you close it" means in code.
Instance Attribute Summary collapse
-
#browser ⇒ Puppeteer::Browser
readonly
The live browser.
Instance Method Summary collapse
-
#close ⇒ void
Shut the browser down the way its driver wants: killed, disconnected from, or told to close so a remote slot is freed immediately.
-
#closed? ⇒ Boolean
Whether this connection has been closed.
-
#initialize(driver, browser) ⇒ Connection
constructor
A new instance of Connection.
-
#new_page ⇒ Puppeteer::Page
A page to drive.
Constructor Details
#initialize(driver, browser) ⇒ Connection
Returns a new instance of Connection.
14 15 16 17 18 19 |
# File 'lib/unmagic/browser/driver/connection.rb', line 14 def initialize(driver, browser) @driver = driver @browser = browser @closed = false @mutex = Mutex.new end |
Instance Attribute Details
#browser ⇒ Puppeteer::Browser (readonly)
Returns the live browser.
22 23 24 |
# File 'lib/unmagic/browser/driver/connection.rb', line 22 def browser @browser end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Shut the browser down the way its driver wants: killed, disconnected from, or told to close so a remote slot is freed immediately.
41 42 43 44 45 46 47 48 |
# File 'lib/unmagic/browser/driver/connection.rb', line 41 def close @mutex.synchronize do return if @closed @closed = true @driver.send(:disconnect_browser, @browser) end end |
#closed? ⇒ Boolean
Returns whether this connection has been closed.
51 52 53 |
# File 'lib/unmagic/browser/driver/connection.rb', line 51 def closed? @closed end |
#new_page ⇒ Puppeteer::Page
A page to drive.
A freshly started browser already has one blank tab open, and opening a second costs most of a second — so the first caller gets the one that's already there.
31 32 33 34 35 |
# File 'lib/unmagic/browser/driver/connection.rb', line 31 def new_page raise Error::SessionClosed, "This browser connection is closed." if closed? Failures.wrap("opening a page") { claim_initial_page || @browser.new_page } end |