SlideShare a Scribd company logo
1 of 3
CLUBBING ZONES WITH ANGULAR
Thread Local Storage (TLS):
Static and global data are shared across all the threads. If you modified a global/static
variable it is visible to all the threads. Unlike global/shared variable if you create a variable in
TLS, every thread has its own copy of the variable, i.e. changes to the variable are local to
the thread.
So what’s a Zone?
Zone plays a major role in async tasks, and it’s a thread local storage for JavaScript VM’s.
They are basically an execution context for asynchronous operations. They turn out to be
really useful for things like error handling and profiling. But what exactly does that mean?
Frameworks, such as Angular, need to know when all of the application work has
completed and perform DOM update before the host environment performs the pixel
rendering. In practice this means that the framework is interested when the main task and
the associated micro tasks have executed but before the VM hands over the control to the
host.
I think that the main purpose of using zone.js in angular is to know when to render.
Zone actually works on detecting changes. So, without zones, we don’t get any change
detection. Angular uses the zone to patch async APIs (addEventListener, setTimeout(),
...) and uses notifications from these patched APIs to run change detection every time
some async event happened. But actually zones are not required for change detection in
general, they simply are the means by which Angular picks up changes and then calls
tick so that any listeners for those changes are actually fired.
In other words, Zone.js is a kind of wrapper around the whole angular application that
patches (almost) all async APIs of the browser. Angular invokes automatic change
detection every time execution of an event handler has been completed, and the use of
zone.js allows Angular2 to get notified when that happened.
RxJS is a way of handling single or a series of events from an async APIs in your code.
In Angular, we don’t have a $digest and $watch like cycle in the original Angular JS, so if
we were to does something like this in Angular 2:
Then the UI wouldn’t actually update properly, because ‘someProperty’ actually is updated
outside of Angular’s zone. So we have to include something like this in order to get the
correct updates:
In other words, zone.run() is kind of like the new $digest().
API support:
Zone.js patched most standard web APIs (such as DOM events, XMLHttpRequest, ...) and
nodejs APIs (EventEmitter, fs, ...), and some nonstandard APIs, such as MediaQuery and
Notification, etc.
NgZone is a wrapper around zone.js, it extends some concepts of Zone. It actually
wrapped a forked zone, we can see it from the full implementation.
NgZone first asserts that Zone.js has been patched, then it assigns itself this to self. _inner
property is created and assigned to the current zone, in turn, _outer property is created
and assigned to _inner value.
The outer zone is used by NgZone to run code outside the angular zone, it is often used
when you don't want the change detection cycle to run.
Inner zone is used to run code inside the angular zone, this is where all our Angular code
is executed when an async operation is caught change detection is triggered.
Monkey-patched Hooks:
It turns out that there are a few other hooks. In fact, those aren’t just hooks, but monkey-
patched methods on the global scope. As soon as we embed zone.js in our site, pretty
much all methods that cause asynchronous operations are monkey-patched to run in a
new zone.
For example, when we call setTimeout() we actually call Zone.setTimeout(), which in turn
creates a new zone using zone.fork() in which the given handler is executed. And that’s
why our hooks are executed as well, because the forked zone in which the handler will be
executed, simply inherits from the parent zone.
There are some other methods that zone.js overrides by default and provides us as hooks:
 Zone.setInterval()
 Zone.alert()
 Zone.prompt()
 Zone.requestAnimationFrame()
 Zone.addEventListener()
 Zone.removeEventListener()
We might wonder why methods like alert() and prompt() are patched as well. As mentioned
earlier, those patched methods are hooks at the same time. We can change and extend
them by forking a zone exactly the same way we did with ‘beforeTask’ and ‘afterTask’. This
turns out to be super powerful, because we can intercept calls to alerts() and prompt() and
change their behaviour when we write tests.
Also zone.js is useful for debugging, testing, profiling. It helps you see whole call stack if
you face with some error.

More Related Content

Similar to Clubbing zones with angular

JavaScript Engines and Event Loop
JavaScript Engines and Event Loop JavaScript Engines and Event Loop
JavaScript Engines and Event Loop Tapan B.K.
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of SignalsCoding Academy
 
26 top angular 8 interview questions to know in 2020 [www.full stack.cafe]
26 top angular 8 interview questions to know in 2020   [www.full stack.cafe]26 top angular 8 interview questions to know in 2020   [www.full stack.cafe]
26 top angular 8 interview questions to know in 2020 [www.full stack.cafe]Alex Ershov
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2Knoldus Inc.
 
devjam2018 - angular 5 performance
devjam2018  - angular 5 performancedevjam2018  - angular 5 performance
devjam2018 - angular 5 performanceElad Hirsch
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/Geekyants
 
Angular 2.0 change detection
Angular 2.0 change detectionAngular 2.0 change detection
Angular 2.0 change detectionRan Wahle
 
Javascript internals
Javascript internalsJavascript internals
Javascript internalsAyush Sharma
 
Fltk S60 Introduction
Fltk S60 IntroductionFltk S60 Introduction
Fltk S60 Introductionguest5c161
 
Android RenderScript on LLVM
Android RenderScript on LLVMAndroid RenderScript on LLVM
Android RenderScript on LLVMJohn Lee
 
Memento Pattern Implementation
Memento Pattern ImplementationMemento Pattern Implementation
Memento Pattern ImplementationSteve Widom
 

Similar to Clubbing zones with angular (20)

Javascript
JavascriptJavascript
Javascript
 
JavaScript Engines and Event Loop
JavaScript Engines and Event Loop JavaScript Engines and Event Loop
JavaScript Engines and Event Loop
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 
26 top angular 8 interview questions to know in 2020 [www.full stack.cafe]
26 top angular 8 interview questions to know in 2020   [www.full stack.cafe]26 top angular 8 interview questions to know in 2020   [www.full stack.cafe]
26 top angular 8 interview questions to know in 2020 [www.full stack.cafe]
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
devjam2018 - angular 5 performance
devjam2018  - angular 5 performancedevjam2018  - angular 5 performance
devjam2018 - angular 5 performance
 
Angular
AngularAngular
Angular
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
Angular 2.0 change detection
Angular 2.0 change detectionAngular 2.0 change detection
Angular 2.0 change detection
 
Using zone.js
Using zone.jsUsing zone.js
Using zone.js
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
Javascript internals
Javascript internalsJavascript internals
Javascript internals
 
Meteor
MeteorMeteor
Meteor
 
Fltk S60 Introduction
Fltk S60 IntroductionFltk S60 Introduction
Fltk S60 Introduction
 
Android RenderScript on LLVM
Android RenderScript on LLVMAndroid RenderScript on LLVM
Android RenderScript on LLVM
 
Memento Pattern Implementation
Memento Pattern ImplementationMemento Pattern Implementation
Memento Pattern Implementation
 
NodeJS @ ACS
NodeJS @ ACSNodeJS @ ACS
NodeJS @ ACS
 
Wwf
WwfWwf
Wwf
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Moment.js overview
Moment.js overviewMoment.js overview
Moment.js overview
 

Recently uploaded

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 

Recently uploaded (20)

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Clubbing zones with angular

  • 1. CLUBBING ZONES WITH ANGULAR Thread Local Storage (TLS): Static and global data are shared across all the threads. If you modified a global/static variable it is visible to all the threads. Unlike global/shared variable if you create a variable in TLS, every thread has its own copy of the variable, i.e. changes to the variable are local to the thread. So what’s a Zone? Zone plays a major role in async tasks, and it’s a thread local storage for JavaScript VM’s. They are basically an execution context for asynchronous operations. They turn out to be really useful for things like error handling and profiling. But what exactly does that mean? Frameworks, such as Angular, need to know when all of the application work has completed and perform DOM update before the host environment performs the pixel rendering. In practice this means that the framework is interested when the main task and the associated micro tasks have executed but before the VM hands over the control to the host. I think that the main purpose of using zone.js in angular is to know when to render. Zone actually works on detecting changes. So, without zones, we don’t get any change detection. Angular uses the zone to patch async APIs (addEventListener, setTimeout(), ...) and uses notifications from these patched APIs to run change detection every time some async event happened. But actually zones are not required for change detection in general, they simply are the means by which Angular picks up changes and then calls tick so that any listeners for those changes are actually fired. In other words, Zone.js is a kind of wrapper around the whole angular application that patches (almost) all async APIs of the browser. Angular invokes automatic change detection every time execution of an event handler has been completed, and the use of zone.js allows Angular2 to get notified when that happened.
  • 2. RxJS is a way of handling single or a series of events from an async APIs in your code. In Angular, we don’t have a $digest and $watch like cycle in the original Angular JS, so if we were to does something like this in Angular 2: Then the UI wouldn’t actually update properly, because ‘someProperty’ actually is updated outside of Angular’s zone. So we have to include something like this in order to get the correct updates: In other words, zone.run() is kind of like the new $digest(). API support: Zone.js patched most standard web APIs (such as DOM events, XMLHttpRequest, ...) and nodejs APIs (EventEmitter, fs, ...), and some nonstandard APIs, such as MediaQuery and Notification, etc. NgZone is a wrapper around zone.js, it extends some concepts of Zone. It actually wrapped a forked zone, we can see it from the full implementation.
  • 3. NgZone first asserts that Zone.js has been patched, then it assigns itself this to self. _inner property is created and assigned to the current zone, in turn, _outer property is created and assigned to _inner value. The outer zone is used by NgZone to run code outside the angular zone, it is often used when you don't want the change detection cycle to run. Inner zone is used to run code inside the angular zone, this is where all our Angular code is executed when an async operation is caught change detection is triggered. Monkey-patched Hooks: It turns out that there are a few other hooks. In fact, those aren’t just hooks, but monkey- patched methods on the global scope. As soon as we embed zone.js in our site, pretty much all methods that cause asynchronous operations are monkey-patched to run in a new zone. For example, when we call setTimeout() we actually call Zone.setTimeout(), which in turn creates a new zone using zone.fork() in which the given handler is executed. And that’s why our hooks are executed as well, because the forked zone in which the handler will be executed, simply inherits from the parent zone. There are some other methods that zone.js overrides by default and provides us as hooks:  Zone.setInterval()  Zone.alert()  Zone.prompt()  Zone.requestAnimationFrame()  Zone.addEventListener()  Zone.removeEventListener() We might wonder why methods like alert() and prompt() are patched as well. As mentioned earlier, those patched methods are hooks at the same time. We can change and extend them by forking a zone exactly the same way we did with ‘beforeTask’ and ‘afterTask’. This turns out to be super powerful, because we can intercept calls to alerts() and prompt() and change their behaviour when we write tests. Also zone.js is useful for debugging, testing, profiling. It helps you see whole call stack if you face with some error.