Class: Capybara::Lightpanda::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/lightpanda/client.rb,
lib/capybara/lightpanda/client/subscriber.rb,
lib/capybara/lightpanda/client/web_socket.rb

Defined Under Namespace

Classes: Subscriber, WebSocket

Instance Method Summary collapse

Constructor Details

#initialize(ws_url, options) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
# File 'lib/capybara/lightpanda/client.rb', line 12

def initialize(ws_url, options)
  @options = options
  @ws = WebSocket.new(ws_url, options)
  @command_id = 0
  @pendings = Concurrent::Hash.new
  @subscriber = Subscriber.new
  @mutex = Mutex.new

  start_message_thread
end

Instance Method Details

#clear_subscriptionsObject

Drop all event subscriptions without closing the WebSocket. Used by Browser#recreate_page so a fresh target’s event handlers don’t pile up on top of the previous target’s subscriptions.



63
64
65
# File 'lib/capybara/lightpanda/client.rb', line 63

def clear_subscriptions
  @subscriber.clear
end

#closeObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/capybara/lightpanda/client.rb', line 67

def close
  @ws&.close
  @message_thread&.join(1) || @message_thread&.kill
  @subscriber.clear
  # Wake any in-flight callers blocked on `pending.value!(timeout)` so
  # they raise DeadBrowserError immediately (via the `@ws.closed?` check
  # in #command) instead of stalling for the full @options.timeout — a
  # 30s freeze per pending command on a dying browser.
  fail_pending_commands
  @pendings.clear
end

#closed?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/capybara/lightpanda/client.rb', line 79

def closed?
  @ws.closed?
end

#command(method, params = {}, async: false, session_id: nil, timeout: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capybara/lightpanda/client.rb', line 23

def command(method, params = {}, async: false, session_id: nil, timeout: nil)
  message = build_message(method, params, session_id: session_id)

  if async
    @ws.send_message(JSON.generate(message))
    return true
  end

  pending = Concurrent::IVar.new
  @pendings[message[:id]] = pending

  @ws.send_message(JSON.generate(message))

  effective_timeout = timeout || @options.timeout
  response = pending.value!(effective_timeout)

  if response.nil?
    raise DeadBrowserError, "Browser closed during #{method}" if @ws.closed?

    raise TimeoutError, "Command #{method} timed out after #{effective_timeout}s"
  end

  handle_error(response) if response["error"]

  response["result"]
ensure
  @pendings.delete(message[:id]) if message
end

#off(event, block = nil) ⇒ Object



56
57
58
# File 'lib/capybara/lightpanda/client.rb', line 56

def off(event, block = nil)
  @subscriber.unsubscribe(event, block)
end

#on(event) ⇒ Object



52
53
54
# File 'lib/capybara/lightpanda/client.rb', line 52

def on(event, &)
  @subscriber.subscribe(event, &)
end