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

v1.4.1-HMR for review #5

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.4.1 (2022-11-29)

* Improvements
* Support new `:notify` configuration for third-party integration to file change events

## 1.4.0 (2022-10-29)

* Improvements
Expand Down
19 changes: 18 additions & 1 deletion lib/phoenix_live_reload/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Phoenix.LiveReloader.Channel do
|> assign(:patterns, config[:patterns] || [])
|> assign(:debounce, config[:debounce] || 0)
|> assign(:root, root)
|> assign(:notify_patterns, config[:notify] || [])

{:ok, socket}
else
Expand All @@ -36,7 +37,13 @@ defmodule Phoenix.LiveReloader.Channel do
@impl true
def handle_info({:file_event, _pid, {path, _event}}, socket) do
with {:stale, socket} <- check_last_modified_at(socket, path) do
%{patterns: patterns, debounce: debounce, root: root} = socket.assigns
%{
patterns: patterns,
debounce: debounce,
notify_patterns: notify_patterns,
root: root
} = socket.assigns

if matches_any_pattern?(path, patterns) do
ext = Path.extname(path)

Expand All @@ -48,6 +55,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
Comment on lines +58 to +66
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基本的には:notifyオプションの追加でnotify_patternsからPubSubで他のappにも通知できるようになったのが主な修正のようでした。

PR見た感じ:phoenix_live_viewで利用できるように拡張したらしい
phoenixframework#131

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:naruhodo:


{:noreply, socket}
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/phoenix_live_reload/live_reloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ defmodule Phoenix.LiveReloader do
* `:script_attrs` - HACK attrs to be given to the script injected by
live reload. Expects a keyword list of atom keys and string values.

* `:notify` - a keyword list of topics pointing to a list of patterns.
A mesage of the form `{:phoenix_live_reload, topic, path}` will be
broadcast on the topic whenever file in the list of patterns changes.

* `:debounce` - an integer in milliseconds to wait before sending
live reload events to the browser. Defaults to `0`.

* `:iframe_attrs` - attrs to be given to the iframe injected by
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

間違って前回のbranchで戻してしまってたので再度削除

live reload. Expects a keyword list of atom keys and string values.

* `:target_window` - the window that will be reloaded, as an atom.
Valid values are `:top` and `:parent`. An invalid value will
default to `:top`.
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule PhoenixLiveReload.Mixfile do
use Mix.Project

@version "1.4.0"
@version "1.4.1"

def project do
[
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