Skip to content

Commit

Permalink
Merge pull request #31 from YusukeIwaki/refactor_event
Browse files Browse the repository at this point in the history
Refactor event
  • Loading branch information
YusukeIwaki authored Nov 24, 2020
2 parents 11d5e5b + dfef2a6 commit 2ed3b25
Show file tree
Hide file tree
Showing 20 changed files with 370 additions and 179 deletions.
37 changes: 37 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ Style/DefWithParentheses:
Style/MethodDefParentheses:
Enabled: true

Style/MethodCallWithArgsParentheses:
Enabled: true
IgnoredMethods:
# Gemfile, gemspec
- source
- add_dependency
- add_development_dependency

# include/require
- require
- require_relative
- include

# fundamental methods
- raise
- sleep

# RSpec
- describe
- it
- to
- not_to
- be
- eq

# async/await
- define_async_method
- await
- future

# utils
- debug_print
- debug_puts

Style/MethodCallWithoutArgsParentheses:
Enabled: true

Style/RedundantFreeze:
Enabled: true

Expand Down
1 change: 1 addition & 0 deletions lib/puppeteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Puppeteer; end

# Custom data types.
require 'puppeteer/device'
require 'puppeteer/events'
require 'puppeteer/errors'
require 'puppeteer/viewport'

Expand Down
45 changes: 19 additions & 26 deletions lib/puppeteer/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,30 @@ def initialize(connection:, context_ids:, ignore_https_errors:, default_viewport
@contexts[context_id] = Puppeteer::BrowserContext.new(@connection, self, context_id)
end
@targets = {}
@connection.on_event 'Events.Connection.Disconnected' do
emit_event 'Events.Browser.Disconnected'
@connection.on_event(ConnectionEmittedEvents::Disconnected) do
emit_event(BrowserEmittedEvents::Disconnected)
end
@connection.on_event 'Target.targetCreated', &method(:handle_target_created)
@connection.on_event 'Target.targetDestroyed', &method(:handle_target_destroyed)
@connection.on_event 'Target.targetInfoChanged', &method(:handle_target_info_changed)
@connection.on_event('Target.targetCreated', &method(:handle_target_created))
@connection.on_event('Target.targetDestroyed', &method(:handle_target_destroyed))
@connection.on_event('Target.targetInfoChanged', &method(:handle_target_info_changed))
end

EVENT_MAPPINGS = {
disconnected: 'Events.Browser.Disconnected',
targetcreated: 'Events.Browser.TargetCreated',
targetchanged: 'Events.Browser.TargetChanged',
targetdestroyed: 'Events.Browser.TargetDestroyed',
}

# @param event_name [Symbol] either of :disconnected, :targetcreated, :targetchanged, :targetdestroyed
def on(event_name, &block)
unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
unless BrowserEmittedEvents.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserEmittedEvents.values.to_a.join(", ")}")
end

add_event_listener(EVENT_MAPPINGS[event_name.to_sym], &block)
super(event_name.to_s, &block)
end

# @param event_name [Symbol]
def once(event_name, &block)
unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
unless BrowserEmittedEvents.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserEmittedEvents.values.to_a.join(", ")}")
end

observe_first(EVENT_MAPPINGS[event_name.to_sym], &block)
super(event_name.to_s, &block)
end

# @return [Puppeteer::BrowserRunner::BrowserProcess]
Expand Down Expand Up @@ -137,8 +130,8 @@ def handle_target_created(event)
# assert(!this._targets.has(event.targetInfo.targetId), 'Target should not exist before targetCreated');
@targets[target_info.target_id] = target
if await target.initialized_promise
emit_event 'Events.Browser.TargetCreated', target
context.emit_event 'Events.BrowserContext.TargetCreated', target
emit_event(BrowserEmittedEvents::TargetCreated, target)
context.emit_event(BrowserContextEmittedEvents::TargetCreated, target)
end
end

Expand All @@ -150,8 +143,8 @@ def handle_target_destroyed(event)
@targets.delete(target_id)
target.closed_callback
if await target.initialized_promise
emit_event 'Events.Browser.TargetDestroyed', target
target.browser_context.emit_event 'Events.BrowserContext.TargetDestroyed', target
emit_event(BrowserEmittedEvents::TargetDestroyed, target)
target.browser_context.emit_event(BrowserContextEmittedEvents::TargetDestroyed, target)
end
end

Expand All @@ -169,8 +162,8 @@ def handle_target_info_changed(event)
was_initialized = target.initialized?
target.handle_target_info_changed(target_info)
if was_initialized && previous_url != target.url
emit_event 'Events.Browser.TargetChanged', target
target.browser_context.emit_event 'Events.BrowserContext.TargetChanged', target
emit_event(BrowserEmittedEvents::TargetChanged, target)
target.browser_context.emit_event(BrowserContextEmittedEvents::TargetChanged, target)
end
end

Expand Down Expand Up @@ -222,12 +215,12 @@ def wait_for_target(predicate:, timeout: nil)

event_listening_ids = []
target_promise = resolvable_future
event_listening_ids << add_event_listener('Events.Browser.TargetCreated') do |target|
event_listening_ids << add_event_listener(BrowserEmittedEvents::TargetCreated) do |target|
if predicate.call(target)
target_promise.fulfill(target)
end
end
event_listening_ids << add_event_listener('Events.Browser.TargetChanged') do |target|
event_listening_ids << add_event_listener(BrowserEmittedEvents::TargetChanged) do |target|
if predicate.call(target)
target_promise.fulfill(target)
end
Expand Down
19 changes: 6 additions & 13 deletions lib/puppeteer/browser_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,22 @@ def initialize(connection, browser, context_id)
@id = context_id
end

EVENT_MAPPINGS = {
disconnected: 'Events.BrowserContext.Disconnected',
targetcreated: 'Events.BrowserContext.TargetCreated',
targetchanged: 'Events.BrowserContext.TargetChanged',
targetdestroyed: 'Events.BrowserContext.TargetDestroyed',
}

# @param event_name [Symbol] either of :disconnected, :targetcreated, :targetchanged, :targetdestroyed
def on(event_name, &block)
unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
unless BrowserContextEmittedEvents.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserContextEmittedEvents.values.to_a.join(", ")}")
end

add_event_listener(EVENT_MAPPINGS[event_name.to_sym], &block)
super(event_name.to_s, &block)
end

# @param event_name [Symbol]
def once(event_name, &block)
unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
unless BrowserContextEmittedEvents.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserContextEmittedEvents.values.to_a.join(", ")}")
end

observe_first(EVENT_MAPPINGS[event_name.to_sym], &block)
super(event_name.to_s, &block)
end

# @return {!Array<!Target>} target
Expand Down
4 changes: 2 additions & 2 deletions lib/puppeteer/cdp_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def handle_message(message)
raise Error.new("unknown id: #{id}")
end
else
emit_event message['method'], message['params']
emit_event(message['method'], message['params'])
end
end

Expand Down Expand Up @@ -79,7 +79,7 @@ def handle_closed
end
@callbacks.clear
@connection = nil
emit_event 'Events.CDPSession.Disconnected'
emit_event(CDPSessionEmittedEvents::Disconnected)
end

# @param event_name [String]
Expand Down
4 changes: 2 additions & 2 deletions lib/puppeteer/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def handle_message(message)
end
end
else
emit_event message['method'], message['params']
emit_event(message['method'], message['params'])
end
end

Expand All @@ -233,7 +233,7 @@ def handle_message(message)
session.handle_closed
end
@sessions.clear
emit_event 'Events.Connection.Disconnected'
emit_event(ConnectionEmittedEvents::Disconnected)
end

def on_close(&block)
Expand Down
2 changes: 1 addition & 1 deletion lib/puppeteer/dom_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def SS(selector)

# @return [String]
def content
evaluate <<-JAVASCRIPT
evaluate(<<-JAVASCRIPT)
() => {
let retVal = '';
if (document.doctype)
Expand Down
4 changes: 4 additions & 0 deletions lib/puppeteer/event_callbackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def add_event_listener(event_name, &block)
(@event_listeners[event_name] ||= EventListeners.new).add(&block)
end

alias_method :on, :add_event_listener

def remove_event_listener(*id_args)
(@event_listeners ||= {}).each do |event_name, listeners|
id_args.each do |id|
Expand All @@ -50,6 +52,8 @@ def observe_first(event_name, &block)
end
end

alias_method :once, :observe_first

def on_event(event_name, &block)
@event_callbackable_handlers ||= {}
@event_callbackable_handlers[event_name] = block
Expand Down
Loading

0 comments on commit 2ed3b25

Please sign in to comment.