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
Why does using handle_call mean the calling LiveView will be updated twice? #84
Comments
@tlietz thank you for opening this issue/question. 👍 |
Currently the counter value is indeed updated twice. The first time when the incr/decr event is trigger by the user: phoenix-liveview-counter-tutorial/lib/live_view_counter_web/live/counter.ex Lines 23 to 29 in fdeee51
We see that the socket is assigned the new counter value. This will update automatically the value on the client:
see https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#module-life-cycle Then because the phoenix-liveview-counter-tutorial/lib/live_view_counter/count.ex Lines 46 to 50 in fdeee51
The phoenix-liveview-counter-tutorial/lib/live_view_counter_web/live/counter.ex Lines 31 to 33 in fdeee51
Which assigns again the counter value and update again the client. So this is way the LiveView value is updated twice. However I don't think it is directly a consequences of using You can test this by removing the assign in the def handle_event("inc", _, socket) do
Count.incr()
{:noreply, socket}
end
def handle_event("dec", _, socket) do
Count.decr()
{:noreply, socket}
end Concerning the To conclude maybe we can still remove this section in the Readme as I think it creates more confusions. I hope this makes sense, let me know if you have any other questions @tlietz and thanks for opening this issue. |
Remove section in Readme, Update dependencies and config see: #84
Remove section in Readme, Update dependencies and config see: #84
Remove section in Readme, Update dependencies and config see: #84
PR removing the section in Readme, merged #85 |
In the Moving State Out of LiveViews section, there is a note stating:
We could have used asyncronous handle_cast functions and relied on the PubSub to update us. Using handle_call means the calling LiveView will be updated twice, but it doesn't really matter at this scale
.Why will the LiveView be updated twice?
I put an
IO.inspect()
in therender
function ofLiveViewCounterWeb.Counter
liveview to verify that it only gets called once. However, I may be misunderstanding something:Upon incrementing the counter once, the following is printed:
The text was updated successfully, but these errors were encountered: