Skip to content

Instantly share code, notes, and snippets.

@mattSpell
Created December 11, 2015 01:38
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 mattSpell/2aa6711dfd1590c49ce4 to your computer and use it in GitHub Desktop.
Save mattSpell/2aa6711dfd1590c49ce4 to your computer and use it in GitHub Desktop.
elixir GenServer issue
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