Reactivity in Javascript
In the heart of all modern frontend framework

I am exploring the world of technology and playing with them. Like a kid, breaking them and modiy them.
Search for a command to run...
In the heart of all modern frontend framework

I am exploring the world of technology and playing with them. Like a kid, breaking them and modiy them.
No comments yet. Be the first to comment.
From last year (July~August), I am working on an open source PaaS to deploy and manage applications easily on any VPS. I have a motive to create a solution which you once setup on your cloud, you will get same kind of experience like other platforms ...

This blog is coming after a long time [almost 3 weeks]. Throughout this week, the main tasks involved were - Update the docker setup to make it more convenient Test the docker setup in all OS [Linux, Mac, Windows] Rewrite Documentation Revamp Do...

Finally, On July 14, I received this email about passing the midterm evaluation ๐๐. You can check out the blog on the phase 1 report here: https://blog.circuitverse.org/posts/tanmoy_sarkar_phase_1_report/ You should check other blogs here: https:/...

At the start, this week's main focus was completing the RBS integration. But that does not go well due to having issues with rbs_rails Gem. So I raised PR in rbs_rails the gem repository about the issue to get some hints from the maintainers. You can...

This week was more on learning rather than coding. The main objectives of this week were Learn RBS to start working on it Generate code coverage report and write the missing unit-tests Split Solargraph PR to small PRs for better review Learn RB...

It's the ability of a piece of code to automatically update or re-render in response to changes in the data it is bound to.
Let's try to understand clearly by โฌ

Selling Price and Buying Price are two state variables on which the value of Profit depends.

In the case of a Reactive System,
The profit variable will be updated upon any changes in Selling Price or Buying Price.
In the initial state, Selling price is 500 and Buying Price is 300, So the Profit will be (500-300) = 200.
When we update buying Price to 100, the Profit is recalculated automatically and updated to (500-100)=400
In the case of a non-reactive system,
Upon any changes in Selling Price or Buying Price, the profit variable will not be updated until calculateProfit() gets called again.
So, In the initial state, Selling price is 500 and Buying Price is 300, So the Profit will be (500-300) = 200.
When we update buying Price to 100, the Profit remains the same as before. (500-300) = 200
The reactivity concept is in the โค๏ธ of all modern frontend frameworks (React, Next.js, Vue.js, etc.).
Some important parts where this concept and programming practice are used -
useState hook of React.js
re-render widgets when some bounded state variable got updated
let buyingPrice = 200
let sellingPrice = 500
let profit;
function calculateProfit(){
profit = sellingPrice - buyingPrice
}
calculateProfit()
console.log("Profit : "+profit) // Profit : 300
// Update the selling price
buyingPrice = 100
// call calculateProfit() to recalculate
calculateProfit()
console.log("Profit : "+profit) // Profit : 400
Manage the dependencies, who need to be notified when this data got updated or modified.
class DependancyTracker{
constructor(){
this.subscribers = []
}
// Register the function of dependent code
depend(){
if(target && this.subscribers.includes(target) !== true){
this.subscribers.push(target);
}
}
// Notify the dependent codes to act on update of this data
notify(){
for (let i = 0; i < this.subscribers.length; i++) {
let func = this.subscribers[i];
func(); // run the function
}
}
}
Let's see that in action
let track = new DependancyTracker();
let profit;
let buyingPrice = 200;
let sellingPrice = 400;
function calculateProfit(){
profit = sellingPrice - buyingPrice
}
// Register the depndent code
target = calculateProfit
track.depend()
target()
// Initial profit
console.log("Profit : "+profit) // Profit : 200
// Update the selling price
sellingPrice = 500
// Notify all the dependent codes for re-compute
track.notify();
// calculateProfit is also an part of the dependent codes.
console.log("Profit : "+profit) // Profit : 300
A dictionary to store initial values
const data = {
"buyingPrice" : 200
}
Let's set getter and setter for the specified key
let internalvalue = data.buyingPrice;
Object.defineProperty(data, "buyingPrice", {
get: function() {
console.log("Get trigerred");
return internalvalue;
},
set: function(val) {
internalvalue = val;
console.log("Set trigerred")
}
})
Let's see that in action
data.buyingPrice=900
console.log(data.buyingPrice)
Output -

function watch(func){
target = func;
target();
target = null;
}
const data = {
"buyingPrice" : 200,
"sellingPrice": 400
}
let target = null;
class DependancyTracker{
constructor(){
this.subscribers = []
}
depend(){
if(target && this.subscribers.includes(target) !== true){
this.subscribers.push(target);
}
}
notify(){
for (let i = 0; i < this.subscribers.length; i++) {
this.subscribers[i]();
}
}
}
Object.keys(data).forEach(key => {
let internal = data[key];
let dep = new DependancyTracker()
Object.defineProperty(data, key, {
get: function() {
dep.depend(); // link target function
return internal;
},
set: function(val) {
internal = val; // set value
dep.notify(); // notify dependent variables linked funtion
}
})
})
// Watch function
function watch(func){
target = func;
target();
target = null;
}
// Link calculateProfit function in watch
watch(() => {
data.profit = data.sellingPrice - data.buyingPrice
})
console.log("Profit : "+data.profit) // Profit : 200
data.sellingPrice = 700
console.log("Profit : "+data.profit) // Profit : 500
data.sellingPrice = 900
console.log("Profit : "+data.profit) // Profit : 700
You may have gained an amazing concept from this blog. If you like it, please share it with your friends.