Rename Omnibot.Module -> Omnibot.Module.Base, add Omnibot.Module with agent
Previously, all modules were Agents. This renames the Omnibot.Module to be the base module, and the new Omnibot.Module uses an Agent by default. This opens the doors to possibly allowing metamodule supervisors for modules that may be more complex. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
29
lib/module/module.ex
Normal file
29
lib/module/module.ex
Normal file
@@ -0,0 +1,29 @@
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user