defmodule Tamnoon.Methods do
@moduledoc """
A _methods module_ that provides built-in methods for use in your Tamnoon application.
This module offers utilities for basic state management via `tmnn_get/2` and `tmnn_update/2`,
as well as PubSub functionality through `tmnn_sub/2`, `tmnn_unsub/2`, `tmnn_pub/2` and `subbed_channels/0`.
Most of these methods are primarily intended either to be used internally by Tamnoon,
or to be triggered from Tamnoon HEEx markup.
This module is automatically included as a _methods module_ by Tamnoon.
_Note: In the documentation for the built-in methods, the term "returns" may refer to
either the function's return value or the value included in the server's response to the
client. The context should clarify which is meant._
"""
require Logger
@doc """
Returns the field from the state corresponding to the `:key` specified in the request.
If the key is not present in the state, returns an error string.
#### Example
```html
@mydata
```
In this example, clicking the button triggers the `:get` method with the key `:mydata`,
updating the contents of the `
` element to reflect the current value of `:mydata` in the state.
_Note: In typical usage, method return values should keep the client state in sync automatically,
making manual `:get` calls unnecessary in most cases._
"""
@spec tmnn_get(map(), map()) ::
{field :: map(), [], state :: map()}
| {%{error: error :: String.t()}, [], state :: map()}
def tmnn_get(req, state), do: get(req, state)
@doc """
Updates the value of the field specified by the `"key"` in the request,
setting it to the value provided under the `"value"` key. Returns the updated field value,
or an error string if the field does not exist.
#### Example
```html
Your name is: @name
```
In the example above, each time the `` changes, the `:name` field in the state will
be updated. The updated value will be automatically reflected in the `
` element.
#### Usage with other elements
This method can also be used with elements that don’t have an intrinsic value
(like `