Class: Playwright::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/playwright/connection.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/playwright/connection.rb', line 8

def initialize(transport)
  @transport = transport
  @transport.on_message_received do |message|
    dispatch(message)
  end
  @transport.on_driver_crashed do
    @callbacks.each_value do |callback|
      callback.reject(::Playwright::DriverCrashedError.new)
    end
    raise ::Playwright::DriverCrashedError.new
  end

  @objects = {} # Hash[ guid => ChannelOwner ]
  @waiting_for_object = {} # Hash[ guid => Promise<ChannelOwner> ]
  @callbacks = {} # Hash [ guid => Promise<ChannelOwner> ]
  @root_object = RootChannelOwner.new(self)
end

Instance Method Details

#async_runObject



26
27
28
# File 'lib/playwright/connection.rb', line 26

def async_run
  @transport.async_run
end

#async_send_message_to_server(guid, method, params, metadata: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/playwright/connection.rb', line 39

def async_send_message_to_server(guid, method, params, metadata: nil)
  callback = Concurrent::Promises.resolvable_future

  with_generated_id do |id|
    # register callback promise object first.
    # @see https://github.com/YusukeIwaki/puppeteer-ruby/pull/34
    @callbacks[id] = callback

    message = {
      id: id,
      guid: guid,
      method: method,
      params: replace_channels_with_guids(params),
      metadata:  || {},
    }

    begin
      @transport.send_message(message)
    rescue => err
      @callbacks.delete(id)
      callback.reject(err)
      raise unless err.is_a?(Transport::AlreadyDisconnectedError)
    end
  end

  callback
end

#initialize_playwrightObject



34
35
36
37
# File 'lib/playwright/connection.rb', line 34

def initialize_playwright
  result = send_message_to_server('', 'initialize', { sdkLanguage: 'ruby' })
  ChannelOwners::Playwright.from(result['playwright'])
end

#send_message_to_server(guid, method, params, metadata: nil) ⇒ Object



67
68
69
# File 'lib/playwright/connection.rb', line 67

def send_message_to_server(guid, method, params, metadata: nil)
  async_send_message_to_server(guid, method, params, metadata: ).value!
end

#stopObject



30
31
32
# File 'lib/playwright/connection.rb', line 30

def stop
  @transport.stop
end