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.
I'm still on 17.X, so that probably explains the discrepancy here. I think I tried doing a major version bump a while back and things didn't compile so I put it on the backlog and forgot about it. It may be time to revisit before I get too far behind.
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.
Knowing the origin of the function is a huge benefit, but also imports create compile time dependency. In this case, it's not an issue, but when you're importing a lot of things across your codebase, it can become an issue.
Ya, if you design relationships as a DAG then those sorts of compile time dependencies don't matter. Of course sometimes it can't be avoided (Phoenix router, ecto relationships) the router isn't updated very often and schemas don't have too far a reach (at least they don't if you keep them in their contexts).
I don't avoid import but I generally I strive to import one module where I'm going to be using many functions. I rarely bother with `only` unless it's absolutely necessary due to conflicts otherwise specifying `only` is a good indication I don't even need to import.
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).
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.
Does this work well with tests? For instance, if you click a button in a test, do you need to do something special to ensure the async update has happened before checking assertions on the HTML contents?
I mean for async assigns specifically. I did something like this in the past and had some troubles with render_click returning the html before the async update completes.
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.
My apologies for the terrible elixir syntax highlighting in this post. I keep putting off finding a good way to do this on Blogger.