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

Push pending events before redirecting #2923

Merged
merged 1 commit into from
Nov 17, 2023
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
8 changes: 8 additions & 0 deletions lib/phoenix_live_view/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -799,20 +799,23 @@ defmodule Phoenix.LiveView.Channel do
|> Map.put(:to, to)

new_state
|> push_pending_events_on_redirect(new_socket)
|> push_redirect(opts, ref)
|> stop_shutdown_redirect(:redirect, opts)

{:redirect, %{to: _to} = opts} ->
opts = copy_flash(new_state, flash, opts)

new_state
|> push_pending_events_on_redirect(new_socket)
|> push_redirect(opts, ref)
|> stop_shutdown_redirect(:redirect, opts)

{:live, :redirect, %{to: _to} = opts} ->
opts = copy_flash(new_state, flash, opts)

new_state
|> push_pending_events_on_redirect(new_socket)
|> push_live_redirect(opts, ref, pending_diff_ack)
|> stop_shutdown_redirect(:live_redirect, opts)

Expand All @@ -837,6 +840,11 @@ defmodule Phoenix.LiveView.Channel do
end
end

defp push_pending_events_on_redirect(state, socket) do
Copy link
Member

Choose a reason for hiding this comment

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

Consider moving this function to the Diff module.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the function makes sense here because it depends on push maybe we could add a get_push_events function to Diff which returns either {"diff", %{e: events}} or nil?

Presumably the goal here is to avoid the duplication of "diff" and @events?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah. get_push_events sounds good to me!

Copy link
Member Author

@Gazler Gazler Nov 17, 2023

Choose a reason for hiding this comment

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

if diff = Diff.get_push_events_diff(socket), do: push_diff(state, diff, nil)
state
end

defp patch_params_and_action!(socket, %{to: to}) do
destructure [path, query], :binary.split(to, ["?", "#"], [:global])
to = %{socket.host_uri | path: path, query: query}
Expand Down
7 changes: 7 additions & 0 deletions lib/phoenix_live_view/diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ defmodule Phoenix.LiveView.Diff do
end
end

@doc """
Returns a diff containing only the events that have been pushed.
"""
def get_push_events_diff(socket) do
if events = Utils.get_push_events(socket), do: %{@events => events}
end

defp maybe_put_title(diff, socket) do
if Utils.changed?(socket.assigns, :page_title) do
Map.put(diff, @title, socket.assigns.page_title)
Expand Down
23 changes: 23 additions & 0 deletions test/phoenix_live_view/integrations/event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ defmodule Phoenix.LiveView.EventTest do
assert render(view) =~ "count: 123"
end

test "supports events with redirects", %{conn: conn} do
{:ok, view, _html} = live(conn, "/events")

GenServer.call(
view.pid,
{:run,
fn socket ->
new_socket =
socket
|> Component.assign(count: 123)
|> LiveView.push_event("my-event", %{one: 1})
|> LiveView.push_event("my-event", %{one: 2})
|> LiveView.push_redirect(to: "/events")

{:reply, :ok, new_socket}
end}
)

assert_push_event(view, "my-event", %{one: 1})
assert_push_event(view, "my-event", %{one: 2})
assert_redirected(view, "/events")
end

test "sends updates with no assigns diff", %{conn: conn} do
{:ok, view, _html} = live(conn, "/events")

Expand Down