LiveStruct - structs for liveview assigns

I decided to whip up this teeny library that lets you use structs as your liveview assigns. It’s kind of experimental for now (might break Phoenix LiveView? I haven’t tested it in too many use cases), I am curious to see if people will find this to be useful.

https://hexdocs.pm/live_struct/LiveStruct.html

6 Likes

Hi @ityonemo, is this for the purpose of guaranteeing assigns always has the same keys? Or something else?

That’s correct. I don’t know if this is a terrible idea (if liveview needs to dynamically create assigns)… So… That’s why I’d still consider this experimental.

2 Likes

Is it accurate to say that LiveView assigns are akin to GenServer state? If so, I’d be interested to know why this isn’t the recommended way of handling assigns in LiveView. Just like with GenServer state, it seems reasonable to be very careful about the possible fields and types for assigns, and codify all assumptions in a struct and custom type.

Can someone more familiar with development of LiveView explain why there isn’t a way do to this out of the box?

Surface adds props and data so you can specify your assigns. I believe this is being ported to LiveView at the moment.

Talking about Introduce a declarative API for components by msaraiva · Pull Request #1747 · phoenixframework/phoenix_live_view · GitHub?

Seems it solely targeted at components though, which makes the scope smaller than these LiveStructs. However, it also makes me wonder why the PR is limited….