Skip to content

Commit 4ede96c

Browse files
authored
fix: Prevent 'Hash#[]=': can't add a new key into hash during iteration (#513)
1 parent a9b9ae3 commit 4ede96c

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixed
1010

1111
- Correctly set mouse events buttons property [#509]
12+
- Prevent 'Hash#[]=': can't add a new key into hash during iteration [#513]
1213

1314
### Removed
1415

lib/ferrum/client/subscriber.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Subscriber
88
def initialize
99
@regular = Queue.new
1010
@priority = Queue.new
11-
@on = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Array.new }
11+
@on = Concurrent::Hash.new
1212

1313
start
1414
end
@@ -22,6 +22,7 @@ def <<(message)
2222
end
2323

2424
def on(event, &block)
25+
@on[event] ||= Concurrent::Array.new
2526
@on[event] << block
2627
true
2728
end
@@ -65,8 +66,8 @@ def call(message)
6566
method, session_id, params = message.values_at("method", "sessionId", "params")
6667
event = SessionClient.event_name(method, session_id)
6768

68-
total = @on[event].size
69-
@on[event].each_with_index do |block, index|
69+
total = @on[event]&.size.to_i
70+
@on[event]&.each_with_index do |block, index|
7071
# In case of multiple callbacks we provide current index and total
7172
block.call(params, index, total)
7273
end

0 commit comments

Comments
 (0)