Skip to main content

Actions & Triggers

Actions and Triggers are the yin and yang of a Node, the push and pull of it. Actions are used to perform some behavior on a Node, to make it do something that the Node is capable of doing. Triggers are used to announce when a Node changes in a noteworthy way that in which other Nodes would be interested. Together Actions and Triggers work together to announce changes and respond to those changes.

Actions

An Action is a Property with a datatype of action. Actions are code that run when activated. They can be activated manually, or by being connected to a Trigger.

The code associated with an action is a function that is set as a value to that action, as shown here:

properties.yaml
someAction:
datatype: action
src./main.js
context.set('someAction', ()=>{
// ... code to be executed on action invocation ...
});

Setup an action

To bind a function to an action property, simply call context.set() as shown here:

src./main.js
context.set('someAction', ()=>{
// ... code to be executed on action invocation ...
});

Example

In this example, the Generate Random action can be clicked on and then the Run Action button can be clicked. This will cause a new random number to be generated and set into the ((randomNumber)) property.

Run an action

One can programmatically execute an action using he context.run() method to execute an Action Property. Since we are developing the node, calling run may seem strange, but it is useful for testing and a few edge cases.

main.js
context.run('send_email');

Additionally, the concept of running an action makes more sense when you consider the need to execute an action on another instance's context, instead of the context of the node you are writing.

main.js
const instance = context.getInstance('9a3b8b8d-18e2-4951-a7cc-de8ff5dd42fd');
instance.run('send_sms');

Triggers

A Trigger is a Property that is used to notify connected instances of some event. An event can be anything you want it to be. Events are typically due to some change in the Node like a mouse click or a photo taken. It depends on the Node and the use casebu t is entirely up to you when to use a trigger.

Typically we call the execution of a Trigger a "firing". So we tend to say "fire a trigger" to describe this process.

Triggers my be connected to Actions such that when the connected Trigger fires, the connected Action is run.

Firing a Trigger

To fire off a Trigger property, simply call the context.trigger() function as shown here...

main.js
context.trigger('some_trigger');

Example

In this example, the defined trigger ((my_trigger)) is executed once every five seconds, causing a new random number to be set in the ((randomNumber)) property.

main.js
// listens for the trigger ((my_trigger)) to be triggered and updates ((randomRumber))
// with a new random number when it is.
context.subscribe('my_trigger',() => {
context.set('randomNumber', Math.round(Math.random() * 100))
});

// Fires the trigger every 5 seconds.
setInterval(() => {
context.trigger('my_trigger'); // Fire a trigger every 5 seconds
}, 5000);
`}