lib/puppeteer/browser.rb in puppeteer-ruby-0.0.22 vs lib/puppeteer/browser.rb in puppeteer-ruby-0.0.23

- old
+ new

@@ -44,41 +44,34 @@ @contexts = {} context_ids.each do |context_id| @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] def process @process @@ -135,12 +128,12 @@ screenshot_task_queue: @screenshot_task_queue, ) # 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 # @param {{targetId: string}} event def handle_target_destroyed(event) @@ -148,12 +141,12 @@ target = @targets[target_id] target.ignore_initialize_callback_promise @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 class TargetNotExistError < StandardError def initialize @@ -167,12 +160,12 @@ target = @targets[target_info.target_id] or raise TargetNotExistError.new previous_url = target.url 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 # @return [String] def ws_endpoint @@ -220,15 +213,15 @@ existing_target = targets.find { |target| predicate.call(target) } return existing_target if existing_target 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 end