teamon
I’m reading through the example todo_trek app to try to understand how it all works.
One thing I’ve noticed is that in the validate event handler the edited todo is built from incoming params:
While this works for the todo_trek app, it will fail when some other non-editable field is necessary to properly render the stream entry - let’s say for the sake of example that I’d like to render todo.inserted_at for every todo.
Since streams do not keep data in memory, in the validate handler I need to fetch todo from database to keep the inserted_at field rendered. This is not ideas, as the validate handler will be called multiple times, even when using phx-debounce.
One solution I can think of is to provide a short-lived cache using process dict with timer-based ttl.
What would be the preferred solution here?
Is there a way to avoid validate events at all?
Trending in Questions
Other Trending Topics
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cmo
I don’t see why you would need to query the database on every change to get information that the client already has. You ensure the client sends it back by making it part of the form, no?
teamon
The inserted_at field is not part of the form, it’s just rendered as
form.data.inserted_at.On the first render it’s all good because
form.datais a full Todo from database, but after validate event it has only id and list_id fields and rendering of inserted_at breakscmo
What is stopping you from making it part of the form? You want that data, you can get it by making it part of the form, you can keep it in memory or you can query for it.
With regards to your process dictionary cache idea, I’d probably use a proper cache so you’re not hand building a cache in every LiveView process. Kind of defeats the point of using streams in the first place doesn’t it?
KP123
Save them in your markup with
phx-value-*properties. As outlined in the docs here you could addphx-value-inserted-atto each todo then on event send this values.Using an in memory cache makes no sense here as you could just save the struct directly in your live view state without having to manage a new dependency.
teamon
Sending a single value might be ok, but imagine you need to also render Todo author avatar and you end up sending multiple values just to rebuild the Todo and User and Avatar schemas by hand. This quickly becames unmanagable and extremaly prone to errors. Not to mention that even with single inserted_at field it can be only sent as a binary so you need to convert it to proper DateTime which is just another layer of complexity. And if you render another field but forget to send it back you will only notice when you try to edit the Todo as the first render will work just fine.
I don’t agree the cache doesn’t make sense - with a long lists you can still benefit from not having to keep everything in memory but only to recently used items.
A proof of concept of a process-local cache could be something like this:
While it’s possible to use socket.assigns as the cache storage it seems more complex to work with when using nested live components that have their own state. If the cache is to be shared between components it must be passed as attributes, potentially causing unnecessary rerendering.
A different way of solving this issue would be to have two zipped streams like this:
and then allow updates to one stream only while keeping the other intact.
cmo
You have the same problem with forgetting to cache a value, no?
Is a cache per liveview better than a cache between all the liveviews and the database? You don’t think you’ll be caching the same thing in each liveview for each person on the page? And if you’re caching most of the data structure why bother with streams at all?
Are you sure that having a cache in the process dictionary that is designed to be accessed/edited in multiple places is easier to manage and reason about than passing it down explictly?
KP123
If you need the data in memory on the server don’t use streams, then you only need to send the ID of the entity you want to work on back to live view.