Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable access from index.js #118

Open
MrWackers opened this issue Jun 8, 2024 · 4 comments
Open

Variable access from index.js #118

MrWackers opened this issue Jun 8, 2024 · 4 comments
Labels

Comments

@MrWackers
Copy link

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

Thanks!

@petersirka
Copy link
Collaborator

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);
}

@MrWackers
Copy link
Author

Thank you , that was fast!

@MrWackers
Copy link
Author

MrWackers commented Jun 8, 2024

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!

@petersirka
Copy link
Collaborator

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants