From 1105d6c0e12e50b3bf7ef27a67790131497e8c2a Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Mon, 6 Jul 2020 17:12:35 -0700 Subject: [PATCH] Remove Plugin.Agent, replace everywhere with Plugin.GenServer Being able to handle arbitrary messages from other processes is a generally useful thing to have for fine-grained control of the plugin. Agents don't provide that, while GenServers do. I've removed all instances of the Agent and replaced it with the GenServer and everything appears fine so far. Signed-off-by: Alek Ratzloff --- lib/plugin/agent.ex | 40 ---------------------------------------- lib/plugin/plugin.ex | 2 +- 2 files changed, 1 insertion(+), 41 deletions(-) delete mode 100644 lib/plugin/agent.ex diff --git a/lib/plugin/agent.ex b/lib/plugin/agent.ex deleted file mode 100644 index e55eef8..0000000 --- a/lib/plugin/agent.ex +++ /dev/null @@ -1,40 +0,0 @@ -defmodule Omnibot.Plugin.Agent do - defmacro __using__([]) do - quote do - alias Omnibot.Plugin - use Agent - - def start_link(opts) do - cfg = opts[:cfg] - state = opts[:state] || on_init(cfg) - Plugin.Agent.start_link(cfg, state, opts) - end - - def cfg, do: Plugin.Agent.cfg(__MODULE__) - def state, do: Plugin.Agent.state(__MODULE__) - - def update_state(update, timeout \\ 5000), - do: Plugin.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/plugin/plugin.ex b/lib/plugin/plugin.ex index a80abd6..b90c06c 100644 --- a/lib/plugin/plugin.ex +++ b/lib/plugin/plugin.ex @@ -2,7 +2,7 @@ defmodule Omnibot.Plugin do defmacro __using__([]) do quote do use Omnibot.Plugin.Base - use Omnibot.Plugin.Agent + use Omnibot.Plugin.GenServer end end end