defmodule GenConsumer do @moduledoc """ Documentation for GenConsumer. ## Usage defmodule MyApp.EventHandler do use GenConsumer, otp_app: :my_app @impl true def handle(event, state) do {:ok, state} end end """ @doc false defmacro __using__(opts) do quote bind_quoted: [opts: opts] do use GenServer config = GenConsumer.Config.compile_config(__MODULE__, opts) @config config def __adapter__, do: @config def child_spec(opts) do %{ id: __MODULE__, start: {__MODULE__, :start_link, [opts]}, type: :worker } end def start_link(opts \\ []) do opts = Keyword.merge(@config.opts, opts) name = Keyword.get(opts, :name, __MODULE__) GenServer.start_link(__MODULE__, opts, name: name) end def init(opts) do callback = __MODULE__.handle_event/1 opts = Keyword.put(opts, :callback, callback) |> IO.inspect @config.module.init(opts) end def handle_event(_) do :ok end defoverridable handle_event: 1 end end end