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

Allow subscriptions to be removed #455

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions lib/ferrum/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def on(event, &block)
@client.on(event_name(event), &block)
end

def off(event, id)
@client.off(event_name(event), id)
end

def subscribed?(event)
@client.subscribed?(event_name(event))
end
Expand Down Expand Up @@ -99,6 +103,10 @@ def on(event, &block)
@subscriber.on(event, &block)
end

def off(event, id)
@subscriber.off(event, id)
end

def subscribed?(event)
@subscriber.subscribed?(event)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/ferrum/client/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def <<(message)

def on(event, &block)
@on[event] << block
@on[event].index(block)
end

def off(event, id)
@on[event].delete_at(id)
true
end

Expand Down
13 changes: 13 additions & 0 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ def on(name, &block)
end
end

def off(name, id)
case name
when :dialog
client.off("Page.javascriptDialogOpening", id)
when :request
client.off("Fetch.requestPaused", id)
when :auth
client.off("Fetch.authRequired", id)
else
client.off(name, id)
end
end

def subscribed?(event)
client.subscribed?(event)
end
Expand Down
38 changes: 38 additions & 0 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,42 @@
expect(page.device_pixel_ratio).to eq(2)
end
end

describe "#on" do
it "subscribes to an event" do
message = nil

page.on("Runtime.consoleAPICalled") do |params|
message = params.dig("args", 0, "value")
end

page.evaluate("console.log('hello')")
expect(message).to eq("hello")
end
end

describe "#off" do
it "unsubscribes a specific event handler" do
message_a = nil
message_b = nil

a = page.on("Runtime.consoleAPICalled") do |params|
message_a = params.dig("args", 0, "value")
end

page.on("Runtime.consoleAPICalled") do |params|
message_b = params.dig("args", 0, "value")
end

page.evaluate("console.log('hello')")
expect(message_a).to eq("hello")
expect(message_b).to eq("hello")

page.off("Runtime.consoleAPICalled", a)
page.evaluate("console.log('goodbye')")

expect(message_a).to eq("hello")
expect(message_b).to eq("goodbye")
end
end
end