View Source Tamnoon.MethodManager (Tamnoon v1.0.0-a.4)

This module handles the management of different methods as you create them. Notably, it provides the defmethod/2 macro.

Importing the module

In order to create handlers for the methods you set up, you must import Tamnoon.MethodManager in your methods module. Then, you can use the defmethod/2 macro to implement handling of the methods.

Summary

Functions

Defines a function named tmnn_[name]. Functions with this prefix in your methods module will automatically be added to the possible methods when invoking route_request/3. Inside the created function, you can access the state of the client and the request object by the state and req variables respectively. Method handlers must return a tuple of {return_value, new_state}.

Returns a tuple containing the diffs and the new state after applying the diffs. Can be used in method handlers to update a value easily.

The function used by Tamnoon.SocketHandler.websocket_handle/2 to route the requests to the appropriate method handler.

Triggers a method with the given name and payload. An additional timeout can be specified to delay the triggering of the method.

Functions

Link to this macro

defmethod(name, list)

View Source (macro)

Defines a function named tmnn_[name]. Functions with this prefix in your methods module will automatically be added to the possible methods when invoking route_request/3. Inside the created function, you can access the state of the client and the request object by the state and req variables respectively. Method handlers must return a tuple of {return_value, new_state}.

Example

defmethod :get do
  key = get_key(req, state)
  if (key != nil) do
    {state[key], state}
  else
    {"Error: no matching key", state}
  end
end
@spec diff(map(), map()) :: {map(), map()}

Returns a tuple containing the diffs and the new state after applying the diffs. Can be used in method handlers to update a value easily.

Example

defmethod :change_something do
  diffs = %{some_key: "New value", another_key: "Another value"}

  diff(diffs, state)
end
Link to this function

route_request(methods_modules, payload, state)

View Source
@spec route_request([module()], map(), map()) ::
  {:reply, {:text, return_value :: String.t()}, new_state :: map()}

The function used by Tamnoon.SocketHandler.websocket_handle/2 to route the requests to the appropriate method handler.

Link to this function

trigger_method(method_name, payload, timeout \\ 0)

View Source
@spec trigger_method(atom(), map(), non_neg_integer()) ::
  :ok | :noconnect | :nosuspend | reference()

Triggers a method with the given name and payload. An additional timeout can be specified to delay the triggering of the method.