View Source Tamnoon.Methods (Tamnoon v0.1.0)

Provides default implementations for methods you need to get your Tamnoon server working - basic state management (with tmnn_get/2 and tmnn_update/2) and basic handling of PubSub channels (via tmnn_sub/2, tmnn_unsub/2, tmnn_pub/2, tmnn_subbed_channels/2)

Using the module

When you use Tamnoon.Methods, every method in this module will be imported for you, letting you conveniently make a methods module. See Tamnoon.MethodManager for details on making one.

Note: in the documentation of the default methods, the term 'returns' is used both to describe the return value of the function and the value of the server's response.

Summary

Functions

Returns the value of the key under the field "key" in the request as an atom. Note: this is NOT a method handler - simply a utility function.

Returns the field with the key specified under the "key" field in the request. Returns an error string if there is no such item.

Will send the request under the "action" key in the request to every client in the channel under the "channel" key.

Subscribes to the channel under the "key" field in the request. If there is no such channel, one will be created (with the client subscribed to it). If the client is already subscribed to the channel nothing will happen.

Returns a list of the channels the client is currently subscribed to.

Unsubscribes from the channel under the "key" in the request.

Updates the value of the item with the key at the "key" field on the request, setting it to the value at the "val" key. Returns a tuple with the updated field and the state, or an error string if there is no such item.

Functions

@spec get_key(map(), map()) :: atom() | nil

Returns the value of the key under the field "key" in the request as an atom. Note: this is NOT a method handler - simply a utility function.

@spec tmnn_get(map(), map()) ::
  {field :: map(), state :: map()}
  | {%{error: error :: String.t()}, state :: map()}

Returns the field with the key specified under the "key" field in the request. Returns an error string if there is no such item.

@spec tmnn_pub(map(), map()) :: {%{pub: :ok}, state :: map()}

Will send the request under the "action" key in the request to every client in the channel under the "channel" key.

@spec tmnn_sub(map(), map()) :: {%{sub: :ok}, state :: map()}

Subscribes to the channel under the "key" field in the request. If there is no such channel, one will be created (with the client subscribed to it). If the client is already subscribed to the channel nothing will happen.

Link to this function

tmnn_subbed_channels(req, state)

View Source
@spec tmnn_subbed_channels(map(), map()) ::
  {%{subbed_channels: channels :: [String.t()]}, state :: map()}

Returns a list of the channels the client is currently subscribed to.

@spec tmnn_unsub(map(), map()) ::
  {%{unsub: :ok}, state :: map()}
  | {%{error: error :: String.t()}, state :: map()}

Unsubscribes from the channel under the "key" in the request.

@spec tmnn_update(map(), map()) ::
  {new_field :: map(), state :: map()}
  | {%{error: error :: String.t()}, state :: map()}

Updates the value of the item with the key at the "key" field on the request, setting it to the value at the "val" key. Returns a tuple with the updated field and the state, or an error string if there is no such item.