Class: Puppeteer::EmulationManager

Inherits:
Object
  • Object
show all
Includes:
DebugPrint
Defined in:
lib/puppeteer/emulation_manager.rb

Instance Method Summary collapse

Methods included from DebugPrint

#debug_print, #debug_puts

Constructor Details

#initialize(client) ⇒ EmulationManager

Returns a new instance of EmulationManager.

Parameters:



6
7
8
9
10
11
12
13
14
# File 'lib/puppeteer/emulation_manager.rb', line 6

def initialize(client)
  @client = client
  @emulating_mobile = false
  @has_touch = false
  @viewport = nil
  @locale = nil
  @locale_configured = false
  @secondary_clients = Set.new
end

Instance Method Details

#emulate_locale(locale) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppeteer/emulation_manager.rb', line 93

def emulate_locale(locale)
  @locale = locale
  @locale_configured = true
  clients = [@client, *@secondary_clients]
  promises = clients.map do |client|
    client.async_send_message(
      'Emulation.setLocaleOverride',
      { locale: locale }.compact,
    )
  end
  Puppeteer::AsyncUtils.await_promise_all(*promises)
end

#emulate_viewport(viewport) ⇒ true|false

Parameters:

Returns:

  • (true|false)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppeteer/emulation_manager.rb', line 45

def emulate_viewport(viewport)
  mobile = viewport.mobile?
  has_touch = viewport.has_touch?

  apply_viewport(@client, viewport)
  @viewport = viewport

  reload_needed = @emulating_mobile != mobile || @has_touch != has_touch
  @emulating_mobile = mobile
  @has_touch = has_touch
  reload_needed
end

#register_speculative_session(client) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppeteer/emulation_manager.rb', line 21

def register_speculative_session(client)
  @secondary_clients << client
  client.once(CDPSessionEmittedEvents::Disconnected) do
    @secondary_clients.delete(client)
  end
  promises = []
  promises.concat(viewport_promises(client, @viewport)) if @viewport
  if @locale_configured
    promises << client.async_send_message(
      'Emulation.setLocaleOverride',
      { locale: @locale }.compact,
    )
  end
  return if promises.empty?

  Async do
    Puppeteer::AsyncUtils.await_promise_all(*promises)
  rescue => err
    debug_puts(err)
  end
end

#update_client(client) ⇒ Object



16
17
18
19
# File 'lib/puppeteer/emulation_manager.rb', line 16

def update_client(client)
  @client = client
  @secondary_clients.delete(client)
end