Class: Ferrum::Worker
- Inherits:
-
Object
- Object
- Ferrum::Worker
- Includes:
- Frame::Runtime, Interceptable
- Defined in:
- lib/ferrum/worker.rb,
sig/ferrum/worker.rbs
Overview
A dedicated or shared Worker spawned by a page. Unlike Page it has no DOM, frames, mouse/keyboard, or navigation history -- just a single global execution context and its own network activity.
Constant Summary
Constants included from Frame::Runtime
Frame::Runtime::DEEP_SERIALIZATION, Frame::Runtime::INTERMITTENT_ATTEMPTS, Frame::Runtime::INTERMITTENT_SLEEP, Frame::Runtime::SHALLOW_SERIALIZATION, Frame::Runtime::UNSERIALIZABLE_NUMBERS
Instance Attribute Summary collapse
-
#client ⇒ Client, SessionClient
readonly
Client connection.
-
#console ⇒ Console
readonly
Console messages.
-
#network ⇒ Network
readonly
Network object.
-
#target_id ⇒ String
readonly
Returns the value of attribute target_id.
-
#type ⇒ String
readonly
Returns the value of attribute type.
-
#url ⇒ String
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#close ⇒ nil
Closes the worker in the browser.
-
#command(method, timeout: nil, **params) ⇒ Hash
Sends a CDP command through #client.
-
#initialize(client, target_id:, url:, type: "worker") ⇒ Worker
constructor
A new instance of Worker.
-
#inspect ⇒ String
Debug representation of the worker.
-
#main_frame ⇒ nil
private
Workers have a single execution context and no navigable document, so there's nothing for Network to check requests against.
-
#release_waiters ⇒ void
private
Wakes the threads waiting for the worker's execution context for good, the worker is gone and no context is coming.
-
#timeout ⇒ Numeric
How long to wait for CDP responses and JS evaluation to complete.
Methods included from Interceptable
#off, #on, #subscribe_interception, #subscribed?
Methods included from Frame::Runtime
#evaluate, #evaluate_handle, #evaluate_in, #execute
Constructor Details
#initialize(client, target_id:, url:, type: "worker") ⇒ Worker
Returns a new instance of Worker.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ferrum/worker.rb', line 22 def initialize(client, target_id:, url:, type: "worker") @client = client @target_id = target_id @url = url @type = type @options = client. @page = self @execution_id = Concurrency::Cell.new @network = Network.new(self) @console = Console.new(self) subscribe prepare end |
Instance Attribute Details
#client ⇒ Client, SessionClient (readonly)
Client connection.
18 19 20 |
# File 'lib/ferrum/worker.rb', line 18 def client @client end |
#console ⇒ Console (readonly)
Console messages.
45 46 47 |
# File 'lib/ferrum/worker.rb', line 45 def console @console end |
#network ⇒ Network (readonly)
Network object.
40 41 42 |
# File 'lib/ferrum/worker.rb', line 40 def network @network end |
#target_id ⇒ String (readonly)
Returns the value of attribute target_id.
20 21 22 |
# File 'lib/ferrum/worker.rb', line 20 def target_id @target_id end |
#type ⇒ String (readonly)
Returns the value of attribute type.
20 21 22 |
# File 'lib/ferrum/worker.rb', line 20 def type @type end |
#url ⇒ String (readonly)
Returns the value of attribute url.
20 21 22 |
# File 'lib/ferrum/worker.rb', line 20 def url @url end |
Instance Method Details
#close ⇒ nil
Closes the worker in the browser. Chrome refuses
Target.closeTarget for a dedicated worker, so it closes itself instead.
87 88 89 90 91 92 93 94 95 |
# File 'lib/ferrum/worker.rb', line 87 def close if type == "worker" client.notify("Runtime.evaluate", expression: "self.close()") else client.notify("Target.closeTarget", targetId: target_id) end nil end |
#command(method, timeout: nil, **params) ⇒ Hash
Sends a CDP command through #client.
69 70 71 |
# File 'lib/ferrum/worker.rb', line 69 def command(method, timeout: nil, **params) client.command(method, timeout: timeout || self.timeout, **params) end |
#inspect ⇒ String
Debug representation of the worker.
110 111 112 |
# File 'lib/ferrum/worker.rb', line 110 def inspect "#<#{self.class} @target_id=#{@target_id.inspect} @url=#{@url.inspect}>" end |
#main_frame ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Workers have a single execution context and no navigable document, so there's nothing for Network to check requests against.
79 80 81 |
# File 'lib/ferrum/worker.rb', line 79 def main_frame nil end |
#release_waiters ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Wakes the threads waiting for the worker's execution context for good, the worker is gone and no context is coming.
103 104 105 |
# File 'lib/ferrum/worker.rb', line 103 def release_waiters @execution_id.close end |
#timeout ⇒ Numeric
How long to wait for CDP responses and JS evaluation to complete.
50 51 52 |
# File 'lib/ferrum/worker.rb', line 50 def timeout @options.timeout end |