For complex reasons (yes, I know this is the prototype of an “X Y problem” but I’m just trying to find out what is the simplest thing that would work without too many changes to existing code) I need to access the %Phoenix.LiveView{}.assigns
in a helper function. That helper funcion needs to be able to handle a number of cases depending on whether it’s an initial render, a diff, a change to a different liveview, etc.
Currently, I’m doing this:
defp update_params(params, socket) do
# Fill in the parameters of the path by looking them up in the socket.
# There is a couple cases we must handle.
# I think these cases are exhaustive but I'm not exactly sure
# as I'm intimately familiar with the intrincacies of LiveView.
# I'm also not sure on whether using the `:__assigns__` or `:changed`
# fields is using public or private APIs.
cond do
Map.has_key?(socket.assigns, :__assigns__) ->
Map.merge(params, socket.assigns.__assigns__.params)
Map.has_key?(socket.assigns, :changed) ->
Map.merge(params, socket.assigns.changed)
true ->
Map.merge(params, socket.assigns.params)
end
end
The goal is to merge the parameters in the socket assigns (which might have a number of different forms depending on what exactly is happening) with the params
given as argument.
I’m pretty sure I shouldn’t be doing what I’m doing above, because the :__assigns__
and the :changed
keys don’t seem to be documented as public.
Is there a portable way of accessing the parameters of a socket?