Skip to content

Commit

Permalink
handle message sync for some significant events and method call results
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeIwaki committed Apr 2, 2021
1 parent 2c7f16b commit d67ad92
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions lib/puppeteer/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ def initialize(url, transport, delay = 0)
@transport = transport
@transport.on_message do |data|
message = JSON.parse(data)
sleep_before_handling_message(message)
async_handle_message(message)
if should_handle_synchronously?(message)
handle_message(message)
else
async_handle_message(message)
end
end
@transport.on_close do |reason, code|
handle_close
Expand All @@ -61,14 +64,27 @@ def closed?
@closed
end

private def sleep_before_handling_message(message)
# Puppeteer doesn't handle any Network monitoring responses.
# So we don't have to sleep.
return if message['method']&.start_with?('Network.')
private def should_handle_synchronously?(message)
return true if message['id']

# For some reasons, sleeping a bit reduces trivial errors...
# 4ms is an interval of internal shared timer of WebKit.
sleep 0.004
case message['method']
when nil
false
when /^Network\./
# Puppeteer doesn't handle any Network monitoring responses.
# So we don't care their handling order.
false
when /^Target\./
true
when /^Runtime\.executionContext/
true
when /^Page\.frame/
true
when 'Page.lifecycleEvent'
true
else
false
end
end

def self.from_session(session)
Expand Down

0 comments on commit d67ad92

Please sign in to comment.