Reply-Function for LiveViews

Hello there,
Whenever I setup a new project, there is a small function I always add: reply/1. What does it do?
In a LiveView mount, handle_event or handle_info
instead of writing
{:ok, stream(socket, :posts, Blog.list_posts())}
or
{:noreply, stream_insert(socket, :posts, post)}

one should be able to write

socket 
|> stream(:posts, Blog.list_posts())
|> reply(:ok)

or

socket
|> stream_insert(:posts, post)
|> reply(:noreply)

Therefore, a function called reply/1 could be added into Phoenix. It could be implemented somewhat like that:
def reply(socket, reply) when is_atom(reply), do: {reply, socket}

This could increase the readability (it’s faster to “scan” the code of your LiveView).
You can put that little function in your project, but I would suggest to make it a part of LiveView itself.

I am eager to hear your opinion on that :slight_smile:

2 Likes