Transcript
  • Meteor

What Is Tracker?

From the class:  Tracker

OK. In this class we're going to talk about a Meteor tracker, and a Meteor tracker is the reactivity system that's built right into the Meteor core. So to start us off, why don't we head over to the Wikipedia article for reactive programming, and I'd like to take a look at what it means to be reactive. Now, the language here I've always found to be a little bit confusing.

It says that reactor programming is some kind of paradigm of programming oriented around data flows and the propagation of change. And what that means specifically as an example is imagine that you have a spreadsheet, and the spreadsheet has these three cells, A, B, and C. And A is equal to the sum of B and C, So if we have 5 and 3, then the sum would be 8.

And what we want to do is if one of the cells changes, we want to automatically recompute the value of A. And ideally we should be able to program this so that it happens automatically. So that's the idea of reactive programming. It's sort of similar to event handling, except that it happens somewhat transparently. Let's go over to a Meteor app and take a look at a few examples that you've probably already seen in your own applications.

OK, so this is the standard Meteor application that gets created when you say Meteor create in your app, and you're seeing an example of reactivity right here in this counter helper. So notice that it's returning the value of calling the get function with some key. And that's one of the important aspects of the Meteor reactivity and tracker system. It means that we're going to call this function, some kind of function that's associated with a reactive data source.

And this reactive data source is called session. There's a few others, like Mongo collections, where we can call find, and find1. And you may have even implemented your own reactive variables, but in this case we're calling the get function. And when we do that we get back a value, the value of the counter over here. And notice I'm also printing out to the log, to the console, the fact that the function has run.

Now, the other side of this reactivity is calling the set function on this reactive variable. So the idea is that we want to change the value of some reactive data source and automatically re-run this function. So that's really the key to Meteor's reactivity. It's going to be able to re-run a function whenever the value that gets returned by this function call changes. So we can see that in action by clicking the button, and notice the console logs down at the bottom just keep getting written.

So the value is changing, and this function here is just being re-run. So there's a lot of mechanics here of what's going on to make this happen. And of course, the idea is to make it as seamless as possible so that you can just use any of these reactive data sources, and that your functions will automatically re-run, and the right thing will happen on the screen.

But in this class we're going to dig into how it actually works. So we'll start off by taking a look at the computation object, and then we'll look at how the computation object works with reactive data sources using something called a dependency. And then from there we'll go on to build our own reactive data source, and then look at some techniques for debugging and working with reactive data sources in your apps.