Skip to content

Commit

Permalink
Merge pull request #257 from YusukeIwaki/porting/8748
Browse files Browse the repository at this point in the history
fix: resolve target manager init if no existing targets detected
  • Loading branch information
YusukeIwaki committed Sep 17, 2022
2 parents b65b7ba + 9f5f47e commit 01b4cff
Showing 1 changed file with 21 additions and 9 deletions.
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

0 comments on commit 01b4cff

Please sign in to comment.