Skip to content

Commit 4298b67

Browse files
committed
Use mvar for execution_id
1 parent 58cc201 commit 4298b67

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

lib/ferrum/frame.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class Frame
2020
def initialize(id, page, parent_id = nil)
2121
@id = id
2222
@page = page
23-
@execution_id = nil
2423
@parent_id = parent_id
24+
@execution_id = Concurrent::MVar.new
2525
end
2626

2727
def state=(value)
@@ -52,25 +52,19 @@ def content=(html)
5252
end
5353
alias set_content content=
5454

55-
def execution_id?(execution_id)
56-
@execution_id == execution_id
57-
end
58-
5955
def execution_id
60-
raise NoExecutionContextError unless @execution_id
61-
62-
@execution_id
63-
rescue NoExecutionContextError
64-
@page.event.reset
65-
@page.event.wait(@page.timeout) ? retry : raise
66-
end
56+
value = @execution_id.borrow(@page.timeout, &:itself)
57+
raise NoExecutionContextError if value.instance_of?(Object)
6758

68-
def set_execution_id(value)
69-
@execution_id ||= value
59+
value
7060
end
7161

72-
def reset_execution_id
73-
@execution_id = nil
62+
def execution_id=(value)
63+
if value.nil?
64+
@execution_id.try_take!
65+
else
66+
@execution_id.try_put!(value)
67+
end
7468
end
7569

7670
def inspect

lib/ferrum/page/frames.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ def frames
1111
@frames.values
1212
end
1313

14-
def frame_by(id: nil, name: nil)
14+
def frame_by(id: nil, name: nil, execution_id: nil)
1515
if id
1616
@frames[id]
1717
elsif name
1818
frames.find { |f| f.name == name }
19+
elsif execution_id
20+
frames.find { |f| f.execution_id == execution_id }
1921
else
2022
raise ArgumentError
2123
end
@@ -69,37 +71,32 @@ def frames_subscribe
6971
end
7072

7173
on("Runtime.executionContextCreated") do |params|
72-
setting_up_main_frame = false
7374
context_id = params.dig("context", "id")
7475
frame_id = params.dig("context", "auxData", "frameId")
7576

7677
unless @main_frame.id
7778
root_frame = command("Page.getFrameTree").dig("frameTree", "frame", "id")
7879
if frame_id == root_frame
79-
setting_up_main_frame = true
8080
@main_frame.id = frame_id
8181
@frames[frame_id] = @main_frame
8282
end
8383
end
8484

8585
frame = @frames[frame_id] || Frame.new(frame_id, self)
86-
frame.set_execution_id(context_id)
87-
88-
# Set event because `execution_id` might raise NoExecutionContextError
89-
@event.set if setting_up_main_frame
86+
frame.execution_id = context_id
9087

9188
@frames[frame_id] ||= frame
9289
end
9390

9491
on("Runtime.executionContextDestroyed") do |params|
9592
execution_id = params["executionContextId"]
96-
frame = frames.find { |f| f.execution_id?(execution_id) }
97-
frame.reset_execution_id
93+
frame = frame_by(execution_id: execution_id)
94+
frame&.execution_id = nil
9895
end
9996

10097
on("Runtime.executionContextsCleared") do
10198
@frames.delete_if { |_, f| !f.main? }
102-
@main_frame.reset_execution_id
99+
@main_frame.execution_id = nil
103100
end
104101
end
105102

0 commit comments

Comments
 (0)