diff --git a/lib/module/agent.ex b/lib/module/agent.ex new file mode 100644 index 0000000..a023f76 --- /dev/null +++ b/lib/module/agent.ex @@ -0,0 +1,43 @@ +defmodule Omnibot.Module.Agent do + defmacro __using__([]) do + quote do + import Omnibot.Module.Agent + alias Omnibot.Module + use Agent + + def start_link(opts) do + cfg = opts[:cfg] + state = opts[:state] || on_init(cfg) + + IO.inspect({__MODULE__, state}) + Module.Agent.start_link(cfg, state, opts) + end + + def cfg, do: Module.Agent.cfg(__MODULE__) + def state, do: Module.Agent.state(__MODULE__) + + def update_state(update, timeout \\ 5000), + do: Module.Agent.update_state(__MODULE__, update, timeout) + end + end + + def start_link(cfg, state, opts) do + Agent.start_link(fn -> {cfg, state} end, opts) + end + + def cfg(agent) do + Agent.get(agent, fn {cfg, _} -> cfg end) + end + + def state(agent) do + Agent.get(agent, fn {_, state} -> state end) + end + + def update_state(agent, update, timeout \\ 5000) do + Agent.update( + agent, + fn {cfg, state} -> {cfg, apply(update, [state])} end, + timeout + ) + end +end diff --git a/lib/module/base.ex b/lib/module/base.ex index 403a5a7..8e0aaea 100644 --- a/lib/module/base.ex +++ b/lib/module/base.ex @@ -32,7 +32,6 @@ defmodule Omnibot.Module.Base do quote do alias Omnibot.{Irc, Module} import Omnibot.Module.Base - require Logger @behaviour Module.Base diff --git a/lib/module/module.ex b/lib/module/module.ex index bb5d6a4..151170d 100644 --- a/lib/module/module.ex +++ b/lib/module/module.ex @@ -2,28 +2,7 @@ defmodule Omnibot.Module do defmacro __using__([]) do quote do use Omnibot.Module.Base - use Agent - - def start_link(opts) do - cfg = opts[:cfg] - Agent.start_link(fn -> {cfg, on_init(cfg)} end, opts ++ [name: __MODULE__]) - end - - def cfg do - Agent.get(__MODULE__, fn {cfg, _} -> cfg end) - end - - def state do - Agent.get(__MODULE__, fn {_, state} -> state end) - end - - def update_state(update, timeout \\ 5000) do - Agent.update( - __MODULE__, - fn {cfg, state} -> {cfg, apply(update, [state])} end, - timeout - ) - end + use Omnibot.Module.Agent end end end