Spell.Role behaviour
The Spell.Role
module defines the behaviour of a role in spell.
A role specifies the logic for handling groups of commands. A peer is started with one or more roles, which the peer uses to configure its state and handle its messages.
Callbacks
A module must implement all Spell.Role
behaviour callbacks, though the `use
Spell.Role` directive provides a sane default implementation for each.
get_features/1
init/2
on_open/2
on_close/2
handle_message/3
handle_call/4
Summary↑
call(roles, role, message, from, peer) | From |
collect_features(roles) | Returns a map with the features of the listed roles |
map_handle_message(roles, message, peer) | Call the |
map_init(roles, peer_options) | Call the |
map_on_close(roles, peer) | Call the |
map_on_open(roles, peer) | Call the |
normalize_role_options(roles, acc \\ []) | Normalize a list of role options by wrapping bare |
Types ↑
peer_options :: %{roles: %{features: Map.t}}
Functions
Specs:
- call([{module, any}], module, any, pid, Peer.t) :: {:ok, [{module, any}]} | {:error, :no_role}
From roles
call the role
‘s send_message
function with the message
and the role’s state.
Specs:
- collect_features([{module, any}]) :: Map.t(atom, Map.t)
Returns a map with the features of the listed roles.
Specs:
- map_handle_message([{module, any}], Message.t, Peer.t) :: {:ok, [{module, any}]} | {:error, any}
Call the handle_message
function for a list of roles.
Specs:
- map_init([{module, any}], Keyword.t) :: {:ok, [{module, any}]} | {:error, any}
Call the on_init
function for a list of roles. peer_options
is the
list of options which a peer was initialized with.
Specs:
- map_on_close([{module, any}], Peer.t) :: {:ok, [{module, any}]} | {:error, any}
Call the on_close
function for a list of roles.
Specs:
- map_on_open([{module, any}], Peer.t) :: {:ok, [{module, any}]} | {:error, any}
Call the on_open
function for a list of roles.
Normalize a list of role options by wrapping bare module
items
with an option tuple. If an unexpected role is encountered, an
error tuple is returned.
Callbacks
Specs:
- get_features(options :: Keyword.t) :: {atom, %{}}
Get the key and features that this role announces. Returns nil
if the features announces no features.
Specs:
- handle_call(message :: any, from :: pid, peer :: Peer.t, state :: any) :: {:ok, any, any} | {:error, any}
Handle a call from the peer.
Return values
{:ok, reply, new_state}
: returnreply
{:error, reason}
Specs:
- handle_message(message :: Message.t, peer :: Peer.t, state :: any) :: {:ok, any} | {:error, any}
Handle an incoming WAMP message.
Specs:
- init(peer_options :: peer_options, role_options :: Keyword.t) :: {:ok, any} | {:error, any}
init callback for generating the role’s initial state given options
.
Specs:
- on_close(peer :: Peer.t, state :: any) :: {:ok, any} | {:error, any}
Called when the connection is being closed. Returns the state wrapped in an ok tuple or an error tuple
Specs:
- on_open(peer :: Peer.t, state :: any) :: {:ok, any} | {:error, any}
Called after the connection is opened. Returns the state wrapped in an ok tuple or an error tuple.