SlideShare a Scribd company logo
M E T E O R ( H T T P S : / / B L O G . D E S I G N V E L O P E R . C O M / C A T E G O R Y / M E T E O R / )
Meteor Deep Dive – Reactive Programming With Tracker
FYI: This is one of three topics of our third Meteor Meetup (https://blog.designveloper.com/2016/08/17/an-overall-look-of-the-3rd-meteor-ho-chi-minh-
meetup/) on August 22th, 2016. The author is Khang Nguyen , a young talent member of Designveloper (https://www.designveloper.com/) . This article
is based on his writing, you can read the original one here (http://nlhuykhang.github.io/javascript/framework/2016/07/27/meteor-deep-dive-
tracker.html).
Introduction
As a Meteor developer, I believe that everyone who has worked with Meteor (https://www.meteor.com/) all had experience with Tracker, or at least used it
through some kinds of interfaces like getMeteordata or createContainer which come with the react-meteor-data package.
However, not all people know how Tracker works its magic. In this post, I’m going to dig into every facet of this case as well as bring to you a little bit of
background knowledge.
What Is Tracker?
Tracker (https://github.com/meteor/meteor/tree/devel/packages/tracker) is a Meteor’s core package, it is small but incredibly powerful library for transparent
reactive programming in Meteor.
Using Tracker you have much of the power of Functional Reactive Programming FRP (https://en.wikipedia.org/wiki/Functional_reactive_programming) system
without following FRP’s principles when implementing your application. Combined with Tracker-aware libraries, like Session/ReactiveVar/ReactiveDict, this lets
you build complex event-driven programs without writing a lot of boilerplate event-handling code.
What Make It Great?
In a nutshell, it is reactivity. In my opinion, it is the strongest aspect of Meteor. Tracker helps us make our system work reactively both on client and server with
ease. We do not have to learn any extra stuffs about reactive programming or functional programming to get started.
B y Va n D o ( h t t p s : // b l o g . d e s i g n v e l o p e r. c o m / a u t h o r / v a n d o / ) o n A u g u s t 2 3 , 2 0 1 6
Just read the Tracker api and do the work then the magic happens. Or even some Meteor-novice who do not know a thing about Tracker, their code still work
reactively. Do you know what am I talking about? It is good old Blaze (https://guide.meteor.com/blaze.html) (I say it’s old because I already moved to React for
all new projects).
Blaze’s helpers are reactive natively because they use Tracker inside, if you put inside them a reactive data source (http://richsilv.github.io/meteor/meteor-
reactive-data-types/) then whenever that source changes those helpers will be recomputed and you get new data on the screen.
Let’s read some code and behold what I am talking about, if you are already familiar with Tracker, skip this part and move to the next section to inspect the
magic.
// Set up the reactive code
const counter1 = new ReactiveVar(0);
const counter2 = new ReactiveVar(0);
const observeCounter = function() {
Tracker.autorun(function() {
const text = `Counter1 is now: ${counter1.get()}`;
console.warn(text);
});
console.warn(`Counter2 is now: ${counter2.get()}`);
};
const computation = Tracker.autorun(observeCounter);
/*
Message on the console:
Counter1 is now: 0
Counter2 is now: 0
*/
// and now change the counter1's value
counter1.set(1);
/*
Message on the console:
Counter1 is now: 1
*/
counter2.set(3);
/*
Message on the console:
Counter1 is now: 1
Counter2 is now: 3
*/
counter1.set(7);
/*
Message on the console:
Counter1 is now: 7
*/
In reality, it happens as shown below:
How Does Tracker Work?
Basically Tracker is a simple interface that lets reactive data sources (like counter in the example above) talk to reactive data consumers (the observeCounter
function). Below is the ow of Tracker (I ignore some good parts to make it as simple as possible)
Call Tracker.autorun with function F
If inside F, there is a reactive data source named R, then that source will add F to its dependence list
Then whenever the value of R changes, R will retrieve all its dependence from the dependence list and run them.
Everything is easier said than done. So, let’s take a look at the code:
const counter = new ReactiveVar(1);
const f = function() {
console.log(counter.get());
};
Tracker.autorun(f);
In the above code: counter is our reactive data source. It raises a question is that how can it know what function used inside to add that function as its
dependence?
This is where the Meteor team does their trick to make this ow transparent. In fact, Tracker is an implementation of the Observer pattern or at least an
Observer-liked pattern. Observer pattern can be used to create a reactive library like Tracker.
In an traditional implementation of Observer, we can think of F as an observer and R as a subject. R must have an interface to add F as its observer and
notify/run F when its value changes. Something like this:
const counter = new Source();
const f = function(val) {
console.log(val);
};
counter.addObserver(f);
To imitate this, Tracker provides us these interface:
Tracker.autorun
Tracker.Dependency
Tracker.Dependency.prototype.depend
Tracker.Dependency.prototype.changed
Tracker.Dependency is the implementation of Subject in traditional Observer. All of reactive data source to use with Tracker use this object inside.
Let’s look at the basic implementation of ReactiveVar I use for examples above:
ReactiveVar = function (initialValue, equalsFunc) {
if (! (this instanceof ReactiveVar))
// called without `new`
return new ReactiveVar(initialValue, equalsFunc);
this.curValue = initialValue;
this.equalsFunc = equalsFunc;
this.dep = new Tracker.Dependency;
};
ReactiveVar.prototype.get = function () {
if (Tracker.active)
this.dep.depend();
return this.curValue;
};
ReactiveVar.prototype.set = function (newValue) {
var oldValue = this.curValue;
if ((this.equalsFunc || ReactiveVar._isEqual)(oldValue, newValue))
// value is same as last time
return;
this.curValue = newValue;
this.dep.changed();
};
So when we create a new instance of ReactiveVar, a Tracker.Dependency object will be created. This object will have two main functions: depend and changed
with get call inside get and set function respectively.
This is the Tracker’s ow with more details:
Tracker.autorun will create a computation with the function (F) pass to it as the computation’s props.
// https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L569-L585
Tracker.autorun = function(f, options) {
// ...
var c = new Tracker.Computation(f, Tracker.currentComputation, options.onError);
// ...
return c;
};
When being initiated, this computation is also set as the current computation inside a “global” variable named Tracker.currentComputation. And F will be run for
the rst time.
// https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L146-L208
Tracker.Computation = function(f, parent, onError) {
// ...
self._func = f;
self._onError = onError;
self._recomputing = false;
var errored = true;
try {
self._compute();
errored = false;
} finally {
self.firstRun = false;
if (errored)
self.stop();
}
};
// https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L302-L316
Tracker.Computation.prototype._compute = function() {
var self = this;
self.invalidated = false;
var previous = Tracker.currentComputation;
setCurrentComputation(self);
var previousInCompute = inCompute;
inCompute = true;
try {
withNoYieldsAllowed(self._func)(self);
} finally {
setCurrentComputation(previous);
inCompute = previousInCompute;
}
};
// https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L29-L32
var setCurrentComputation = function(c) {
Tracker.currentComputation = c;
Tracker.active = !!c;
};
If there is a .get operation (meaning .depend) inside body of F , this function will be run and set the current computation stored in the global var named
Tracker.currentComputation as it’s dependent.
// https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L403-L420
Tracker.Dependency.prototype.depend = function(computation) {
if (!computation) {
// ...
computation = Tracker.currentComputation;
}
var self = this;
var id = computation._id;
if (!(id in self._dependentsById)) {
self._dependentsById[id] = computation;
// ...
return true;
}
return false;
};
Then whenever .set is call (meaning .changed), F will be rerun
// https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L428-L432
Tracker.Dependency.prototype.changed = function() {
var self = this;
for (var id in self._dependentsById)
self._dependentsById[id].invalidate();
};
Yeah so it is the basic idea. Beyond this basic ow actually there are some other important things to be take care for to have a complete production-ready
Tracker library. I am not going to write about those things, instead I will just name them. And you can go and check yourself for a deeper understanding of
Tracker. They are:
Tracker inside Tracker (computation inside computation)
Clear stopped computations
Prevent in nite loop
Takeaways
Here’s something we’ve discovered so far:
Tracker make it possible to do reactive programming in Meteor
It is an observer-liked implementation
A good trick: use a “global currentComputation” to implement transparent Observer
Those guys who wrote Tracker are awesome
So, did you nd some great information of your own in this blog? I would love to know your ideas in the comments below.
Also, don’t forget to help this post spread by emailing it to a friend, or sharing it on Twitter or Facebook if you enjoyed it. Thank you!
Ad

More Related Content

What's hot (19)

Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
Dillon Lee
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
Tomáš Kypta
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
Lei Kang
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJava
Fabio Collini
 
разработка серверов и серверных приложений лекция №4
разработка серверов и серверных приложений лекция №4разработка серверов и серверных приложений лекция №4
разработка серверов и серверных приложений лекция №4
Eugeniy Tyumentcev
 
Survey_Paper
Survey_PaperSurvey_Paper
Survey_Paper
Abhra Koley
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
Concurrent programming with RTOS
Concurrent programming with RTOSConcurrent programming with RTOS
Concurrent programming with RTOS
Sirin Software
 
The Ring programming language version 1.5.4 book - Part 79 of 185
The Ring programming language version 1.5.4 book - Part 79 of 185The Ring programming language version 1.5.4 book - Part 79 of 185
The Ring programming language version 1.5.4 book - Part 79 of 185
Mahmoud Samir Fayed
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
Ted Liang
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstitute
Suresh Loganatha
 
Code Samples
Code SamplesCode Samples
Code Samples
Bill La Forge
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UK
Fabio Collini
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
Programming Homework Help
 
RxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixRxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMix
Tracy Lee
 
Golang dot-testing-lite
Golang dot-testing-liteGolang dot-testing-lite
Golang dot-testing-lite
Richárd Kovács
 
No Hugging, No Learning
No Hugging, No LearningNo Hugging, No Learning
No Hugging, No Learning
Olaf Alders
 
Retrofit library for android
Retrofit library for androidRetrofit library for android
Retrofit library for android
InnovationM
 
Storm @ Fifth Elephant 2013
Storm @ Fifth Elephant 2013Storm @ Fifth Elephant 2013
Storm @ Fifth Elephant 2013
Prashanth Babu
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
Dillon Lee
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
Tomáš Kypta
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
Lei Kang
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJava
Fabio Collini
 
разработка серверов и серверных приложений лекция №4
разработка серверов и серверных приложений лекция №4разработка серверов и серверных приложений лекция №4
разработка серверов и серверных приложений лекция №4
Eugeniy Tyumentcev
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
Concurrent programming with RTOS
Concurrent programming with RTOSConcurrent programming with RTOS
Concurrent programming with RTOS
Sirin Software
 
The Ring programming language version 1.5.4 book - Part 79 of 185
The Ring programming language version 1.5.4 book - Part 79 of 185The Ring programming language version 1.5.4 book - Part 79 of 185
The Ring programming language version 1.5.4 book - Part 79 of 185
Mahmoud Samir Fayed
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
Ted Liang
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstitute
Suresh Loganatha
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UK
Fabio Collini
 
RxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixRxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMix
Tracy Lee
 
No Hugging, No Learning
No Hugging, No LearningNo Hugging, No Learning
No Hugging, No Learning
Olaf Alders
 
Retrofit library for android
Retrofit library for androidRetrofit library for android
Retrofit library for android
InnovationM
 
Storm @ Fifth Elephant 2013
Storm @ Fifth Elephant 2013Storm @ Fifth Elephant 2013
Storm @ Fifth Elephant 2013
Prashanth Babu
 

Similar to Reactive programming with tracker (20)

RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
YarikS
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
Maxim Volgin
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
Shahar Barsheshet
 
Android application architecture
Android application architectureAndroid application architecture
Android application architecture
Romain Rochegude
 
Concurrent talk
Concurrent talkConcurrent talk
Concurrent talk
rahulrevo
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalRxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android Montréal
Sidereo
 
Rxandroid
RxandroidRxandroid
Rxandroid
Thinh Thanh
 
RxAndroid
RxAndroidRxAndroid
RxAndroid
Thinh Thanh
 
Analysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMSAnalysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMS
PVS-Studio
 
Bowtie: Interactive Dashboards
Bowtie: Interactive DashboardsBowtie: Interactive Dashboards
Bowtie: Interactive Dashboards
Jacques Kvam
 
Understanding and Extending Prometheus AlertManager
Understanding and Extending Prometheus AlertManagerUnderstanding and Extending Prometheus AlertManager
Understanding and Extending Prometheus AlertManager
Lee Calcote
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2
JollyRogers5
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
React – Structure Container Component In Meteor
 React – Structure Container Component In Meteor React – Structure Container Component In Meteor
React – Structure Container Component In Meteor
Designveloper
 
Angular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupAngular Optimization Web Performance Meetup
Angular Optimization Web Performance Meetup
David Barreto
 
Reactotron - A Debugging Agent
Reactotron -  A Debugging AgentReactotron -  A Debugging Agent
Reactotron - A Debugging Agent
Matthieu Vachon
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
YarikS
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
Android application architecture
Android application architectureAndroid application architecture
Android application architecture
Romain Rochegude
 
Concurrent talk
Concurrent talkConcurrent talk
Concurrent talk
rahulrevo
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalRxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android Montréal
Sidereo
 
Analysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMSAnalysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMS
PVS-Studio
 
Bowtie: Interactive Dashboards
Bowtie: Interactive DashboardsBowtie: Interactive Dashboards
Bowtie: Interactive Dashboards
Jacques Kvam
 
Understanding and Extending Prometheus AlertManager
Understanding and Extending Prometheus AlertManagerUnderstanding and Extending Prometheus AlertManager
Understanding and Extending Prometheus AlertManager
Lee Calcote
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2
JollyRogers5
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
React – Structure Container Component In Meteor
 React – Structure Container Component In Meteor React – Structure Container Component In Meteor
React – Structure Container Component In Meteor
Designveloper
 
Angular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupAngular Optimization Web Performance Meetup
Angular Optimization Web Performance Meetup
David Barreto
 
Reactotron - A Debugging Agent
Reactotron -  A Debugging AgentReactotron -  A Debugging Agent
Reactotron - A Debugging Agent
Matthieu Vachon
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Ad

More from Designveloper (20)

Let us take care of your brand image
Let us take care of your brand imageLet us take care of your brand image
Let us take care of your brand image
Designveloper
 
5 java script frameworks to watch in 2017
5 java script frameworks to watch in 20175 java script frameworks to watch in 2017
5 java script frameworks to watch in 2017
Designveloper
 
Happy international women's day!
Happy international women's day!Happy international women's day!
Happy international women's day!
Designveloper
 
Typing racer game - a nice break from work
Typing racer game  - a nice break from workTyping racer game  - a nice break from work
Typing racer game - a nice break from work
Designveloper
 
Should we work remotely?
Should we work remotely?Should we work remotely?
Should we work remotely?
Designveloper
 
Meet song nhi your virtual financial assistance
Meet song nhi   your virtual financial assistanceMeet song nhi   your virtual financial assistance
Meet song nhi your virtual financial assistance
Designveloper
 
Why pair programming is a good idea
Why pair programming is a good idea Why pair programming is a good idea
Why pair programming is a good idea
Designveloper
 
5 worst mistakes of diy websites
5 worst mistakes of diy websites5 worst mistakes of diy websites
5 worst mistakes of diy websites
Designveloper
 
Basic glossary of web design terms for non designers (part 2)
Basic glossary of web design terms for non designers (part 2)Basic glossary of web design terms for non designers (part 2)
Basic glossary of web design terms for non designers (part 2)
Designveloper
 
Single page web application development using meteor js
Single page web application development using meteor jsSingle page web application development using meteor js
Single page web application development using meteor js
Designveloper
 
Multiplayer game with unity3 d and meteor
Multiplayer game with unity3 d and meteorMultiplayer game with unity3 d and meteor
Multiplayer game with unity3 d and meteor
Designveloper
 
Awesome free resources for learning javascript
Awesome free resources for learning javascriptAwesome free resources for learning javascript
Awesome free resources for learning javascript
Designveloper
 
What is the best java script frameworks to learn?
What is the best java script frameworks to learn?What is the best java script frameworks to learn?
What is the best java script frameworks to learn?
Designveloper
 
Travelling forms a young man
Travelling forms a young manTravelling forms a young man
Travelling forms a young man
Designveloper
 
5 compelling reasons your website should be responsive
5 compelling reasons your website should be responsive5 compelling reasons your website should be responsive
5 compelling reasons your website should be responsive
Designveloper
 
Benefits of using single page websites
Benefits of using single page websitesBenefits of using single page websites
Benefits of using single page websites
Designveloper
 
What is the best programming language for beginner?
What is the best programming language for beginner?What is the best programming language for beginner?
What is the best programming language for beginner?
Designveloper
 
No sql injection in meteor.js application
No sql injection in meteor.js applicationNo sql injection in meteor.js application
No sql injection in meteor.js application
Designveloper
 
How to deploy and scale your meteor apps
How to deploy and scale your meteor appsHow to deploy and scale your meteor apps
How to deploy and scale your meteor apps
Designveloper
 
Meetup groups you need to join if you are new to tech
Meetup groups you need to join if you are new to techMeetup groups you need to join if you are new to tech
Meetup groups you need to join if you are new to tech
Designveloper
 
Let us take care of your brand image
Let us take care of your brand imageLet us take care of your brand image
Let us take care of your brand image
Designveloper
 
5 java script frameworks to watch in 2017
5 java script frameworks to watch in 20175 java script frameworks to watch in 2017
5 java script frameworks to watch in 2017
Designveloper
 
Happy international women's day!
Happy international women's day!Happy international women's day!
Happy international women's day!
Designveloper
 
Typing racer game - a nice break from work
Typing racer game  - a nice break from workTyping racer game  - a nice break from work
Typing racer game - a nice break from work
Designveloper
 
Should we work remotely?
Should we work remotely?Should we work remotely?
Should we work remotely?
Designveloper
 
Meet song nhi your virtual financial assistance
Meet song nhi   your virtual financial assistanceMeet song nhi   your virtual financial assistance
Meet song nhi your virtual financial assistance
Designveloper
 
Why pair programming is a good idea
Why pair programming is a good idea Why pair programming is a good idea
Why pair programming is a good idea
Designveloper
 
5 worst mistakes of diy websites
5 worst mistakes of diy websites5 worst mistakes of diy websites
5 worst mistakes of diy websites
Designveloper
 
Basic glossary of web design terms for non designers (part 2)
Basic glossary of web design terms for non designers (part 2)Basic glossary of web design terms for non designers (part 2)
Basic glossary of web design terms for non designers (part 2)
Designveloper
 
Single page web application development using meteor js
Single page web application development using meteor jsSingle page web application development using meteor js
Single page web application development using meteor js
Designveloper
 
Multiplayer game with unity3 d and meteor
Multiplayer game with unity3 d and meteorMultiplayer game with unity3 d and meteor
Multiplayer game with unity3 d and meteor
Designveloper
 
Awesome free resources for learning javascript
Awesome free resources for learning javascriptAwesome free resources for learning javascript
Awesome free resources for learning javascript
Designveloper
 
What is the best java script frameworks to learn?
What is the best java script frameworks to learn?What is the best java script frameworks to learn?
What is the best java script frameworks to learn?
Designveloper
 
Travelling forms a young man
Travelling forms a young manTravelling forms a young man
Travelling forms a young man
Designveloper
 
5 compelling reasons your website should be responsive
5 compelling reasons your website should be responsive5 compelling reasons your website should be responsive
5 compelling reasons your website should be responsive
Designveloper
 
Benefits of using single page websites
Benefits of using single page websitesBenefits of using single page websites
Benefits of using single page websites
Designveloper
 
What is the best programming language for beginner?
What is the best programming language for beginner?What is the best programming language for beginner?
What is the best programming language for beginner?
Designveloper
 
No sql injection in meteor.js application
No sql injection in meteor.js applicationNo sql injection in meteor.js application
No sql injection in meteor.js application
Designveloper
 
How to deploy and scale your meteor apps
How to deploy and scale your meteor appsHow to deploy and scale your meteor apps
How to deploy and scale your meteor apps
Designveloper
 
Meetup groups you need to join if you are new to tech
Meetup groups you need to join if you are new to techMeetup groups you need to join if you are new to tech
Meetup groups you need to join if you are new to tech
Designveloper
 
Ad

Recently uploaded (20)

Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Unlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive GuideUnlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive Guide
vikasascentbpo
 
MINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PRMINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PR
MIND CTI
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Vaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without HallucinationsVaibhav Gupta BAML: AI work flows without Hallucinations
Vaibhav Gupta BAML: AI work flows without Hallucinations
john409870
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Unlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive GuideUnlocking the Power of IVR: A Comprehensive Guide
Unlocking the Power of IVR: A Comprehensive Guide
vikasascentbpo
 
MINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PRMINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PR
MIND CTI
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 

Reactive programming with tracker

  • 1. M E T E O R ( H T T P S : / / B L O G . D E S I G N V E L O P E R . C O M / C A T E G O R Y / M E T E O R / )
  • 2. Meteor Deep Dive – Reactive Programming With Tracker FYI: This is one of three topics of our third Meteor Meetup (https://blog.designveloper.com/2016/08/17/an-overall-look-of-the-3rd-meteor-ho-chi-minh- meetup/) on August 22th, 2016. The author is Khang Nguyen , a young talent member of Designveloper (https://www.designveloper.com/) . This article is based on his writing, you can read the original one here (http://nlhuykhang.github.io/javascript/framework/2016/07/27/meteor-deep-dive- tracker.html). Introduction As a Meteor developer, I believe that everyone who has worked with Meteor (https://www.meteor.com/) all had experience with Tracker, or at least used it through some kinds of interfaces like getMeteordata or createContainer which come with the react-meteor-data package. However, not all people know how Tracker works its magic. In this post, I’m going to dig into every facet of this case as well as bring to you a little bit of background knowledge. What Is Tracker? Tracker (https://github.com/meteor/meteor/tree/devel/packages/tracker) is a Meteor’s core package, it is small but incredibly powerful library for transparent reactive programming in Meteor. Using Tracker you have much of the power of Functional Reactive Programming FRP (https://en.wikipedia.org/wiki/Functional_reactive_programming) system without following FRP’s principles when implementing your application. Combined with Tracker-aware libraries, like Session/ReactiveVar/ReactiveDict, this lets you build complex event-driven programs without writing a lot of boilerplate event-handling code. What Make It Great? In a nutshell, it is reactivity. In my opinion, it is the strongest aspect of Meteor. Tracker helps us make our system work reactively both on client and server with ease. We do not have to learn any extra stuffs about reactive programming or functional programming to get started. B y Va n D o ( h t t p s : // b l o g . d e s i g n v e l o p e r. c o m / a u t h o r / v a n d o / ) o n A u g u s t 2 3 , 2 0 1 6
  • 3. Just read the Tracker api and do the work then the magic happens. Or even some Meteor-novice who do not know a thing about Tracker, their code still work reactively. Do you know what am I talking about? It is good old Blaze (https://guide.meteor.com/blaze.html) (I say it’s old because I already moved to React for all new projects). Blaze’s helpers are reactive natively because they use Tracker inside, if you put inside them a reactive data source (http://richsilv.github.io/meteor/meteor- reactive-data-types/) then whenever that source changes those helpers will be recomputed and you get new data on the screen. Let’s read some code and behold what I am talking about, if you are already familiar with Tracker, skip this part and move to the next section to inspect the magic.
  • 4. // Set up the reactive code const counter1 = new ReactiveVar(0); const counter2 = new ReactiveVar(0); const observeCounter = function() { Tracker.autorun(function() { const text = `Counter1 is now: ${counter1.get()}`; console.warn(text); }); console.warn(`Counter2 is now: ${counter2.get()}`); }; const computation = Tracker.autorun(observeCounter); /* Message on the console: Counter1 is now: 0 Counter2 is now: 0 */ // and now change the counter1's value counter1.set(1); /* Message on the console: Counter1 is now: 1 */ counter2.set(3); /* Message on the console: Counter1 is now: 1 Counter2 is now: 3 */ counter1.set(7); /* Message on the console: Counter1 is now: 7 */
  • 5. In reality, it happens as shown below: How Does Tracker Work? Basically Tracker is a simple interface that lets reactive data sources (like counter in the example above) talk to reactive data consumers (the observeCounter function). Below is the ow of Tracker (I ignore some good parts to make it as simple as possible) Call Tracker.autorun with function F If inside F, there is a reactive data source named R, then that source will add F to its dependence list Then whenever the value of R changes, R will retrieve all its dependence from the dependence list and run them. Everything is easier said than done. So, let’s take a look at the code: const counter = new ReactiveVar(1); const f = function() { console.log(counter.get()); }; Tracker.autorun(f); In the above code: counter is our reactive data source. It raises a question is that how can it know what function used inside to add that function as its dependence? This is where the Meteor team does their trick to make this ow transparent. In fact, Tracker is an implementation of the Observer pattern or at least an Observer-liked pattern. Observer pattern can be used to create a reactive library like Tracker. In an traditional implementation of Observer, we can think of F as an observer and R as a subject. R must have an interface to add F as its observer and notify/run F when its value changes. Something like this:
  • 6. const counter = new Source(); const f = function(val) { console.log(val); }; counter.addObserver(f); To imitate this, Tracker provides us these interface: Tracker.autorun Tracker.Dependency Tracker.Dependency.prototype.depend Tracker.Dependency.prototype.changed Tracker.Dependency is the implementation of Subject in traditional Observer. All of reactive data source to use with Tracker use this object inside. Let’s look at the basic implementation of ReactiveVar I use for examples above:
  • 7. ReactiveVar = function (initialValue, equalsFunc) { if (! (this instanceof ReactiveVar)) // called without `new` return new ReactiveVar(initialValue, equalsFunc); this.curValue = initialValue; this.equalsFunc = equalsFunc; this.dep = new Tracker.Dependency; }; ReactiveVar.prototype.get = function () { if (Tracker.active) this.dep.depend(); return this.curValue; }; ReactiveVar.prototype.set = function (newValue) { var oldValue = this.curValue; if ((this.equalsFunc || ReactiveVar._isEqual)(oldValue, newValue)) // value is same as last time return; this.curValue = newValue; this.dep.changed(); }; So when we create a new instance of ReactiveVar, a Tracker.Dependency object will be created. This object will have two main functions: depend and changed with get call inside get and set function respectively. This is the Tracker’s ow with more details: Tracker.autorun will create a computation with the function (F) pass to it as the computation’s props.
  • 8. // https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L569-L585 Tracker.autorun = function(f, options) { // ... var c = new Tracker.Computation(f, Tracker.currentComputation, options.onError); // ... return c; }; When being initiated, this computation is also set as the current computation inside a “global” variable named Tracker.currentComputation. And F will be run for the rst time.
  • 9. // https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L146-L208 Tracker.Computation = function(f, parent, onError) { // ... self._func = f; self._onError = onError; self._recomputing = false; var errored = true; try { self._compute(); errored = false; } finally { self.firstRun = false; if (errored) self.stop(); } }; // https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L302-L316 Tracker.Computation.prototype._compute = function() { var self = this; self.invalidated = false; var previous = Tracker.currentComputation; setCurrentComputation(self); var previousInCompute = inCompute; inCompute = true; try { withNoYieldsAllowed(self._func)(self); } finally { setCurrentComputation(previous); inCompute = previousInCompute; } }; // https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L29-L32 var setCurrentComputation = function(c) { Tracker.currentComputation = c;
  • 10. Tracker.active = !!c; }; If there is a .get operation (meaning .depend) inside body of F , this function will be run and set the current computation stored in the global var named Tracker.currentComputation as it’s dependent. // https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L403-L420 Tracker.Dependency.prototype.depend = function(computation) { if (!computation) { // ... computation = Tracker.currentComputation; } var self = this; var id = computation._id; if (!(id in self._dependentsById)) { self._dependentsById[id] = computation; // ... return true; } return false; }; Then whenever .set is call (meaning .changed), F will be rerun // https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js#L428-L432 Tracker.Dependency.prototype.changed = function() { var self = this; for (var id in self._dependentsById) self._dependentsById[id].invalidate(); }; Yeah so it is the basic idea. Beyond this basic ow actually there are some other important things to be take care for to have a complete production-ready Tracker library. I am not going to write about those things, instead I will just name them. And you can go and check yourself for a deeper understanding of Tracker. They are:
  • 11. Tracker inside Tracker (computation inside computation) Clear stopped computations Prevent in nite loop Takeaways Here’s something we’ve discovered so far: Tracker make it possible to do reactive programming in Meteor It is an observer-liked implementation A good trick: use a “global currentComputation” to implement transparent Observer Those guys who wrote Tracker are awesome So, did you nd some great information of your own in this blog? I would love to know your ideas in the comments below. Also, don’t forget to help this post spread by emailing it to a friend, or sharing it on Twitter or Facebook if you enjoyed it. Thank you!