Skip to content

Commit

Permalink
fix: Prevent 'Hash#[]=': can't add a new key into hash during iterati…
Browse files Browse the repository at this point in the history
…on (#513)
  • Loading branch information
route authored Jan 8, 2025
1 parent a9b9ae3 commit 4ede96c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions lib/ferrum/client/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +22,7 @@ def <<(message)
end

def on(event, &block)
@on[event] ||= Concurrent::Array.new
@on[event] << block
true
end
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4ede96c

Please sign in to comment.