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