Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent 'Hash#[]=': can't add a new key into hash during iteration #513

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading