Skip to content

Commit

Permalink
add v0.9.0 snapshot of lib/live_view_counter_web/live/counter.ex for …
Browse files Browse the repository at this point in the history
…step 13 #5
  • Loading branch information
nelsonic committed Mar 10, 2020
1 parent eddd8a7 commit f2df2dd
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/live_view_counter_web/live/counter.ex
@@ -1,17 +1,28 @@
defmodule LiveViewCounterWeb.Counter do
use Phoenix.LiveView

def mount(_params, _session, socket) do
@topic "live"

def mount(_session, _params, socket) do
LiveViewCounterWeb.Endpoint.subscribe(@topic) # subscribe to the channel
{:ok, assign(socket, :val, 0),
layout: {LiveViewCounterWeb.LayoutView, "app.html"}}
end

def handle_event("inc", _, socket) do
{:noreply, update(socket, :val, &(&1 + 1))}
def handle_event("inc", _value, socket) do
new_state = update(socket, :val, &(&1 + 1))
LiveViewCounterWeb.Endpoint.broadcast_from(self(), @topic, "inc", new_state.assigns)
{:noreply, new_state}
end

def handle_event("dec", _, socket) do
{:noreply, update(socket, :val, &(&1 - 1))}
new_state = update(socket, :val, &(&1 - 1))
LiveViewCounterWeb.Endpoint.broadcast_from(self(), @topic, "dec", new_state.assigns)
{:noreply, new_state}
end

def handle_info(msg, socket) do
{:noreply, assign(socket, val: msg.payload.val)}
end

def render(assigns) do
Expand Down

0 comments on commit f2df2dd

Please sign in to comment.