Class: Ferrum::Browser::Subscriber
- Inherits:
-
Object
- Object
- Ferrum::Browser::Subscriber
- Includes:
- Concurrent::Async
- Defined in:
- lib/ferrum/browser/subscriber.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(message) ⇒ Object
-
#initialize ⇒ Subscriber
constructor
A new instance of Subscriber.
- #on(event, &block) ⇒ Object
- #subscribed?(event) ⇒ Boolean
Constructor Details
#initialize ⇒ Subscriber
Returns a new instance of Subscriber.
14 15 16 17 |
# File 'lib/ferrum/browser/subscriber.rb', line 14 def initialize super @on = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Array.new } end |
Class Method Details
.build(size) ⇒ Object
10 11 12 |
# File 'lib/ferrum/browser/subscriber.rb', line 10 def self.build(size) (0..size).map { new } end |
Instance Method Details
#call(message) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/ferrum/browser/subscriber.rb', line 28 def call() method, params = .values_at("method", "params") total = @on[method].size @on[method].each_with_index do |block, index| # If there are a few callback we provide current index and total block.call(params, index, total) end end |
#on(event, &block) ⇒ Object
19 20 21 22 |
# File 'lib/ferrum/browser/subscriber.rb', line 19 def on(event, &block) @on[event] << block true end |
#subscribed?(event) ⇒ Boolean
24 25 26 |
# File 'lib/ferrum/browser/subscriber.rb', line 24 def subscribed?(event) @on.key?(event) end |