Hello,
Can someone explain me why this simple code doesn’t work as expected ?
scope "/", TestWeb do
pipe_through :browser
live_session :default do
live "/", PageLive, :index
live "/:slug", PageLive, :show
end
end
defmodule TestWeb.PageLive do
use TestWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(:slug, "")}
end
@impl true
def handle_params(params, url, socket) do
{:noreply, socket |> assign(:slug, params["slug"])}
end
@impl true
def render(assigns) do
~H"""
<%= live_render(@socket, TestWeb.BLive, id: "b", session: %{"value" => @slug}) %>
<%= live_render(@socket, TestWeb.ALive, id: "a", session: %{"value" => @slug}) %>
"""
end
end
defmodule TestWeb.ALive do
use TestWeb, :live_view
@impl true
def mount(_params, session, socket) do
{:ok, socket |> assign(:value, session["slug"])}
end
@impl true
def handle_event("click", %{"slug" => value}, socket) do
{:noreply,
socket |> assign(:value, value)
}
end
@impl true
def handle_event("click", _, socket) do
{:noreply,
socket
|> assign(:value, "on")
}
end
@impl true
def render(assigns) do
IO.inspect(assigns, label: "A render")
~H"""
A (<%= @value %>) (<%= inspect(@value) %>)
"""
end
end
defmodule TestWeb.BLive do
use TestWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:value, "on")
}
end
@impl true
def handle_event("click", %{"slug" => value}, socket) do
{:noreply,
socket
|> assign(:value, value)
}
end
@impl true
def handle_event("click", _, socket) do
{:noreply,
socket
|> assign(:value, "on")
}
end
@impl true
def render(assigns) do
~H"""
B
<%= if @value == "off" do %>
<a data-phx-link="patch" data-phx-link-state="push" phx-click="click" phx-target="#a,#b" href="/on">on</a>
<% else %>
<a data-phx-link="patch" data-phx-link-state="push" phx-click="click" phx-value-slug="off" phx-target="#a,#b" href="/off">off</a>
<% end %>
"""
end
end
This code is a simple on/off switch, it works in the first few clicks, but then just freeze. I made a short video about this issue https://www.dropbox.com/s/9nuk5r8wdsbzwr8/elixir.mp4?dl=0
My first thought was that this is somehow related to safari browser, because on safari browser it breaks after a few clicks, on firefox or chrome it works a lot better, but when you click long enough it also breaks.