Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live reloading LiveViews without a hard refresh #131

Merged
merged 3 commits into from
Nov 29, 2022
Merged
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: 16 additions & 1 deletion lib/phoenix_live_reload/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Phoenix.LiveReloader.Channel do
socket
|> assign(:patterns, config[:patterns] || [])
|> assign(:debounce, config[:debounce] || 0)
|> assign(:notify_patterns, config[:notify] || [])

{:ok, socket}
else
Expand All @@ -24,7 +25,11 @@ defmodule Phoenix.LiveReloader.Channel do
end

def handle_info({:file_event, _pid, {path, _event}}, socket) do
%{patterns: patterns, debounce: debounce} = socket.assigns
%{
patterns: patterns,
debounce: debounce,
notify_patterns: notify_patterns,
} = socket.assigns

if matches_any_pattern?(path, patterns) do
ext = Path.extname(path)
Expand All @@ -36,6 +41,16 @@ defmodule Phoenix.LiveReloader.Channel do
end
end

for {topic, patterns} <- notify_patterns do
if matches_any_pattern?(path, patterns) do
Phoenix.PubSub.broadcast(
socket.pubsub_server,
to_string(topic),
{:phoenix_live_reload, topic, path}
)
end
end

{:noreply, socket}
end

Expand Down
9 changes: 9 additions & 0 deletions test/channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,13 @@ defmodule Phoenix.LiveReloader.ChannelTest do
send(socket.channel_pid, file_event('a/b/c/lib/live_web/views/user_view.ex', :created))
assert_push "assets_change", %{asset_type: "ex"}
end

@endpoint MyApp.ReloadEndpoint
test "sends notification for liveviews" do
{:ok, _, socket} =
LiveReloader.Socket |> socket() |> subscribe_and_join(Channel, "phoenix:live_reload", %{})
socket.endpoint.subscribe("live_view")
send(socket.channel_pid, file_event("lib/live_web/live/user_live.ex", :created))
assert_receive {:phoenix_live_reload, :live_view, "lib/live_web/live/user_live.ex"}
end
end
26 changes: 26 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,37 @@ Application.put_env(:phoenix_live_reload, MyApp.EndpointWrongWindow,
]
)

Application.put_env(:phoenix_live_reload, MyApp.ReloadEndpoint,
pubsub_server: MyApp.PubSub,
live_reload: [
url: "ws://localhost:4000",
patterns: [
~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"priv/gettext/.*(po)$",
~r{web/views/.*(ex)$},
~r{web/templates/.*(eex)$}
],
notify: [
live_view: [
~r{web/components.ex$},
~r{web/live/.*(ex)$}
]
]
]
)

defmodule MyApp.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix_live_reload

socket "/socket", Phoenix.LiveReloader.Socket, websocket: true, longpoll: true
end

defmodule MyApp.ReloadEndpoint do
use Phoenix.Endpoint, otp_app: :phoenix_live_reload

socket "/socket", Phoenix.LiveReloader.Socket, websocket: true, longpoll: true
end

defmodule MyApp.EndpointScript do
use Phoenix.Endpoint, otp_app: :phoenix_live_reload
end
Expand All @@ -74,6 +99,7 @@ children = [
MyApp.EndpointConfig,
MyApp.EndpointParentWindow,
MyApp.EndpointWrongWindow,
MyApp.ReloadEndpoint
]

Supervisor.start_link(children, strategy: :one_for_one)
Expand Down