index d362431..8cf71aa 100644 index d362431..8cf71aa 100644 --- a/apps/hello_web/lib/hello_live.ex --- a/apps/hello_web/lib/hello_live.ex +++ b/apps/hello_web/lib/hello_live.ex +++ b/apps/hello_web/lib/hello_live.ex @@ -4,16 +4,27 @@ defmodule HelloWeb.HelloLive do @@ -4,16 +4,27 @@ defmodule HelloWeb.HelloLive do def render(assigns) do def render(assigns) do ~L""" ~L""" Count: <%= @count %> Count: <%= @count %> + <button phx-click="reverse">リバ!</button> + <button phx-click="reverse">リバ!</button> """ """ end end def mount(%{}, socket) do def mount(%{}, socket) do if connected?(socket), do: :timer.send_interval(1000, self(), :update) if connected?(socket), do: :timer.send_interval(1000, self(), :update) - {:ok, assign(socket, :count, 0)} - {:ok, assign(socket, :count, 0)} + socket = + socket = + socket + socket + |> assign(:count, 0) + |> assign(:count, 0) + |> assign(:diff, 1) + |> assign(:diff, 1) + {:ok, socket} + {:ok, socket} + end + end + + + def handle_event("reverse", _value, socket) do + def handle_event("reverse", _value, socket) do + diff = socket.assigns.diff + diff = socket.assigns.diff + {:noreply, assign(socket, :diff, -diff)} + {:noreply, assign(socket, :diff, -diff)} end end def handle_info(:update, socket) do def handle_info(:update, socket) do count = socket.assigns.count count = socket.assigns.count - {:noreply, assign(socket, :count, count + 1)} - {:noreply, assign(socket, :count, count + 1)} + diff = socket.assigns.diff + diff = socket.assigns.diff + {:noreply, assign(socket, :count, count + diff)} + {:noreply, assign(socket, :count, count + diff)} end end end end \ No newline at end of file \ No newline at end of file