Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule Mix.TasksServer do
@moduledoc false
use GenServer.Behaviour
def start_link() do
:gen_server.start_link({ :local, __MODULE__ }, __MODULE__, :ok, [])
end
def clear_tasks() do
call :clear_tasks
end
def run_task(task, proj) do
call { :run_task, task, proj }
end
def put_task(task, proj) do
cast { :put_task, task, proj }
end
def delete_task(task, proj) do
cast { :delete_task, task, proj }
end
defp call(arg) do
:gen_server.call(__MODULE__, arg, 30_000)
end
defp cast(arg) do
:gen_server.cast(__MODULE__, arg)
end
## Callbacks
def init(:ok) do
{ :ok, HashSet.new }
end
def handle_call(:clear_tasks, _from, set) do
{ :reply, set, HashSet.new }
end
def handle_call({ :run_task, task, proj }, _from, set) do
item = { task, proj }
{ :reply, not(item in set), Set.put(set, item) }
end
def handle_call(request, from, config) do
super(request, from, config)
end
def handle_cast({ :put_task, task, proj }, set) do
{ :noreply, Set.put(set, { task, proj }) }
end
def handle_cast({ :delete_task, task, proj }, set) do
{ :noreply, Set.delete(set, { task, proj }) }
end
def handle_cast(request, config) do
super(request, config)
end
end
defmodule Mix.TasksServer do
@moduledoc false
def start_link() do
Agent.start_link(fn -> HashSet.new end, local: __MODULE__)
end
def clear_tasks() do
get_and_update fn set ->
{ set, HashSet.new }
end
end
def run_task(task, proj) do
item = { task, proj }
get_and_update fn set ->
{ not(item in set), Set.put(set, item) }
end
end
def put_task(task, proj) do
update &Set.put(&1, { task, proj })
end
def delete_task(task, proj) do
update &Set.delete(&1, { task, proj })
end
defp get_and_update(fun) do
Agent.get_and_update(__MODULE__, fun, 30_000)
end
defp update(fun) do
Agent.update(__MODULE__, fun)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.