Class: Ferrum::Client::Subscriber

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

Constant Summary collapse

INTERRUPTIONS =
%w[Fetch.requestPaused Fetch.authRequired].freeze

Instance Method Summary collapse

Constructor Details

#initializeSubscriber

Returns a new instance of Subscriber.



8
9
10
11
12
13
14
# File 'lib/ferrum/client/subscriber.rb', line 8

def initialize
  @regular = Queue.new
  @priority = Queue.new
  @on = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Array.new }

  start
end

Instance Method Details

#<<(message) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/ferrum/client/subscriber.rb', line 16

def <<(message)
  if INTERRUPTIONS.include?(message["method"])
    @priority.push(message)
  else
    @regular.push(message)
  end
end

#clear(session_id:) ⇒ Object



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

def clear(session_id:)
  @on.delete_if { |k, _| k.match?(session_id) }
end

#closeObject



33
34
35
36
# File 'lib/ferrum/client/subscriber.rb', line 33

def close
  @regular_thread&.kill
  @priority_thread&.kill
end

#on(event, &block) ⇒ Object



24
25
26
27
# File 'lib/ferrum/client/subscriber.rb', line 24

def on(event, &block)
  @on[event] << block
  true
end

#subscribed?(event) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ferrum/client/subscriber.rb', line 29

def subscribed?(event)
  @on.key?(event)
end