DEV Community

Discussion on: URL shortener with Elixir Phoenix 1.6 and LiveView

Collapse
 
simonmcconnell profile image
Simon McConnell • Edited

I would reconsider piping the result of a Repo call into a broadcast function. Clearly the broadcasting should only be done when the Repo call was successful, but it is not the PubSub's responsibility to check the result of a Repo call.

def broadcast_record({:ok, record}, event) when is_struct(record) do
  Phoenix.PubSub.broadcast(MyApp.PubSub, @topic, {event, record})
  {:ok, record}
end

def broadcast_record({:error, reason}, _event), do: {:error, reason}
Enter fullscreen mode Exit fullscreen mode

Could be:

def broadcast(record, event) when is_struct(record) do
  Phoenix.PubSub.broadcast(MyApp.PubSub, @topic, {event, record})
end
Enter fullscreen mode Exit fullscreen mode