Skip to content

Commit 5f7ecc3

Browse files
committed
Prevent RuntimeError: can't add a new key into hash during iteration. ruby-concurrency/concurrent-ruby#594
1 parent 46e3049 commit 5f7ecc3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/ferrum/context.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def dispose
7272
@contexts.dispose(@id)
7373
end
7474

75+
def has_target?(target_id)
76+
@targets.keys.include?(target_id)
77+
end
78+
7579
def inspect
7680
%(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>)
7781
end

lib/ferrum/contexts.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Contexts
77
attr_reader :contexts
88

99
def initialize(browser)
10-
@contexts = Concurrent::Hash.new
10+
@contexts = Concurrent::Map.new
1111
@browser = browser
1212
subscribe
1313
discover
@@ -18,7 +18,9 @@ def default_context
1818
end
1919

2020
def find_by(target_id:)
21-
@contexts.find { |_, c| c.targets.keys.include?(target_id) }&.last
21+
context = nil
22+
@contexts.each_value { |c| context = c if c.has_target?(target_id) }
23+
context
2224
end
2325

2426
def create

0 commit comments

Comments
 (0)