Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve target manager init if no existing targets detected #257

Merged
merged 2 commits into from Sep 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 21 additions & 9 deletions lib/puppeteer/chrome_target_manager.rb
@@ -1,4 +1,5 @@
class Puppeteer::ChromeTargetManager
include Puppeteer::DebugPrint
include Puppeteer::EventCallbackable

def initialize(connection:, target_factory:, target_filter_callback:)
Expand Down Expand Up @@ -33,20 +34,34 @@ def initialize(connection:, target_factory:, target_filter_callback:)
)

setup_attachment_listeners(@connection)
@connection.async_send_message('Target.setDiscoverTargets', discover: true)
@connection.async_send_message('Target.setDiscoverTargets', {
discover: true,
filter: [
{ type: 'tab', exclude: true },
{},
],
}).then do
store_existing_targets_for_init
end.rescue do |err|
debug_puts(err)
end
end

def init
private def store_existing_targets_for_init
@discovered_targets_by_target_id.each do |target_id, target_info|
if @target_filter_callback.call(target_info)
if @target_filter_callback.call(target_info) && target_info.type != 'browser'
@target_ids_for_init << target_id
end
end
end

def init
@connection.send_message('Target.setAutoAttach', {
waitForDebuggerOnStart: true,
flatten: true,
autoAttach: true,
})
finish_initialization_if_ready
@initialize_promise.value!
end

Expand Down Expand Up @@ -219,10 +234,7 @@ class SessionNotCreatedError < StandardError ; end

@target_ids_for_init.delete(target.target_id)
future { emit_event(TargetManagerEmittedEvents::TargetAvailable, target) }

if @target_ids_for_init.empty?
@initialize_promise.fulfill(nil) unless @initialize_promise.resolved?
end
finish_initialization_if_ready

future do
# TODO: the browser might be shutting down here. What do we do with the error?
Expand All @@ -239,8 +251,8 @@ class SessionNotCreatedError < StandardError ; end
end
end

private def finish_initialization_if_ready(target_id)
@target_ids_for_init.delete(target_id)
private def finish_initialization_if_ready(target_id = nil)
@target_ids_for_init.delete(target_id) if target_id
if @target_ids_for_init.empty?
@initialize_promise.fulfill(nil) unless @initialize_promise.resolved?
end
Expand Down