Allow nested template.autorun calls #148

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

Comments

Projects
None yet
2 participants
@mitar
Collaborator

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 in d3aa3fa Nov 1, 2016

mitar added a commit that referenced this issue Nov 1, 2016

@zimme

This comment has been minimized.

Show comment
Hide comment
@zimme

zimme 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 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

This comment has been minimized.

Show comment
Hide comment
@zimme

zimme 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?

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

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Jan 17, 2017

Collaborator

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.

Collaborator

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

This comment has been minimized.

Show comment
Hide comment
@zimme

zimme Jan 17, 2017

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

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