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

Allow nested template.autorun calls #148

Closed
mitar opened this issue Nov 1, 2016 · 4 comments
Closed

Allow nested template.autorun calls #148

mitar opened this issue Nov 1, 2016 · 4 comments

Comments

@mitar
Copy link
Contributor

mitar commented Nov 1, 2016

For some reason this is not allowed. I do not understand why. I think something like this should be supported:

template.onCreated(function () {
  this.autorun(() => {
    this.autorun(() => {
      // do something
    });
  });
});

Ideally, the following would be equivalent:

template.onCreated(function () {
  this.autorun(() => {
    Tracker.autorun(() => {
      // do something
    });
  });
});

But this would then be a solution for this as well: meteor/meteor#4494

@mitar mitar closed this as completed in d3aa3fa Nov 1, 2016
mitar added a commit that referenced this issue Nov 1, 2016
@zimme
Copy link

zimme commented Jan 12, 2017

What would happen if the outer computation is invalidated in this case? Would it stop and and re-initiate a new inner computation or would it just create new computations for each re-run?

@zimme
Copy link

zimme commented Jan 12, 2017

Also, if that code would be equivalent with the code that you suggested, would the inner computation be destroyed with the template?

@mitar
Copy link
Contributor Author

mitar commented Jan 17, 2017

So this is already implemented. The inner computation is recreated. This is how it works normally in Tracker. And yes, inner computation is destroyed with the template (as a consequence of outside computation being invalidated).

So there is some cost of using nested autorun, but I do not see a reason to prevent it. It might not be good in your case to do it, because creating a computation does cost a bit, but there are also reasons (like computed field) when this is useful because you do want to cache an expensive computation inside another computation.

@zimme
Copy link

zimme commented Jan 17, 2017

Thanks, just wanted to know how this work s a bit closer :)

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

No branches or pull requests

2 participants