Last active
March 24, 2018 13:19
-
-
Save suhanlee/66cb067ee339d08fbcfa838a1081b34b to your computer and use it in GitHub Desktop.
TodoServer
This file contains hidden or 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
Process.register(self, :some_name) | |
send(:some_name, :msg) | |
receive do | |
msg -> IO.puts "received #{msg}" | |
end |
This file contains hidden or 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 TodoServer do | |
def start do | |
spawn(fn -> loop(TodoList.new) end) | |
end | |
def add_entry(todo_server, new_entry) do | |
send(todo_server, {:add_entry, new_entry}) | |
end | |
defp process_message(todo_list, {:add_entry, new_entry}) do | |
TodoList.add_entry(todo_list, new_entry) | |
end | |
def entries(todo_server, date) do | |
send(todo_server, {:entries, self, date}) | |
receive do | |
{:todo_entries, entries} -> entires | |
after 5000 -> | |
{:error, :timeout} | |
end | |
end | |
defp process_message(todo_list, {:entries, caller, date}) do | |
send(caller, {:todo_entries, TodoList.entries(todo_list, date)}) | |
todo_list | |
end | |
defp loop(todo_list) do | |
new_todo_list = receive do | |
message -> | |
process_message(todo_list, message) | |
end | |
loop(new_todo_list) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment