Skip to content

Instantly share code, notes, and snippets.

@blackode
Last active November 2, 2017 20:57
Show Gist options
  • Save blackode/dbe29e09b9e480b1c9dfe4f53ddda531 to your computer and use it in GitHub Desktop.
Save blackode/dbe29e09b9e480b1c9dfe4f53ddda531 to your computer and use it in GitHub Desktop.
GenServer in Elixir Server Callbacks
def handle_call({:unlock, password}, _from, passwords) do # ----> synchronous request
if password in passwords do
{:reply, :ok, passwords}
else
write_to_logfile password
{:reply, {:error,"wrongpassword"}, passwords}
end
end
def handle_call({:reset, {old_password,new_password}}, _from, passwords) do
if old_password in passwords do
new_state = List.delete(passwords,old_password)
{:reply, :ok, [new_password | new_state]}
else
write_to_logfile new_password
{:reply, {:error,"wrongpassword"}, passwords}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment