Skip to content
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
17 changes: 8 additions & 9 deletions lib/phoenix_live_session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ defmodule PhoenixLiveSession do

defp put_meta(data, sid, opts) do
data
|> Map.put(:__sid__, sid)
|> Map.put(:__opts__, opts)
|> Map.put("__sid__", sid)
|> Map.put("__opts__", opts)
end

defp maybe_clean(opts) do
Expand Down Expand Up @@ -225,20 +225,19 @@ defmodule PhoenixLiveSession do
"""
@spec maybe_subscribe(Phoenix.LiveView.Socket.t(), Plug.Session.Store.session()) ::
Phoenix.LiveView.Socket.t()
def maybe_subscribe(socket, session) do
def maybe_subscribe(socket, %{"__sid__" => sid, "__opts__" => opts}) do
if LiveView.connected?(socket) do
sid = Map.fetch!(session, :__sid__)
opts = Map.fetch!(session, :__opts__)
pub_sub = Keyword.fetch!(opts, :pub_sub)
channel = "live_session:#{sid}"
PubSub.subscribe(pub_sub, channel)
PubSub.subscribe(pub_sub, "live_session:#{sid}")

put_in(socket.private[:live_session], id: sid, opts: opts)
else
socket
end
end

def maybe_subscribe(socket, _), do: socket

@doc """
This function can be called in two ways:any()

Expand All @@ -263,8 +262,8 @@ defmodule PhoenixLiveSession do
socket
end

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

get(nil, sid, opts)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule PhoenixLiveSession.MixProject do
%{
description: "In-memory live sessions for LiveViews and Phoenix controllers.",
licenses: ["Apache-2.0"],
links: %{"GitHub" => "https://github.com/pentacent/phoenix_live_session"},
links: %{"GitHub" => "https://github.com/pentacent/phoenix_live_session"}
}
end

Expand Down