Variable access from index.js
I wanted to know if there was a way to update the Global Variables from the root Program?
Thanks!
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);
}
Thank you , that was fast!
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!
Here: https://github.com/totaljs/flow/blob/master/definitions/flowstream.js#L13
Flow.on('save', function(schema) {
// @schema {Object} with FlowStream data
});