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