Class: Selenium::WebDriver::BiDi::Dispatcher Private

Inherits:
Object
  • Object
show all
Defined in:
rb/lib/selenium/webdriver/bidi/dispatcher.rb,
rb/lib/selenium/webdriver/bidi/dispatcher/listener.rb,
rb/lib/selenium/webdriver/bidi/dispatcher/ordered_listener.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Holds the event listeners of one BiDi connection and invokes each with the typed payloads of its event. Listening is local: nothing here sends a command, so the caller subscribes with Protocol::Session#subscribe itself.

Defined Under Namespace

Classes: Listener, OrderedListener

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Dispatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Dispatcher.



32
33
34
35
36
37
# File 'rb/lib/selenium/webdriver/bidi/dispatcher.rb', line 32

def initialize
  @listeners = {}
  @mutex = Mutex.new
  @last_id = 0
  @closed = false
end

Instance Method Details

#add_listener(event, payload_type, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'rb/lib/selenium/webdriver/bidi/dispatcher.rb', line 39

def add_listener(event, payload_type, &block)
  raise ArgumentError, 'a block is required to listen for an event' unless block

  @mutex.synchronize do
    raise Error::WebDriverError, 'The BiDi connection is closed' if @closed

    id = "#{event}##{@last_id += 1}"
    listener_class = ENV.fetch('SE_ORDERED_EVENTS', nil) == 'true' ? OrderedListener : Listener
    @listeners[id] = listener_class.new(event, payload_type, block)
    id
  end
end

#close ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
76
# File 'rb/lib/selenium/webdriver/bidi/dispatcher.rb', line 70

def close
  listeners = @mutex.synchronize do
    @closed = true
    @listeners.values.tap { @listeners.clear }
  end
  listeners.each(&:deactivate)
end

#receive(event, params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs on the socket thread, which also delivers command replies, so it never waits.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'rb/lib/selenium/webdriver/bidi/dispatcher.rb', line 57

def receive(event, params)
  listeners = @mutex.synchronize { @listeners.values.select { |listener| listener.event == event } }
  return if listeners.empty?

  payload_type = listeners.first.payload_type
  begin
    payload = payload_type ? payload_type.from_json(params) : params
  rescue Error::SerializationError => e
    return WebDriver.logger.error("Dropped a #{event} payload that failed validation: #{e.message}", id: :bidi)
  end
  listeners.each { |listener| listener.dispatch(payload) }
end

#remove_listener(id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
# File 'rb/lib/selenium/webdriver/bidi/dispatcher.rb', line 52

def remove_listener(id)
  @mutex.synchronize { @listeners.delete(id) }&.deactivate
end