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
Trending in Proposals: Ideas
I’m a bit in a fuzz about this topic. I’ve worked out some sort of Dropbox feature for a specific client. This feature allows him to uplo...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
One reason this may not exist already - the
map.fieldnotation 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
definetly some new feature I didn’t see yet
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:
But that’s just a guess. I wrote that in the forum post editor
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
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