Skip to content

Commit

Permalink
🐛 Remove callbacks when finished
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvy committed Oct 1, 2017
1 parent c17a6b4 commit 31767c2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/page_session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,19 @@ defmodule ChromeRemoteInterface.PageSession do
method = json["method"]

# Message is an RPC response
if id do
send_rpc_response(state.callbacks, id, json)
end
callbacks =
if id do
send_rpc_response(state.callbacks, id, json)
else
state.callbacks
end

# Message is an Domain event
if method do
send_event(state.event_subscribers, method, json)
end

{:noreply, state}
{:noreply, %{state | callbacks: callbacks}}
end

defp send_rpc_request(state, method, params) do
Expand All @@ -213,6 +216,11 @@ defmodule ChromeRemoteInterface.PageSession do
end)
end

defp remove_callback(callbacks, from) do
callbacks
|> Enum.reject(&(&1 == from))
end

defp increment_ref_id(state) do
state
|> Map.update(:ref_id, 1, &(&1 + 1))
Expand All @@ -225,13 +233,16 @@ defmodule ChromeRemoteInterface.PageSession do
ref_id == id
end)
|> case do
{_ref_id, {:cast, method, from}} ->
{_ref_id, {:cast, method, from}} = callback ->
event = {:chrome_remote_interface, method, json}
send(from, event)
{_ref_id, {:call, from}} ->
remove_callback(callbacks, callback)
{_ref_id, {:call, from}} = callback ->
status = if error, do: :error, else: :ok
GenServer.reply(from, {status, json})
_ -> :ok
remove_callback(callbacks, callback)
_ ->
callbacks
end
end

Expand Down
35 changes: 35 additions & 0 deletions test/page_session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ defmodule ChromeRemoteInterface.PageSessionTest do
end
end

describe "RPC" do
test "Can call RPC events" do
websocket = spawn_fake_websocket()
from = {make_ref(), self()}

state = %PageSession{socket: websocket}
{:noreply, state} = PageSession.handle_call({:call_command, "TestCommand", %{}}, from, state)

assert [{_ref, {:call, _from}}] = state.callbacks
end

test "Receiving message for RPC event removes callback" do
websocket = spawn_fake_websocket()
from = {self(), make_ref()}

state = %PageSession{socket: websocket}
{:noreply, state} = PageSession.handle_call({:call_command, "TestCommand", %{}}, from, state)

assert [{ref, {:call, _from}}] = state.callbacks

frame = %{"id" => ref, "result" => %{"data" => %{"foo" => "bar"}}} |> Poison.encode!()
{:noreply, state} = PageSession.handle_info({:message, frame}, state)

assert [] = state.callbacks
end
end

def subscribe_to_test_event(state) do
{:reply, :ok, state} = PageSession.handle_call(
{:subscribe, "TestEvent", self()},
Expand All @@ -60,4 +87,12 @@ defmodule ChromeRemoteInterface.PageSessionTest do

PageSession.handle_info({:message, json}, state)
end

def spawn_fake_websocket() do
spawn_link(fn() ->
receive do
{:"$websockex_send", from, _frame} -> GenServer.reply(from, :ok)
end
end)
end
end

0 comments on commit 31767c2

Please sign in to comment.