Transcript
  • Meteor

Only Run A Function Once

From the class:  Tracker

Another useful trick is to only run a function or only do some kind of logic the first time a computation is run and not to continue to do something over and over again. For example, on one of your route functions, you might only want to report a metric to mix panel or some other metric tracking service once, not every single time the function reruns. So to get a sense of how we can do that, let's create a computation again. In this way, we could just invalidate it manually.

And inside of this, I'm going to just print out the string running computation. And over on the right when this refreshes, it should print it out the first time. And then every time I invalidate it, it should just run that again. So what I want to do is to only have this print out the message one time, the first time that it runs.

So to do that, I can just simply check whether or not the computation-- we can check the first run property of the computation. So to get access to the computation inside of this function, if I have direct access to the parameter, I can do it this way. So I can say, c.firstrun if it's the first run and go ahead and run this console log. Let's try that.

Now this time, if I invalidate the function, the log doesn't get-- the console log statement doesn't run, because if we look at the computation, notice the first run property is false now because it's already been run once. Now, most of the time, we won't have access to this parameter.

For example, it's not passed as a parameter to our helpers. So a better way to do this is to just check the current computation. So we can say tracker.currentcomputation. And we can say if C exists and it's the first run, then print out running computation. So now this time, if I call Comp invalidate over and over again, we should have the same effect as before.

Another thing we can use is a helper that's provided to us by underscore called once. And this function takes a callback as a parameter, and what it does is to make sure that that call that can only be called once ever. So let's redefine this Auto Run up here. I'm just going to call it-- I called it C1. I renamed it off camera. And I'm going to call this one C2.

And this time, I'm going to create a callback that gets wrapped by this once function. And then I'll just call it inside of the computation. So let's just call this two and this one so that we can see the difference.

Now I'll say C2, and I'll call the invalidate function, and it doesn't run it again because the once underscore helper made sure that this function, this function that's returned and assigned to callback, is only run once. And actually, if we make this a global variable and inspect it in the console, you can see what the implementation looks like.

Let's try console.tir. So a little hard to read, but you can get a sense that it's keeping track of whether it was run, and if it has already run, not to run again. So I've added some comments here, and hopefully this gives you an idea of how we can make sure that a piece of code only runs one time? And this is particularly useful when you're doing things like reporting metrics on a page view.