[HN Gopher] Phoenix LiveView: Async Assign Pattern
___________________________________________________________________
Phoenix LiveView: Async Assign Pattern
Author : gnutrino
Score : 62 points
Date : 2023-06-14 18:37 UTC (4 hours ago)
(HTM) web link (blog.andyglassman.com)
(TXT) w3m dump (blog.andyglassman.com)
| social_ism wrote:
| [dead]
| gnutrino wrote:
| I started using this pattern in my LiveView projects, and finally
| rolled it into a common module for others to use. I've found it
| really helps clean up the mount function, which can get pretty
| messy as your LiveViews grow over time.
|
| My apologies for the terrible elixir syntax highlighting in this
| post. I keep putting off finding a good way to do this on
| Blogger.
| conradfr wrote:
| Are you sure your import should not be: import
| Phoenix.Socket, only: [assign: 2, assign: 3]
| gnutrino wrote:
| I believe you are correct. I could probably remove the
| imports altogether, and just prefix those functions with
| `Phoenix.LiveView`.
| sodapopcan wrote:
| +1 for removing imports!
|
| Not that importing is useless but single-use imports drive
| me a bit nuts. Even just using it a few times. It's nice to
| be able to jump to some code and know exactly where
| everything is coming from without having to jump around the
| file.
| sodapopcan wrote:
| When using LiveView I always use: import
| Phoenix.Component, only: [assign: 2, assign: 3]
|
| https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#.
| ..
|
| Importing from Socket should work, but you're right, the code
| in the post wouldn't work. They use to be in LiveView but got
| moved to Component.
| wahnfrieden wrote:
| Anyone doing similar with Swift given it also has server
| continuations?
| tommica wrote:
| Looks really useful and neat especially the way it chains so
| fluently for parallerization
| gnutrino wrote:
| As I evolved the pattern over time, I found it really did help
| clean up a lot of code in `mount`. Once I got it working for
| one LiveView, it was really easy to rip out a lot of messy code
| in the other LiveViews. I'm definitely happy with the results!
| Thanks for reading.
| vinnymac wrote:
| I haven't written Elixir professionally the last couple of years,
| but I was an early user of LiveView when it was first demoed at a
| conference.
|
| As someone who currently writes applications using React Server
| Components, I am interested to see how concepts like LiveView
| evolve over time. We are now shipping significantly more HTML
| than before, and a lot less Javascript, as more has been moved to
| the server.
|
| Makes me wish LiveView was portable to every other platform I
| work on, rather than having to convince companies to write more
| Elixir (although that isn't a bad thing).
| nocsi wrote:
| There's https://native.live if you want to deploy it as a
| native app. You can also embed React Components into LiveView
| if you wanted. Elixir can be anywhere you want it to be.
| gnutrino wrote:
| I haven't looked into this much, but saw someone do a talk on
| it at ElixirConf maybe two years ago. I bet it's much more
| robust now.
| floodfx wrote:
| There is a port of it to JavaScript/Typescript.
| https://LiveViewJS.com
|
| It uses the same front end javascript from Phoenix and speaks
| the same protocol.
|
| Note: am author
| jjdeveloper wrote:
| Does this work with Astro (or Next.js)?
| golemiprague wrote:
| [dead]
___________________________________________________________________
(page generated 2023-06-14 23:01 UTC)