Raxol.Gateway.Handler behaviour (RaxolGateway v0.2.0)

Copy Markdown View Source

The per-chat logic a Raxol.Gateway.Session runs.

A handler is initialized once per chat with its route, then handles each inbound event, optionally returning a rendered message to deliver back. Any module satisfying init/2 and handle_event/2 works; Raxol.Gateway.Handler.Lifecycle (a full TEA app under environment: :gateway) ships with the package.

The optional terminate/2 runs when the session stops cleanly (idle timeout, SessionRouter.stop_session/2, supervisor shutdown, explicit stop; the session traps exits so supervisor-driven stops reach it) -- the hook for a handler that owns linked processes, since a session's :normal exit does not propagate over links. It is best-effort: a brutally killed session never reaches it.

Summary

Types

event()

@type event() :: term()

rendered()

@type rendered() :: term()

state()

@type state() :: term()

Callbacks

handle_event(event, state)

@callback handle_event(event(), state()) ::
  {:reply, rendered(), state()} | {:noreply, state()}

init(t, keyword)

@callback init(Raxol.Gateway.Route.t(), keyword()) :: {:ok, state()} | {:error, term()}

terminate(reason, state)

(optional)
@callback terminate(reason :: term(), state()) :: term()