flow icon indicating copy to clipboard operation
flow copied to clipboard

Variable access from index.js

Open MrWackers opened this issue 1 year ago • 4 comments

I wanted to know if there was a way to update the Global Variables from the root Program?

Thanks!

MrWackers avatar Jun 08 '24 18:06 MrWackers

Of course. Flow is a global variable, and you need to understand that it supports two kinds of global variables:

  • global variables that are shared between all flows (read-only in FlowStreams)
  • FlowStream variables controlled by the FlowStream
let variables = {
    db: 'value',
    secret: 'value'
};

// Global shared variables
for (let key in Flow.instances) {
    let instance = Flow.instances[key];
    instance.variables2(variables);
}

// Private variables per Flow
for (let key in Flow.instances) {
    let instance = Flow.instances[key];
    instance.variables(variables);
}

petersirka avatar Jun 08 '24 18:06 petersirka

Thank you , that was fast!

MrWackers avatar Jun 08 '24 20:06 MrWackers

Follow up on this, currently if you write to a variable using variables or variables2, this will disable saving.

the code is pretty simple:

ON('ready', function () {
    console.log('Ready...');

    setInterval(updateVars, 10000);

});

var counter = 0;

function updateVars() {
    counter++;
    let variables = {
        HA_MASTER: counter
    };

    // Global shared variables
    for (let key in Flow.instances) {
        let instance = Flow.instances[key];
        instance.variables2(variables);
    }
}

Also, How would i go about knowing when the Flow is saved. i tried using

instance.flow.on('schema', function (schema) {
                console.log(schema);
            });

but nothing prints.

Thanks again!

MrWackers avatar Jun 08 '24 22:06 MrWackers

Here: https://github.com/totaljs/flow/blob/master/definitions/flowstream.js#L13

Flow.on('save', function(schema) {
    // @schema {Object} with FlowStream data
});

petersirka avatar Jun 09 '24 06:06 petersirka