Created
December 11, 2015 01:38
-
-
Save mattSpell/2aa6711dfd1590c49ce4 to your computer and use it in GitHub Desktop.
elixir GenServer issue
This file contains hidden or 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
defmodule MyApp.MainWorker do | |
use GenServer | |
# Client | |
def start_link(default) do | |
GenServer.start_link(__MODULE__, default) | |
end | |
def add(pid, num) do | |
GenServer.call(pid, {:add, num}) | |
end | |
def subtract(pid, num) do | |
GenServer.call(pid, {:subtract, num}) | |
end | |
def get_state(pid) do | |
GenServer.call(pid, :get_state) | |
end | |
# Server (callbacks) | |
def handle_call({:add, num}, state) do | |
{:reply, num + state} | |
end | |
def handle_call({:subtract, num}, state) do | |
{:reply, num - state} | |
end | |
def handle_call(:get_state, _from, current_state) do | |
{:reply, current_state, current_state} | |
end | |
def handle_call(request, from, state) do | |
super(request, from, state) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment