defmodule DemoServer do
use GenServer
- @vsn "1"
+ @vsn "2"
## Client API
def start_link employee do
GenServer.start_link __MODULE__, employee, []
end
def add_money(pid, value) do
GenServer.call(pid, {:add, value})
end
## Server API
def init(employee) do # initiating state with passed employee details
{:ok, employee}
end
# add the value to the state and returns :ok
- def handle_call({:add, value},_from, %{name: name, money: money} = state) do
+ def handle_call({:add, value},_from, %{name: name, salary: salary} = state) do
- {:reply, "#{value} added to #{name} ", Map.put(state, :money, money+value)}
+ new_state = Map.put(state, :salary, salary+value)
+ {:reply, new_state, new_state}
end
+ def code_change("1", %{name: name, money: money} = old_state, _dontcare) do
+ {:ok, %{name: name, salary: money}}
+ end
end
Last active
December 24, 2018 10:33
-
-
Save blackode/ae01f05783182ec3d5ecd762253bcb04 to your computer and use it in GitHub Desktop.
demo_server version 2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment