Liveview: is_connected macro?

I came across a blog post that implemented a function guard for a liveview socket connection’s presence. It seems like a really ergonomic way to separate out mount functions for connected sockets and disconnected sockets, and I was surprised I wasn’t able to find any existing proposals or suggestions to implement connected? as a macro (very possibly my search fu failing me).

Is there any appetite in implementing this function guard in core liveview? Are there reasons it’s not implemented as a guard yet.

Code referenced in the blog post:

defguard is_connected?(socket) when socket.transport_pid != nil

Enabling:

def mount(params, session, socket) when is_connected?(socket) do
  # Connected mount
end

def mount(params, session, socket) do
  # Disconnected mount
end
4 Likes