Skip to content

Commit

Permalink
pubsub integration for phx.gen.live
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Feb 20, 2025
1 parent 6c5d3fc commit ba53e60
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
7 changes: 7 additions & 0 deletions priv/templates/phx.gen.context/access_no_schema_scope.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
alias <%= inspect schema.module %>
alias <%= inspect scope.alias %>

@doc """
Subscribes to scoped notifications about any <%= schema.singular %> changes.
"""
def subscribe_<%= schema.singular %>(%<%= inspect scope.alias %>{} = _<%= scope.name %>_scope) do
raise "TODO"
end

@doc """
Returns the list of <%= schema.plural %>.
Expand Down
48 changes: 41 additions & 7 deletions priv/templates/phx.gen.context/schema_access_scope.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
alias <%= inspect schema.module %>
alias <%= inspect scope.module %>

@doc """
Subscribes to scoped notifications about any <%= schema.singular %> changes.
The broadcasted messages match the pattern:
* {:created, %<%= inspect schema.alias %>{}}
* {:updated, %<%= inspect schema.alias %>{}}
* {:deleted, %<%= inspect schema.alias %>{}}
"""
def subscribe_<%= schema.plural %>(%<%= inspect scope.alias %>{} = <%= scope.name %>_scope) do
key = <%= scope.name %>_scope.<%= Enum.join(scope.access_path, ".") %>

Phoenix.PubSub.subscribe(<%= inspect context.base_module %>.PubSub, "<%= scope.name %>:#{key}:<%= schema.plural %>")
end

defp broadcast(%<%= inspect scope.alias %>{} = <%= scope.name %>_scope, message) do
key = <%= scope.name %>_scope.<%= Enum.join(scope.access_path, ".") %>

Phoenix.PubSub.broadcast(<%= inspect context.base_module %>.PubSub, "<%= scope.name %>:#{key}:<%= schema.plural %>", message)
end

@doc """
Returns the list of <%= schema.plural %>.
Expand Down Expand Up @@ -46,9 +68,13 @@
"""
def create_<%= schema.singular %>(%<%= inspect scope.alias %>{} = <%= scope.name %>_scope, attrs \\ %{}) do
%<%= inspect schema.alias %>{}
|> <%= inspect schema.alias %>.changeset(attrs, <%= scope.name %>_scope)
|> Repo.insert()
with {:ok, <%= schema.singular %> = %<%= inspect schema.alias %>{}} <-
%<%= inspect schema.alias %>{}
|> <%= inspect schema.alias %>.changeset(attrs, <%= scope.name %>_scope)
|> Repo.insert() do
broadcast(<%= scope.name %>_scope, {:created, <%= schema.singular %>})
{:ok, <%= schema.singular %>}
end
end

@doc """
Expand All @@ -66,9 +92,13 @@
def update_<%= schema.singular %>(%<%= inspect scope.alias %>{} = <%= scope.name %>_scope, %<%= inspect schema.alias %>{} = <%= schema.singular %>, attrs) do
true = <%= schema.singular %>.<%= scope.schema_key %> == <%= scope.name %>_scope.<%= Enum.join(scope.access_path, ".") %>

<%= schema.singular %>
|> <%= inspect schema.alias %>.changeset(attrs, <%= scope.name %>_scope)
|> Repo.update()
with {:ok, <%= schema.singular %> = %<%= inspect schema.alias %>{}} <-
<%= schema.singular %>
|> <%= inspect schema.alias %>.changeset(attrs, <%= scope.name %>_scope)
|> Repo.update() do
broadcast(<%= scope.name %>_scope, {:updated, <%= schema.singular %>})
{:ok, <%= schema.singular %>}
end
end

@doc """
Expand All @@ -86,7 +116,11 @@
def delete_<%= schema.singular %>(%<%= inspect scope.alias %>{} = <%= scope.name %>_scope, %<%= inspect schema.alias %>{} = <%= schema.singular %>) do
true = <%= schema.singular %>.<%= scope.schema_key %> == <%= scope.name %>_scope.<%= Enum.join(scope.access_path, ".") %>

Repo.delete(<%= schema.singular %>)
with {:ok, <%= schema.singular %> = %<%= inspect schema.alias %>{}} <-
Repo.delete(<%= schema.singular %>) do
broadcast(<%= scope.name %>_scope, {:deleted, <%= schema.singular %>})
{:ok, <%= schema.singular %>}
end
end

@doc """
Expand Down
11 changes: 9 additions & 2 deletions priv/templates/phx.gen.live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
end

@impl true
def mount(_params, _session, socket) do
def mount(_params, _session, socket) do<%= if scope do %>
<%= inspect context.alias %>.subscribe_<%= schema.plural %>(<%= socket_scope %>)
<% end %>
{:ok,
socket
|> assign(:page_title, "Listing <%= schema.human_plural %>")<%= if primary_key != :id do %>
Expand All @@ -54,5 +56,10 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
{:ok, _} = <%= inspect context.alias %>.delete_<%= schema.singular %>(<%= context_scope_prefix %><%= schema.singular %>)

{:noreply, stream_delete(socket, :<%= schema.collection %>, <%= schema.singular %>)}
end
end<%= if scope do %>

@impl true
def handle_info({type, %<%= inspect schema.module %>{}}, socket) when type in [:created, :updated, :deleted] do
{:noreply, stream(socket, :<%= schema.collection %>, <%= inspect context.alias %>.list_<%= schema.plural %>(<%= socket_scope %>), reset: true)}
end<% end %>
end
16 changes: 15 additions & 1 deletion priv/templates/phx.gen.live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,24 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
end

@impl true
def mount(%{"<%= primary_key %>" => <%= primary_key %>}, _session, socket) do
def mount(%{"<%= primary_key %>" => <%= primary_key %>}, _session, socket) do<%= if scope do %>
<%= inspect context.alias %>.subscribe_<%= schema.plural %>(<%= socket_scope %>)
<% end %>
{:ok,
socket
|> assign(:page_title, "Show <%= schema.human_singular %>")
|> assign(:<%= schema.singular %>, <%= inspect context.alias %>.get_<%= schema.singular %>!(<%= context_scope_prefix %><%= primary_key %>))}
end<%= if scope do %>

@impl true
def handle_info({:updated, %<%= inspect schema.module %>{<%= primary_key %>: <%= primary_key %>} = <%= schema.singular %>}, %{assigns: %{<%= schema.singular %>: %{<%= primary_key %>: <%= primary_key %>}}} = socket) do
{:noreply, assign(socket, :<%= schema.singular %>, <%= schema.singular %>)}
end

def handle_info({:deleted, %<%= inspect schema.module %>{<%= primary_key %>: <%= primary_key %>}}, %{assigns: %{<%= schema.singular %>: %{<%= primary_key %>: <%= primary_key %>}}} = socket) do
{:noreply,
socket
|> put_flash(:error, "The current <%= schema.singular %> was deleted.")
|> push_navigate(to: ~p"<%= schema.route_prefix %>")}
end<% end %>
end

0 comments on commit ba53e60

Please sign in to comment.