Skip to content

Commit

Permalink
Sleep a bit for stable handling message.
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeIwaki committed Jun 20, 2020
1 parent c4741eb commit 345a4bf
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/puppeteer/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def initialize(url, transport, delay = 0)

@transport = transport
@transport.on_message do |data|
async_handle_message(JSON.parse(data))
message = JSON.parse(data)
sleep_before_handling_message(message)
async_handle_message(message)
end
@transport.on_close do |reason, code|
handle_close(reason, code)
Expand All @@ -54,6 +56,16 @@ def initialize(url, transport, delay = 0)
@closed = false
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.')

# For some reasons, sleeping a bit reduces trivial errors...
# 4ms is an interval of internal shared timer of WebKit.
sleep 0.004
end

def self.from_session(session)
session.connection
end
Expand Down

0 comments on commit 345a4bf

Please sign in to comment.