Spanish Total.js
English
Documentation For AI agents
Contents

Triggers, timers and closing

Start messages from a button or a timer, and clean up when a node is removed or changed.

.md GitHub
On this page

Some components start messages themselves: when the user selects a button, at an interval, or when something happens on the server. This page builds Heartbeat, which sends the current date and time at an interval and has a Send now button.

The trigger button

A <button name="trigger"> in the <body> of the component calls instance.trigger on the server:

<body>
    <header>
        <i class="$ICON"></i>$NAME
    </header>
    <footer style="padding:0 10px 10px">
        <button name="trigger" style="width:100%">Send now</button>
    </footer>
</body>
instance.trigger = function(msg) {
    instance.send('output', { ts: new Date() });
};

msg.data holds the text of the data-data attribute of the button, if it has one. The designer does not run triggers while the FlowStream has changes that are not applied: select APPLY first.

Start a new message

instance.send(output, data) creates a new message and sends it to the nodes connected to that output. It returns the number of nodes that received it.

Official components like this set exports.kind = 'trigger';. It is a label for the catalogue, which copies it.

Run code at an interval

Start the timer in instance.configure, so that a new interval from the settings takes effect at once:

var timer = null;

instance.configure = function() {
    timer && clearInterval(timer);
    timer = config.interval > 0 ? setInterval(instance.trigger, config.interval * 1000) : null;
};

instance.configure();

For work that is done once a minute, Flow calls instance.service(minutes) every minute, with the number of minutes since the FlowStream started.

Clean up with close

Flow calls instance.close when the node stops: when the user removes it and applies the design, and when the component changes. Stop timers, close connections and release files there, or they keep running:

instance.close = function() {
    timer && clearInterval(timer);
    timer = null;
};

Pause

When the user pauses the FlowStream, Flow stops delivering messages and calls instance.pause(true); when the user resumes, it calls instance.pause(false). Messages sent during the pause are dropped. A component that holds messages can wait with them while it is paused, as Delay does.

The whole component

<script total>

    exports.id = 'heartbeat';
    exports.name = 'Heartbeat';
    exports.icon = 'ti ti-heart';
    exports.author = 'Jane Doe';
    exports.version = '1';
    exports.group = 'Demo';
    exports.kind = 'trigger';
    exports.config = { interval: 5 };
    exports.outputs = [{ id: 'output', name: 'Output' }];

    exports.make = function(instance, config) {

        var timer = null;

        instance.trigger = function() {
            instance.send('output', { ts: new Date() });
        };

        instance.configure = function() {
            timer && clearInterval(timer);
            timer = config.interval > 0 ? setInterval(instance.trigger, config.interval * 1000) : null;
        };

        instance.close = function() {
            timer && clearInterval(timer);
            timer = null;
        };

        instance.configure();
    };

</script>

<readme>
Sends the current date and time at a regular interval. Select **Send now** to send it at once.
</readme>

<settings>
    <div class="padding">
        <ui-component name="input" path="?.interval" config="type:number;min:0" class="m">Interval in seconds</ui-component>
        <div class="help">Use <code>0</code> to send only when you select <b>Send now</b>.</div>
    </div>
</settings>

<body>
    <header>
        <i class="$ICON"></i>$NAME
    </header>
    <footer style="padding:0 10px 10px">
        <button name="trigger" style="width:100%">Send now</button>
    </footer>
</body>

With Interval in seconds set to 1, a Print node connected to Heartbeat shows a new date every second. After the node is removed, no more dates arrive.

Made in Europe