Skip to content

Commit

Permalink
Merge pull request #131 from phoenixframework/bms_liveview_reload
Browse files Browse the repository at this point in the history
Live reloading LiveViews without a hard refresh
  • Loading branch information
chrismccord authored Nov 29, 2022
2 parents 6771687 + 573a791 commit 78459f8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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

0 comments on commit 78459f8

Please sign in to comment.