Skip to content

Commit

Permalink
Prevent RuntimeError: can't add a new key into hash during iteration. r…
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Feb 25, 2021
1 parent 46e3049 commit 5f7ecc3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/ferrum/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def dispose
@contexts.dispose(@id)
end

def has_target?(target_id)
@targets.keys.include?(target_id)
end

def inspect
%(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/ferrum/contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Contexts
attr_reader :contexts

def initialize(browser)
@contexts = Concurrent::Hash.new
@contexts = Concurrent::Map.new
@browser = browser
subscribe
discover
Expand All @@ -18,7 +18,9 @@ def default_context
end

def find_by(target_id:)
@contexts.find { |_, c| c.targets.keys.include?(target_id) }&.last
context = nil
@contexts.each_value { |c| context = c if c.has_target?(target_id) }
context
end

def create
Expand Down

0 comments on commit 5f7ecc3

Please sign in to comment.