How to log what caused autorun to run?


#1

Hey everyone,

My problem is that some autoruns are fired many times, and i’d like to have a way to quickly check the source.

I wanted to know if something like that was possible :

let reactive_var_1 = new ReactiveVar();
let reactive_var_2 = new ReactiveVar();
let reactive_var_3 = new ReactiveVar();
let reactive_var_4 = new ReactiveVar();

Template.test.onCreated(function () {
    console.log('Template.test.onCreated...');
    this.autorun(function () {
        console.log('Template.test.onCreated.autorun...');
        console.log('autorun source is : ');
        console.log(source);

        let do_something_1 = reactive_var_1.get() + 1;
        let do_something_2 = reactive_var_3.get() + 2;
        let do_something_3 = reactive_var_3.get() + 3;
        let do_something_4 = reactive_var_4.get() + 4;
    });
});

Template.test.events({
    'click .something': function () {
        console.log('Someone clicked !!');
        reactive_var_3.set(12);
    }
});

Output console should look like this :

Template.test.onCreated...
Template.test.onCreated.autorun...
autorun source is : 
undefined
Someone clicked !!
Template.test.onCreated.autorun...
autorun source is : 
'reactive_var_3'

This is the source function i’m looking for, which could output my 'reactive_var_3’

Thanks for your help :slight_smile:


#2

This is not something that is provided to you.

It might be possible by creating an error and parsing the stack to see if you can find which dependency changed.
The thing to look for is Dependency.changed()

Or you can keep track of the last seen values and do your own comparisons