Skip to content

Instantly share code, notes, and snippets.

@Ayoush
Last active August 25, 2020 05:48
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 Ayoush/6fa91505e056b13cdbc48f307617afa7 to your computer and use it in GitHub Desktop.
Save Ayoush/6fa91505e056b13cdbc48f307617afa7 to your computer and use it in GitHub Desktop.
defmodule OtpConceptsProcesses do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, :ok, [])
end
def init(:ok) do
{:ok, %{}}
end
def add(pid, value) do
GenServer.cast(pid, {:add, value})
end
def remove(pid, value) do
GenServer.cast(pid, {:remove, value})
end
def value(pid) do
GenServer.call(pid, :final)
end
def handle_cast({:add, value}, state) do
new_state = Map.put(state, :add, value)
{:noreply, new_state}
end
def handle_cast({:remove, value}, state) do
new_state = Map.put(state, :remove, value)
{:noreply, new_state}
end
def handle_call(:final, _from, state) do
{:reply, state, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment