Skip to content

Commit

Permalink
define on and once in EventCallbackable
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeIwaki committed Nov 24, 2020
1 parent c1eb572 commit 8f2f9e5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 52 deletions.
19 changes: 6 additions & 13 deletions lib/puppeteer/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,22 @@ def initialize(connection:, context_ids:, ignore_https_errors:, default_viewport
@connection.on_event('Target.targetInfoChanged', &method(:handle_target_info_changed))
end

EVENT_MAPPINGS = {
disconnected: BrowserEmittedEvents::Disconnected,
targetcreated: BrowserEmittedEvents::TargetCreated,
targetchanged: BrowserEmittedEvents::TargetChanged,
targetdestroyed: BrowserEmittedEvents::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 Events::Browser.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{Events::Browser.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 Events::Browser.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{Events::Browser.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
18 changes: 6 additions & 12 deletions lib/puppeteer/browser_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,22 @@ def initialize(connection, browser, context_id)
@id = context_id
end

EVENT_MAPPINGS = {
targetcreated: BrowserContextEmittedEvents::TargetCreated,
targetchanged: BrowserContextEmittedEvents::TargetChanged,
targetdestroyed: BrowserContextEmittedEvents::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 Events::BrowserContext.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{Events::BrowserContext.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 Events::BrowserContext.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{Events::BrowserContext.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: 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
4 changes: 4 additions & 0 deletions lib/puppeteer/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def define_const_into(target_module)
target_module.const_set(key, value)
target_module.define_singleton_method(key) { value }
end
keyset = Set.new(self.keys)
valueset = Set.new(self.values)
target_module.define_singleton_method(:keys) { keyset }
target_module.define_singleton_method(:values) { valueset }
end
end
end
Expand Down
33 changes: 6 additions & 27 deletions lib/puppeteer/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,43 +128,22 @@ def init
)
end

EVENT_MAPPINGS = {
close: Events::Page::Close,
# console: Events::Page::Console,
dialog: Events::Page::Dialog,
domcontentloaded: Events::Page::DOMContentLoaded,
# error:
frameattached: Events::Page::FrameAttached,
framedetached: Events::Page::FrameDetached,
framenavigated: Events::Page::FrameNavigated,
load: Events::Page::Load,
# metrics: Events::Page::Metrics,
# pageerror: Events::Page::PageError,
popup: Events::Page::Popup,
request: Events::Page::Request,
requestfailed: Events::Page::RequestFailed,
requestfinished: Events::Page::RequestFinished,
response: Events::Page::Response,
# workercreated: Events::Page::WorkerCreated,
# workerdestroyed: Events::Page::WorkerDestroyed,
}

# @param event_name [Symbol]
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 Events::Page.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{Events::Page.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 Events::Page.values.include?(event_name.to_s)
raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{Events::Page.values.to_a.join(", ")}")
end

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

def handle_file_chooser(event)
Expand Down

0 comments on commit 8f2f9e5

Please sign in to comment.