diff --git a/apps/api_web/lib/api_web/event_stream.ex b/apps/api_web/lib/api_web/event_stream.ex index 5dd85de2..fcb090ce 100644 --- a/apps/api_web/lib/api_web/event_stream.ex +++ b/apps/api_web/lib/api_web/event_stream.ex @@ -21,11 +21,16 @@ defmodule ApiWeb.EventStream do @spec call(Plug.Conn.t(), module, map) :: Plug.Conn.t() def call(conn, module, _params) do - state = initialize(conn, module) - hibernate_loop(state) + case initialize(conn, module) do + %__MODULE__{} = state -> + hibernate_loop(state) + + %Plug.Conn{} = conn -> + conn + end end - @spec initialize(Plug.Conn.t(), module) :: state + @spec initialize(Plug.Conn.t(), module) :: state | Plug.Conn.t() def initialize(conn, module, timeout \\ 30_000) do conn = conn @@ -41,8 +46,8 @@ defmodule ApiWeb.EventStream do ensure_timer(%__MODULE__{conn: conn, pid: pid, timeout: timeout}) else - send(self(), {:close, conn}) - %__MODULE__{conn: conn, pid: nil, timeout: timeout} + state = %__MODULE__{conn: conn, pid: nil, timeout: timeout} + unsubscribe(state) end end diff --git a/apps/api_web/test/api_web/event_stream_test.exs b/apps/api_web/test/api_web/event_stream_test.exs index 2ccaf3c3..0ac2661d 100644 --- a/apps/api_web/test/api_web/event_stream_test.exs +++ b/apps/api_web/test/api_web/event_stream_test.exs @@ -141,8 +141,8 @@ defmodule ApiWeb.EventStreamTest do } do CheckForShutdown.shutdown() - state = initialize(conn, @module) - assert {:close, conn} = receive_result(state) + conn = call(conn, @module, %{}) + assert_receive {:plug_conn, :sent} assert chunks(conn) == "" end