lecture 04
This commit is contained in:
22 files changed
+428
No files matched your search
@@ -0,0 +1,18 @@
|
||||
defmodule Counter do
|
||||
@moduledoc """
|
||||
Documentation for `Counter`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Hello world.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Counter.hello()
|
||||
:world
|
||||
|
||||
"""
|
||||
def hello do
|
||||
:world
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
defmodule Counter.Application do
|
||||
# See https://hexdocs.pm/elixir/Application.html
|
||||
# for more information on OTP Applications
|
||||
@moduledoc false
|
||||
|
||||
use Application
|
||||
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
# Starts a worker by calling: Counter.Worker.start_link(arg)
|
||||
{Counter.Store, "counter.db"}, # we must start the store before the worker
|
||||
{Counter.Worker, 0}
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: Counter.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
defmodule Counter.Store do
|
||||
use GenServer
|
||||
|
||||
# client
|
||||
def start_link(file) do
|
||||
GenServer.start_link(__MODULE__, file, name: __MODULE__)
|
||||
end
|
||||
|
||||
def put(value) do
|
||||
GenServer.cast(__MODULE__, {:put, value})
|
||||
end
|
||||
|
||||
def get() do
|
||||
GenServer.call(__MODULE__, :get)
|
||||
end
|
||||
|
||||
# server
|
||||
@impl true
|
||||
def init(file) do
|
||||
{:ok, file}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call(:get, _from, file) do
|
||||
reply =
|
||||
case File.read(file) do
|
||||
{:ok, content} -> :erlang.binary_to_term(content)
|
||||
_ -> nil
|
||||
end
|
||||
{:reply, reply, file}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_cast({:put, value}, file) do
|
||||
File.write(file, :erlang.term_to_binary(value))
|
||||
{:noreply, file}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
defmodule Counter.Worker do
|
||||
use GenServer
|
||||
|
||||
@store Counter.Store
|
||||
|
||||
# client
|
||||
def start_link(n \\ 0) do
|
||||
GenServer.start_link(__MODULE__, n, name: __MODULE__)
|
||||
end
|
||||
|
||||
def inc(amt \\ 1) do
|
||||
GenServer.cast(__MODULE__, {:inc, amt})
|
||||
end
|
||||
|
||||
def value() do
|
||||
GenServer.call(__MODULE__, :value)
|
||||
end
|
||||
|
||||
# server
|
||||
@impl true
|
||||
def init(arg) do
|
||||
{:ok, @store.get() || arg}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call(:value, _from, state) do
|
||||
{:reply, state, state}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_cast({:inc, amt}, state) do
|
||||
{:noreply, state + amt}
|
||||
end
|
||||
|
||||
# for demonstration only
|
||||
@impl true
|
||||
def handle_info(:reset, _state) do
|
||||
{:noreply, 0}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def terminate(_reason, state) do
|
||||
@store.put(state)
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user