Further separate module agents and module impl

Add Omnibot.Module.Agent which implements the basic agent cfg + state
persistence functionality, which is then included in the Omnibot.Module.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-06-14 18:46:19 -04:00
parent 0b01f3524e
commit 871b7771fe
3 changed files with 44 additions and 23 deletions

View File

@@ -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