diff --git a/CHANGELOG.md b/CHANGELOG.md index 8661f324..7a72bc73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixed - Correctly set mouse events buttons property [#509] +- Prevent 'Hash#[]=': can't add a new key into hash during iteration [#513] ### Removed diff --git a/lib/ferrum/client/subscriber.rb b/lib/ferrum/client/subscriber.rb index 74b8887c..ed720b57 100644 --- a/lib/ferrum/client/subscriber.rb +++ b/lib/ferrum/client/subscriber.rb @@ -8,7 +8,7 @@ class Subscriber def initialize @regular = Queue.new @priority = Queue.new - @on = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Array.new } + @on = Concurrent::Hash.new start end @@ -22,6 +22,7 @@ def <<(message) end def on(event, &block) + @on[event] ||= Concurrent::Array.new @on[event] << block true end @@ -65,8 +66,8 @@ def call(message) method, session_id, params = message.values_at("method", "sessionId", "params") event = SessionClient.event_name(method, session_id) - total = @on[event].size - @on[event].each_with_index do |block, index| + total = @on[event]&.size.to_i + @on[event]&.each_with_index do |block, index| # In case of multiple callbacks we provide current index and total block.call(params, index, total) end