feat: add support for registering server pid

This commit is contained in:
Moritz Böhme 2024-08-03 09:44:00 +02:00
parent 4998e0ac5b
commit 09ae0c9bb3
Signed by: moritz
GPG key ID: 970C6E89EB0547A9

View file

@ -1,6 +1,12 @@
defmodule TodoServer do defmodule TodoServer do
def start(entries \\ []) do def start(entries \\ [], options \\ []) do
spawn(fn -> loop(TodoList.new(entries)) end) pid = spawn(fn -> loop(TodoList.new(entries)) end)
if Keyword.get(options, :local) do
Process.register(pid, :todo_list)
end
pid
end end
def from_list(todo_list) do def from_list(todo_list) do
@ -50,6 +56,10 @@ defmodule TodoServer do
nil nil
end end
def add(entry) do
add(:todo_list, entry)
end
def entries(pid, date) do def entries(pid, date) do
send(pid, {:entries, self(), date}) send(pid, {:entries, self(), date})
@ -61,15 +71,27 @@ defmodule TodoServer do
end end
end end
def entries(date) do
entries(:todo_list, date)
end
def update(pid, id, update_fun) do def update(pid, id, update_fun) do
send(pid, {:update, id, update_fun}) send(pid, {:update, id, update_fun})
nil nil
end end
def update(id, update_fun) do
update(:todo_list, id, update_fun)
end
def delete(pid, id) do def delete(pid, id) do
send(pid, {:delete, id}) send(pid, {:delete, id})
nil nil
end end
def delete(id) do
delete(:todo_list, id)
end
end end
defmodule TodoList do defmodule TodoList do