-
Notifications
You must be signed in to change notification settings - Fork 935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add preload/2 that accepts list of previous assigns #2362
Conversation
@josevalim I took a stab at the |
4123a54
to
cdfeaf2
Compare
@josevalim maybe this has now become obsolete / less relevant now that we have streams? My original usecase was that I wanted to differentiate between newly added and existing livecomponents in a list. But after reading https://fly.io/phoenix-files/phoenix-dev-blog-streams/ I guess lists won't be using (in most cases) live components at all? |
Hi @dvic, sorry for the delay. I was not happy with the current proposal but I could not elaborate on why, so I left it in the "tinkering folder" in lieu of giving incomplete feedback. It is up to you if you think streams resolve this, but I think I was finally able to come up with a better API than the one that exists today that also addresses your problem. The issue with preload/upload dance is that you have to manage assigns in two different but deeply connected places: A LiveView may implement either def update_many(assigns, sockets) do
assigns = preload(assigns)
Enum.zip_with(assigns, sockets, fn assigns, socket -> update(assigns, socket) end)
end I think this would simplify your use case considerably and be generally more flexible. preload could be deprecated in the long term. Thoughts? |
No worries!
Interesting. This indeed solves the issue and I think that the API is more elegant as well. However, as an upgrade path, wouldn't you want to allow either In any case, implementation details aside, I think this is a good idea 👍 Curious to hear what others think about this! |
For the upgrade path, update_many will always replace both preload+update. So if you define it, none of them would be called. But we want to avoid generating code into the user module and rely on function_exported? checks instead. |
Closing this for now. If there's interest I can reserve some time for the |
Thank you @dvic. This is in the back of my mind but i don’t want to commit to anything yet before we address the LC and LV communication. |
Hi @dvic, sorry for the delay in here. We would like to go ahead with update_many, can you please send a PR? You must either use update_many or preload+update. If preload is defined, we will emit a deprecation warning telling you to use update_many. Also, we want to support |
No worries, I'll pick this up! You can expect a PR this week.
Sure! I read the discussion earlier, this would indeed help a lot 👍 I'll send a separate PR for this. |
Btw, ping :) |
Haven't forgotten about it, added a wip pr #2771 I found there are some sharp edges in adding this functionality without inserting user code. So what I did for now is update |
Fixes #2357