bianchidotdev

bianchidotdev

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

Showing Posts 1 to 5

al2o3cr

al2o3cr

One reason this may not exist already - the map.field notation wasn’t supported in guards until this PR, released in 1.11:

https://github.com/elixir-lang/elixir/pull/9711

Phoenix 1.6 only requires Elixir 1.9+, so Phoenix 1.7 is the earliest version this guard could have been included in.

D4no0

D4no0

definetly some new feature I didn’t see yet

JohnnyCurran

JohnnyCurran

Glad you liked the article! I have been thinking about making a PR to add it to LiveView. I wanted to get some feedback on it first. I suppose this thread might serve that purpose

I haven’t tested it, but I imagine the map.field syntax could be worked around by matching on the fields in the struct:

defguard connected?(%Socket{transport: transport}) when transport != nil

But that’s just a guess. I wrote that in the forum post editor

codeanpeace

codeanpeace

Gave it a whirl and it doesn’t look like you can pattern match when defining guards:

** (ArgumentError) invalid syntax in defguard is_connected?(%Socket{transport_pid: transport_pid})

JohnnyCurran

JohnnyCurran

:frowning:

Thanks for giving it a shot though!

I guess that makes sense because guards have to be compile time constants and pattern matching is not

— All posts loaded —

Where Next? Top

Trending in Proposals: Ideas Top

woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews