Skip to content

Commit 945c0a2

Browse files
Fix not-found error key in test mode
issue: All session keys in a Plug.Conn get turned into strings, so there's no way to setup the maybe_subscribe for success in a test environment. issue ref: pentacent#13
1 parent ce93852 commit 945c0a2

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lib/phoenix_live_session.ex

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,20 @@ defmodule PhoenixLiveSession do
225225
"""
226226
@spec maybe_subscribe(Phoenix.LiveView.Socket.t(), Plug.Session.Store.session()) ::
227227
Phoenix.LiveView.Socket.t()
228-
def maybe_subscribe(socket, session) do
228+
def maybe_subscribe(socket, %{"__sid__" => sid, "__opts__" => opts}) do
229229
if LiveView.connected?(socket) do
230-
sid = Map.fetch!(session, :__sid__)
231-
opts = Map.fetch!(session, :__opts__)
232230
pub_sub = Keyword.fetch!(opts, :pub_sub)
233-
channel = "live_session:#{sid}"
234-
PubSub.subscribe(pub_sub, channel)
231+
232+
PubSub.subscribe(pub_sub, "live_session:#{sid}")
235233

236234
put_in(socket.private[:live_session], id: sid, opts: opts)
237235
else
238236
socket
239237
end
240238
end
241239

240+
def maybe_subscribe(socket, _), do: socket
241+
242242
@doc """
243243
This function can be called in two ways:any()
244244
@@ -253,8 +253,8 @@ defmodule PhoenixLiveSession do
253253
from the `mount/3` callback directly in this function.
254254
Retrieves and returns updated session data.
255255
"""
256-
@spec put_session(Phoenix.LiveView.Socket.t(), String.t() | atom(), term()) ::
257-
Phoenix.LiveView.Socket.t()
256+
@spec put_session(Phoenix.LiveView.Socket.t() | map(), String.t() | atom(), term()) ::
257+
Phoenix.LiveView.Socket.t() | %{}
258258
def put_session(%Phoenix.LiveView.Socket{} = socket, key, value) do
259259
sid = get_in(socket.private, [:live_session, :id])
260260
opts = get_in(socket.private, [:live_session, :opts])
@@ -263,8 +263,7 @@ defmodule PhoenixLiveSession do
263263
socket
264264
end
265265

266-
@spec put_session(%{__sid__: String.t(), __opts__: list()}, String.t() | atom(), term()) :: %{}
267-
def put_session(%{__sid__: sid, __opts__: opts}, key, value) do
266+
def put_session(%{"__sid__" => sid, "__opts__" => opts}, key, value) do
268267
put_in(sid, to_string(key), value, opts)
269268

270269
get(nil, sid, opts)

0 commit comments

Comments
 (0)