Polish off wordbot implementation, add Omnibot.Module.GenServer

Wordbot implementation now uses the new Omnibot.Module.GenServer module,
which uses a GenServer instead of an Agent. This way, the module can
receive messages and makes storage a little easier.

Beyond that, minor changes all around.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-07-02 16:23:38 -07:00
parent 67ba7a5847
commit 7f8a886550
8 changed files with 222 additions and 22 deletions

View File

@@ -1,15 +1,12 @@
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
@@ -18,7 +15,7 @@ defmodule Omnibot.Module.Agent do
def update_state(update, timeout \\ 5000),
do: Module.Agent.update_state(__MODULE__, update, timeout)
end
end
end
def start_link(cfg, state, opts) do

View File

@@ -44,7 +44,6 @@ defmodule Omnibot.Module.Base do
defp route_msg(irc, msg) do
nick = msg.prefix.nick
case String.upcase(msg.command) do
"PRIVMSG" ->
[channel | params] = msg.params

50
lib/module/gen_server.ex Normal file
View File

@@ -0,0 +1,50 @@
defmodule Omnibot.Module.GenServer do
defmacro __using__([]) do
quote do
alias Omnibot.Module
use GenServer
def start_link(opts) do
cfg = opts[:cfg]
state = opts[:state]
GenServer.start_link(__MODULE__, {cfg, state}, opts)
end
def cfg() do
GenServer.call(__MODULE__, :cfg)
end
def state() do
GenServer.call(__MODULE__, :state)
end
def update_state(update) do
GenServer.cast(__MODULE__, {:state, update})
end
## Server callbacks
@impl GenServer
def init({cfg, state}) do
state = state || on_init(cfg)
{:ok, {cfg, state}}
end
@impl GenServer
def handle_call(:cfg, _from, {cfg, state}) do
{:reply, cfg, {cfg, state}}
end
@impl GenServer
def handle_call(:state, _from, {cfg, state}) do
{:reply, state, {cfg, state}}
end
@impl GenServer
def handle_cast({:state, update}, {cfg, state}) do
{:noreply, {cfg, apply(update, [state])}}
end
end
end
end