Skip to main content

Tracker API

Tracker is Meteor’s reactivity system. It allows you to automatically rerun functions when data changes. Source: packages/tracker/tracker.js

Core Functions

Tracker.autorun()

Run a function now and rerun it later whenever its dependencies change. Returns a Computation object that can be used to stop or observe the rerunning. Locus: Client
runFunc
function
required
The function to run. It receives one argument: the Computation object that will be returned.
options
object
Optional options object.
options.onError
function
Optional function to run when an error happens in the Computation. The only argument it receives is the Error thrown. Defaults to the error being logged to the console.
Returns: Tracker.Computation
Async Support:

Tracker.nonreactive()

Run a function without tracking dependencies. Locus: Client
func
function
required
A function to call immediately.

Tracker.flush()

Process all reactive updates immediately and ensure that all invalidated computations are rerun. Locus: Client

Tracker.onInvalidate()

Registers a new onInvalidate callback on the current computation, to be called immediately when the current computation is invalidated or stopped. Locus: Client
callback
function
required
A callback function that will be invoked as func(c), where c is the computation on which the callback is registered.

Tracker.afterFlush()

Schedules a function to be called during the next flush, or later in the current flush if one is in progress, after all invalidated computations have been rerun. Locus: Client
callback
function
required
A function to call at flush time.

Tracker.Computation

A Computation object represents code that is repeatedly rerun in response to reactive data changes.

Properties

stopped
boolean
True if this computation has been stopped.
invalidated
boolean
True if this computation has been invalidated (and not yet rerun), or if it has been stopped.
firstRun
boolean
True during the initial run of the computation at the time Tracker.autorun is called, and false on subsequent reruns and at other times.

Methods

computation.stop()

Prevents this computation from rerunning. Locus: Client

computation.invalidate()

Invalidates this computation so that it will be rerun. Locus: Client

computation.onInvalidate()

Registers a callback to run when this computation is next invalidated, or runs it immediately if the computation is already invalidated. Locus: Client
callback
function
required
Function to be called on invalidation. Receives one argument: the computation that was invalidated.

computation.onStop()

Registers a callback to run when this computation is stopped, or runs it immediately if the computation is already stopped. Locus: Client
callback
function
required
Function to be called on stop. Receives one argument: the computation that was stopped.

computation.flush()

Process the reactive updates for this computation immediately and ensure that the computation is rerun. Locus: Client

computation.run()

Causes the function inside this computation to run and synchronously process all reactive updates. Locus: Client

Tracker.Dependency

A Dependency represents an atomic unit of reactive data that a computation might depend on.

Constructor

Methods

dependency.depend()

Declares that the current computation depends on this dependency. Locus: Client
fromComputation
Tracker.Computation
An optional computation declared to depend on this dependency instead of the current computation.
Returns: boolean - True if the computation is a new dependent of this dependency rather than an existing one.

dependency.changed()

Invalidate all dependent computations immediately and remove them as dependents. Locus: Client

dependency.hasDependents()

True if this Dependency has one or more dependent Computations. Locus: Client Returns: boolean

Tracker.active

True if there is a current computation. Locus: Client Type: boolean

Tracker.currentComputation

The current computation, or null if there isn’t one. Locus: Client Type: Tracker.Computation

Example: Custom Reactive Data Source