LiveAttribute - Save typing up all those subscribe(), handle_info(), fetch...() pairs

Just created a teeny tiny library to save some of the plumbing work when connecting a LiveView with subscriptions on updateable lists. Save yourself from implementing more handcrafted handle_update() update_...() functions with LiveAttribute

Gist of it in a before/after screenshots:

This works also great if you use the same set of attributes in multiple Views, just put your attribute definition into a function and use it in all your views:

defmodule MyApp.Attributes do
   def users({&Accounts.subscribe/0, users: &Accounts.list_all/0})
end

defmodule UserLive do
   use Phoenix.LiveView
   use LiveAttribute

   def mount(_params, _session, socket) do
      {:ok, assign_attribute(socket, MyApp.Attributes.users())}
  end
end

I’m super curious to hear what other approaches there are to remove the handcrafting, and what others have been doing so far.

Cheers!

6 Likes