Class: Ferrum::Browser::Client
- Inherits:
-
Object
- Object
- Ferrum::Browser::Client
- Defined in:
- lib/ferrum/browser/client.rb
Instance Method Summary collapse
- #close ⇒ Object
- #command(method, params = {}) ⇒ Object
-
#initialize(browser, ws_url, start_id = 0, allow_slowmo = true) ⇒ Client
constructor
A new instance of Client.
- #on(event, &block) ⇒ Object
Constructor Details
#initialize(browser, ws_url, start_id = 0, allow_slowmo = true) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ferrum/browser/client.rb', line 10 def initialize(browser, ws_url, start_id = 0, allow_slowmo = true) @command_id = start_id @pendings = Concurrent::Hash.new @browser = browser @slowmo = @browser.slowmo if allow_slowmo && @browser.slowmo > 0 @ws = WebSocket.new(ws_url, @browser.logger) @subscriber = Subscriber.new @thread = Thread.new do Thread.current.abort_on_exception = true Thread.current.report_on_exception = true if Thread.current.respond_to?(:report_on_exception=) while = @ws..pop if .key?("method") @subscriber.async.call() else @pendings[["id"]]&.set() end end end end |
Instance Method Details
#close ⇒ Object
52 53 54 55 56 57 |
# File 'lib/ferrum/browser/client.rb', line 52 def close @ws.close # Give a thread some time to handle a tail of messages @pendings.clear @thread.kill unless @thread.join(1) end |
#command(method, params = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ferrum/browser/client.rb', line 32 def command(method, params = {}) pending = Concurrent::IVar.new = (method, params) @pendings[[:id]] = pending sleep(@slowmo) if @slowmo @ws.() data = pending.value!(@browser.timeout) @pendings.delete([:id]) raise DeadBrowserError if data.nil? && @ws..closed? raise TimeoutError unless data error, response = data.values_at("error", "result") raise_browser_error(error) if error response end |
#on(event, &block) ⇒ Object
48 49 50 |
# File 'lib/ferrum/browser/client.rb', line 48 def on(event, &block) @subscriber.on(event, &block) end |