This file contains 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 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