Coder Social home page Coder Social logo

Comments (3)

SimonLab avatar SimonLab commented on May 12, 2024 2

Currently the counter value is indeed updated twice.

The first time when the incr/decr event is trigger by the user:

def handle_event("inc", _, socket) do
{:noreply, assign(socket, :val, Count.incr())}
end
def handle_event("dec", _, socket) do
{:noreply, assign(socket, :val, Count.decr())}
end

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:

defp make_change(count, change) do
new_count = count + change
PubSub.broadcast(LiveViewCounter.PubSub, topic(), {:count, new_count})
{:reply, new_count, new_count}
end

The :count event is handle with:

def handle_info({:count, count}, socket) do
{:noreply, assign(socket, val: count)}
end

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.

SimonLab avatar SimonLab commented on May 12, 2024 1

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.

nelsonic avatar nelsonic commented on May 12, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.