Skip to content

Instantly share code, notes, and snippets.

@thiagofm
Last active January 1, 2016 22:58
Show Gist options
  • Save thiagofm/8213053 to your computer and use it in GitHub Desktop.
Save thiagofm/8213053 to your computer and use it in GitHub Desktop.
defmodule ChocolateMachine do
defrecordp :state, [:sock, :state, :and_so_on]
def start_link
# starts tcp server and saves sock to state
end
def handle_call({:get_chocolate, chocola_name}, from, state(state: :processing, sock: sock))
case :gen_tcp.send(sock, chocolate_name) do
:ok -> :ok
:error -> raise("Gimme chocolate pls")
end
{:reply, :ok, state}
end
def handle_info({:tcp, :processed}, state = s)
{ :noreply, state(s, state: :processed }
end
def get(pid, chocolate_name)
:gen_server.call(pid, {:get_chocolate, chocolate_name}}) do
:ok -> :ok # How do I get state of the current chocolate?
end
end
end
{:ok, pid} = ChocolateMachine.start_link()
ChocolateMachine.get(pid, "hershey's")
# What if the chocolate machine takes 10 seconds to get the chocolate with the :processed state and gives me back the chocolate while it's still processing?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment