Comments (3)
Currently the counter value is indeed updated twice.
The first time when the incr/decr event is trigger by the user:
We see that the socket is assigned the new counter value. This will update automatically the value on the client:
Any time a stateful view changes or updates its socket assigns, it is automatically re-rendered and the updates are pushed to the client.
see https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#module-life-cycle
Then because the Count/incr or Count.decr
function is called to update the value, PubSub
will send another event to all the connected clients, included the user who clicked on the inc/decr button:
phoenix-liveview-counter-tutorial/lib/live_view_counter/count.ex
Lines 46 to 50 in fdeee51
The :count
event is handle with:
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 handle_call
(vs handle_cast
) in the GenServer count
. If we still assign
a value to the socket even if we use handle_cast
the client will still be updated twice.
You can test this by removing the assign in the handle
function, you will see the counter is still updated because of PubSub:
def handle_event("inc", _, socket) do
Count.incr()
{:noreply, socket}
end
def handle_event("dec", _, socket) do
Count.decr()
{:noreply, socket}
end
Concerning the IO.inspect
display the values only once, this is normal. LiveView will only call/render the view once then it will update only the html nodes when the socket is updated but the render
function will not be called again.
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.
from phoenix-liveview-counter-tutorial.
PR removing the section in Readme, merged #85
Don't hesitate to reopen this issue if my explanation is unclear #84 (comment)
from phoenix-liveview-counter-tutorial.
@tlietz thank you for opening this issue/question. 👍
on re-reading the section, agree that we should remove it. ✂️
from phoenix-liveview-counter-tutorial.
Related Issues (20)
- Update to Phoenix 1.7.0 HOT 3
- mix: FunctionClauseError: no function clause matching in String.to_charlist/1 HOT 4
- The project's URL leads to a non-existent Heroku app HOT 6
- Feat: Split `counter.ex` into a basic `LiveView` Component HOT 15
- Chore: Tidy `README.md` Screenshots and Markdown Fails HOT 3
- Opening an issue to be closed :) HOT 6
- Distributed version HOT 7
- Images/assets not loading when running the finished app on my localhost (step 0) HOT 1
- Update "Create the App" section HOT 2
- update readme links
- Add Dependabot to Update Dependencies HOT 1
- 2022 Update HOT 5
- Optimizing User Experience with LiveView HOT 1
- Real-Time Form Validation with Phoenix LiveView HOT 1
- TDD with Liveview HOT 1
- update links
- Which pubsub? HOT 1
- Learning components
- typo
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phoenix-liveview-counter-tutorial.