Transcript
  • Meteor

Why Was My Function Rerun?

From the class:  Tracker

Great. There's our item printed out to the page. Now, when this item changes, or really if any of the items change since we called fetch, we would expect that this function should be rerun so long as we're in a computation, which we are because we're inside of a helper. So we can make use of our knowledge here and say if we're in a computation-- in other words, if tracker is active-- then why don't we grab the computation and then provide it an uninvalidate uninvalidate. Then I'm going to name this function so we can see it in the stack.

So we're going to report the invalidation, and then I'm going to use a special trick of using the Chrome debug tools. We normally just call console log, but you can also call console trace. And console trace, the trace function, will print out a stack trace starting with where we are currently in the stack.

So we can also pass a string, which is a label for it in case we have a bunch of stacked traces, so I find that to be very useful. So I'll call this computation invalidated. And then-- let me close this out to give us a little bit more room-- I'll show the ID of the computation that's being invalidated.

And here, printed to the console is the stack trace from my report invalidation function. Now, the reason that the function was invalidated is remember that the first time the page renders, we have no data yet, no data because the publish function hasn't finished. And so once we get the data off the wire, once we have the data, the function, our helper, will invalidate, or they will rerun because it has the data.

So if we open up this stack trace, you can see the stack trace all the way down starting from or ending in the report invalidation function, and then it'll take us all the way to where the invalidation actually started. So let's scroll down to the bottom of this list and look at the very bottom there. It should give you a hint that this came off of sock.js. It was a WebSocket that caused this, and so that should tell you that this has to do with data coming off of the wire.

And once we got that message, you can look up the stack trace here and see the various functions that are called, like local collection insert. And then the tracker dependency actually changing right here. So you see that function called dependency changed. So I find this console.trace function to be quite useful as a way to know how it is that we got to an invalidation.

Now, to make this function reusable and a little bit more generic, why don't we create a function called Report Invalidation? And I'll take all this logic in here and just dump it into that function, and then it's reusable throughout the app. You could even make it a global function so that it's available outside of this file.

And you might even extend this function to only print out the stuff if we're in a debug mode, for example. So I hope this helps you to debug and figure out why certain functions are rerunning.