Skip to content

Instantly share code, notes, and snippets.

@zkessin
Created May 10, 2019 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkessin/f445fe363d4c5536232efd4a76d0a3c9 to your computer and use it in GitHub Desktop.
Save zkessin/f445fe363d4c5536232efd4a76d0a3c9 to your computer and use it in GitHub Desktop.
defmodule Counter do
use GenServer
def increment() do
GenServer.cast(__MODULE__, :increment)
end
def decrement() do
GenServer.cast(__MODULE__, :decrement)
end
def value() do
GenServer.call(__MODULE__, :value)
end
def start_link(initial_value \\ 0) do
GenServer.start_link(__MODULE__, initial_value, name: __MODULE__)
end
def init(initial_value) do
{:ok, initial_value}
end
def handle_cast(:increment, val) do
{:noreply, val + 1}
end
def handle_cast(:decrement, val) do
{:noreply, val - 1}
end
def handle_call(:value, _from, val) do
{:reply, val, val}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment