Class: Unmagic::Browser::Driver::Connection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(driver, browser) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • driver (Base)

    the driver that opened this browser

  • browser (Puppeteer::Browser)

    the connected browser



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

#browserPuppeteer::Browser (readonly)

Returns the live browser.

Returns:

  • (Puppeteer::Browser)

    the live browser



22
23
24
# File 'lib/unmagic/browser/driver/connection.rb', line 22

def browser
  @browser
end

Instance Method Details

#closevoid

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.

Returns:

  • (Boolean)

    whether this connection has been closed



51
52
53
# File 'lib/unmagic/browser/driver/connection.rb', line 51

def closed?
  @closed
end

#new_pagePuppeteer::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.

Returns:

  • (Puppeteer::Page)

Raises:



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