Variables
Variables can be used to store data in memory without persisting it. Variables are solely stored in the application memory and are therefore only available during the lifetime of the application. When the app is killed, all previously stored variables are also deleted. A variable is a user-specified key-value pair that can be referenced via the key.
In mos. the scope of all variables is global, i.e. all variables can be accessed from anywhere. Variables do not have to be declared. Assigning a variable with the setVariable
action declares and assigns the variable.
Usage
This section explains how to create (declare/assign) and use (reference) variables in mos.
Variable declaration and assignment
To create a variable, the setVariable
action can be used as follows:
{
"type": "setVariable",
"params": {
"variable": "foo",
"value": "bar"
}
}
Firing this action leads to the creation of a variable with the name foo
and the value bar
.
Using variables
Typically, variables are used within evaluables. Here is an example of how a variable can be bound to a text layer value:
{
"type": "text",
"value": {
"context": "variable",
"field": "foo"
}
}
In this example, the text layer always displays the value of the variable foo
. Setting the variable to a different value automatically updates the text layer and its UI.
To reference a variable in another action, the dynamicParams of this action can be used. The following example shows how to send a variable to a backend with a POST request:
{
"type": "request",
"dynamicParams": {
"fooVariable": {
"context": "variable",
"field": "foo"
}
},
"leadingDelimiter": "{&",
"trailingDelimiter": "&}",
"params": {
"url": "https://non-existing-example.com/route",
"method": "POST",
"post": {
"foo": "{&fooVariable&}"
}
}
}
When this action is executed, mos. first retrieves the dynamic parameters and allows the action to use these parameters in the action params. Considering that the value of our variable foo
has been set to bar
. The resulting request body will look like this:
{
"foo": "bar"
}
Destroying variables
To remove a variable, you can use the setVariable action and pass null
in its value field:
{
"type": "setVariable",
"params": {
"variable": "foo",
"value": null
}
}
This sets the value of the variable to null
, which effectively removes it from the in-memory storage.