-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
13 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,19 @@ | ||
defmodule Crawler.Store.Counter do | ||
use GenServer | ||
use Agent | ||
|
||
def inc(pid) when is_pid(pid) do | ||
GenServer.call(pid, :inc) | ||
def start_link(_args) do | ||
Agent.start_link(fn -> 0 end, name: __MODULE__) | ||
end | ||
|
||
def count(pid) when is_pid(pid) do | ||
GenServer.call(pid, :count) | ||
def value do | ||
Agent.get(__MODULE__, & &1) | ||
end | ||
|
||
def reset(pid) when is_pid(pid) do | ||
GenServer.call(pid, :reset) | ||
def inc do | ||
Agent.update(__MODULE__, &(&1 + 1)) | ||
end | ||
|
||
def start_link(opts \\ []) do | ||
GenServer.start_link(__MODULE__, 0, opts) | ||
end | ||
|
||
def init(args) do | ||
{:ok, args} | ||
end | ||
|
||
def handle_call(:inc, _from, state) do | ||
count = state + 1 | ||
{:reply, count, count} | ||
end | ||
|
||
def handle_call(:count, _from, state) do | ||
{:reply, state, state} | ||
end | ||
|
||
def handle_call(:reset, _from, _state) do | ||
{:reply, 0, 0} | ||
def reset do | ||
Agent.update(__MODULE__, fn _ -> 0 end) | ||
end | ||
end |