Skip to content

Commit

Permalink
send updates when liveview files are updated
Browse files Browse the repository at this point in the history
  • Loading branch information
bemesa21 committed Nov 11, 2022
1 parent 6771687 commit 207cc31
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/phoenix_live_reload/channel.ex
Expand Up @@ -11,11 +11,14 @@ defmodule Phoenix.LiveReloader.Channel do
if Process.whereis(:phoenix_live_reload_file_monitor) do
FileSystem.subscribe(:phoenix_live_reload_file_monitor)
config = socket.endpoint.config(:live_reload)
live_reload_config = socket.endpoint.config(:live_view)

socket =
socket
|> assign(:patterns, config[:patterns] || [])
|> assign(:debounce, config[:debounce] || 0)
|> assign(:notify_patterns, config[:notify] || [])
|> assign(:live_reload_topic, live_reload_config[:live_reload_topic] || nil)

{:ok, socket}
else
Expand All @@ -24,7 +27,12 @@ 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,
live_reload_topic: live_reload_topic
} = socket.assigns

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

if matches_any_pattern?(path, notify_patterns) do
Phoenix.PubSub.broadcast(
socket.pubsub_server,
live_reload_topic,
{:phoenix_live_reload, live_reload_topic, path}
)
end

{:noreply, socket}
end

Expand Down

0 comments on commit 207cc31

Please sign in to comment.