What is Tracker?
Tracker is an incredibly tiny (~1k) but incredibly powerful library for transparent reactive programming in JavaScript. It gives you much of the power of Functional Reactive Programming (FRP) without requiring you to rewrite your program as a data flow graph.Tracker is essentially a simple convention that lets reactive data sources (like your database) talk to reactive data consumers (such as a live-updating HTML templating library) without the application code in between having to be involved.
The Big Idea
With Tracker, you write normal JavaScript code, but it automatically reruns when data changes:Core API
Tracker.autorun
Runs a function now and reruns it whenever dependencies change:The function is called once immediately, then again whenever any reactive data source it accessed changes.
Tracker.Dependency
Creates a reactive data source:Tracker.Computation
Represents a reactive computation:Tracker.nonreactive
Run code without tracking dependencies:Reactive Data Sources
Meteor provides several built-in reactive data sources:ReactiveVar
A single reactive value:ReactiveDict
A reactive key-value store:Session (Global ReactiveDict)
Minimongo Collections
Database queries are reactive:Meteor.user() and Meteor.userId()
Current user is reactive:Real-World Example: Temperature Monitor
Here’s the complete temperature example from Tracker’s README:Integration with UI Libraries
React
UseuseTracker hook:
Blaze
Blaze templates are automatically reactive:Vue
Useautorun in lifecycle hooks:
Advanced Patterns
Computation Lifecycle
Async Autoruns (Meteor 3.x)
Meteor 3 supports async autoruns:Throttling Reruns
Selective Invalidation
How It Works Internally
Here’s the actual implementation frompackages/tracker/tracker.js:
Dependency Implementation
Global State
Autorun Implementation
Performance Considerations
Minimize Autoruns
Minimize Autoruns
Each autorun has overhead. Combine related logic:
Use Specific Queries
Use Specific Queries
Reactive queries only invalidate when results change:
Stop Unused Computations
Stop Unused Computations
Clean up when done:In React:
Debugging Reactivity
Next: DDP Protocol
Learn about Meteor’s real-time data protocol