Class: Capybara::Lightpanda::Client::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/lightpanda/client/subscriber.rb

Instance Method Summary collapse

Constructor Details

#initializeSubscriber

Returns a new instance of Subscriber.



7
8
9
10
# File 'lib/capybara/lightpanda/client/subscriber.rb', line 7

def initialize
  @subscriptions = Hash.new { |h, k| h[k] = [] }
  @mutex = Mutex.new
end

Instance Method Details

#clearObject



38
39
40
# File 'lib/capybara/lightpanda/client/subscriber.rb', line 38

def clear
  @mutex.synchronize { @subscriptions.clear }
end

#dispatch(event, params) ⇒ Object



28
29
30
31
32
# File 'lib/capybara/lightpanda/client/subscriber.rb', line 28

def dispatch(event, params)
  callbacks = @mutex.synchronize { @subscriptions[event].dup }

  callbacks.each { |callback| callback.call(params) }
end

#subscribe(event, &block) ⇒ Object



12
13
14
15
16
# File 'lib/capybara/lightpanda/client/subscriber.rb', line 12

def subscribe(event, &block)
  @mutex.synchronize do
    @subscriptions[event] << block
  end
end

#subscribed?(event) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/capybara/lightpanda/client/subscriber.rb', line 34

def subscribed?(event)
  @mutex.synchronize { @subscriptions.key?(event) && @subscriptions[event].any? }
end

#unsubscribe(event, block = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/capybara/lightpanda/client/subscriber.rb', line 18

def unsubscribe(event, block = nil)
  @mutex.synchronize do
    if block
      @subscriptions[event].delete(block)
    else
      @subscriptions.delete(event)
    end
  end
end