Counters work, now for pubsub
This commit is contained in:
parent
b35d8687a0
commit
48a4c97a02
1 changed files with 19 additions and 5 deletions
24
main.ex
24
main.ex
|
@ -1,4 +1,6 @@
|
|||
Application.put_env(:sample, Sfe.Endpoint,
|
||||
counter = :counters.new(1, [])
|
||||
|
||||
Application.put_env(:sfe, Sfe.Endpoint,
|
||||
http: [ip: {127, 0, 0, 1}, port: 5001],
|
||||
adapter: Bandit.PhoenixAdapter,
|
||||
server: true,
|
||||
|
@ -6,6 +8,8 @@ Application.put_env(:sample, Sfe.Endpoint,
|
|||
secret_key_base: String.duplicate("a", 64)
|
||||
)
|
||||
|
||||
Application.put_env(:sfe, Sfe.HomeLive, counter_ref: counter)
|
||||
|
||||
Mix.install([
|
||||
{:bandit, "~> 1.2"},
|
||||
{:jason, "~> 1.0"},
|
||||
|
@ -14,6 +18,7 @@ Mix.install([
|
|||
{:phoenix_pubsub, "~> 2.1"}
|
||||
])
|
||||
|
||||
|
||||
defmodule Sfe.ErrorView do
|
||||
def render(template, _), do: Phoenix.Controller.status_message_from_template(template)
|
||||
end
|
||||
|
@ -21,8 +26,11 @@ end
|
|||
defmodule Sfe.HomeLive do
|
||||
use Phoenix.LiveView, layout: {__MODULE__, :live}
|
||||
|
||||
defp counter_ref(), do: Application.get_env(:sfe, __MODULE__)[:counter_ref]
|
||||
defp counter_value(), do: :counters.get(counter_ref(), 1)
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :count, 0)}
|
||||
{:ok, assign(socket, :count, counter_value())}
|
||||
end
|
||||
|
||||
defp phx_vsn, do: Application.spec(:phoenix, :vsn)
|
||||
|
@ -53,11 +61,17 @@ defmodule Sfe.HomeLive do
|
|||
end
|
||||
|
||||
def handle_event("inc", _params, socket) do
|
||||
{:noreply, assign(socket, :count, socket.assigns.count + 1)}
|
||||
dbg(:counters.add(counter_ref(), 1, 1))
|
||||
counter_value(socket)
|
||||
end
|
||||
|
||||
def handle_event("dec", _params, socket) do
|
||||
{:noreply, assign(socket, :count, socket.assigns.count - 1)}
|
||||
dbg(:counters.sub(counter_ref(), 1, 1))
|
||||
counter_value(socket)
|
||||
end
|
||||
|
||||
defp counter_value(socket) do
|
||||
{:noreply, assign(socket, :count, counter_value())}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -77,7 +91,7 @@ defmodule Sfe.Router do
|
|||
end
|
||||
|
||||
defmodule Sfe.Endpoint do
|
||||
use Phoenix.Endpoint, otp_app: :sample
|
||||
use Phoenix.Endpoint, otp_app: :sfe
|
||||
socket("/live", Phoenix.LiveView.Socket)
|
||||
plug(Sfe.Router)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue