Hello
I followed some interesting thread , thread2 & others regarding state-management with non-trivial UI situations (eg a forum or a shopping ecommerce with an ubiquituous cart) and a likely candidate to good practice is using PubSub
or similar channel to store state.
My final use-case is very similar to a shopping cart, but I here isolate the problem to see if what I’m trying to do is possible and how.
# Router
live "/", PageLive
live "/second", SecondLive
# both at root.html.leex & live.html.leex
<%= inner_content %>
--- PageLive
# at "render" function ( also tried live_patch)
<%= live_redirect "SecondPage", to: Routes.live_path(@socket, LivesocketsWeb.SecondLive) %>
---- SecondLive
# at "render" ( also tried live_patch)
<%= live_redirect "Go to FirstPage", to: Routes.live_path(@socket, LivesocketsWeb.PageLive) %>
# at "handle_info"
# --> some logic to write to socket
socket = assign(socket, number: Enum.random(1..5000))
Then I’m interacting this way with the UI
-
Go to
/
-> Fires upPageLive
PID<0.XXX.0> -
Go to
/second
via link atPageLive
-> Fires upSecondLive
-> Kills PID<0.YYY.0> from:observer
app as far as I can see. -
After 3 seconds
SecondLive
sends itself (0.YYY.0) a new value
-> assign new value into socket, egassigns = %{ ..., number = 444}
-
Go to
/
via button to land onPageLive
again.
-> Kills PID<0.YYY.0> that was created bySecondLive
-> Now this creates a new PID<0.ZZZ.0>) that has no access to the previously modified socket withnumber
Question: Other than using PubSub
is there a way to keep the same socket (ie its assigned data) alive during route transitions to other LiveViews?
This is very much a gap in my understanding of the socket lifecycle when routing around liveviews, so any help highly appreciated, thank you!
Repo here https://github.com/git-toni/nested-lvs