Skip to content

Commit

Permalink
Merge pull request #14 from andrewvy/feature/configure-pids
Browse files Browse the repository at this point in the history
Add ability to use `async: pid()` to specify consumer
  • Loading branch information
andrewvy authored Oct 1, 2017
2 parents 31767c2 + 560401a commit 4cd53e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
26 changes: 11 additions & 15 deletions lib/chrome_remote_interface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,25 @@ defmodule ChromeRemoteInterface do
#{arg_doc}
"""
def unquote(:"#{name}")(page_pid) do
page_pid |> PageSession.call(
page_pid |> PageSession.execute_command(
unquote("#{domain["domain"]}.#{name}"),
%{}
%{},
[]
)
end
def unquote(:"#{name}")(page_pid, parameters) do
page_pid |> PageSession.call(
page_pid |> PageSession.execute_command(
unquote("#{domain["domain"]}.#{name}"),
parameters
parameters,
[]
)
end
def unquote(:"#{name}")(page_pid, parameters, opts) when is_list(opts) do
if opts[:async] do
page_pid |> PageSession.cast(
unquote("#{domain["domain"]}.#{name}"),
parameters
)
else
page_pid |> PageSession.call(
unquote("#{domain["domain"]}.#{name}"),
parameters
)
end
page_pid |> PageSession.execute_command(
unquote("#{domain["domain"]}.#{name}"),
parameters,
opts
)
end
end
end
Expand Down
20 changes: 19 additions & 1 deletion lib/page_session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,30 @@ defmodule ChromeRemoteInterface.PageSession do
end

@doc """
Unscribes to all events.
Unsubcribes to all events.
"""
def unsubscribe_all(pid, subscriber_pid \\ self()) do
GenServer.call(pid, {:unsubscribe_all, subscriber_pid})
end

@doc """
Executes an RPC command with the given options.
Options:
`:async` -
If a boolean, sends the response as a message to the current process.
Else, if provided with a PID, it will send the response to that process instead.
"""
def execute_command(pid, method, params, opts) do
async = Keyword.get(opts, :async, false)

case async do
false -> call(pid, method, params)
true -> cast(pid, method, params, self())
from when is_pid(from) -> cast(pid, method, params, from)
end
end

@doc """
Executes a raw JSON RPC command through Websockets.
"""
Expand Down

0 comments on commit 4cd53e7

Please sign in to comment.