SlideShare a Scribd company logo
AngularJS. An overview of the
$provide’s API and $injector.
Yan Yankowski

Differences between
providers, factories, services, values,
constants, and decorators.
createInjector Method
•

providerCache – contains references to instantiated providers (after the body
function of the provider has been invoked as the provider’s constructor). All
custom factories and services in the application will be converted to providers and
registered here on application start. When the providerCache object is created it
always has $provide as the 1-st cached provider. This providerCache object also
has constants cached.
The $get method
hasn’t been
invoked.
• instanceCache – all instantiated providers (i.e.
whose method $get has been called) and
constants will be cached here for further
reusage.
A logManagerProvider’s $get
method has already been
invoked. Hence we see here
an instantiated service
object providing logging
functionality.
createInternalInjector Method
• How the provider is instantiated
1

providerCache

2

Important!
At this stage providerCache
dictionary is used by the
getService method.

The algorithm of the instantiation:
1.
Check whether the provider is already
instantiated (saved in the providerCache). If yes –
return it from the cache. Normally all the
providers defined in the application are already
there at this point.
2.
If the value in the cache points to the
INSTANTIATING token, then we are inside the
instantiation process of a dependency of some
other parentModule . The problem is that we are
also dependent on the parentModule and trying
to instantiate it as well. A chicken and an egg
problem.
3.
If no instance is found in the providerCache then
the exception will be thrown upon accessing the
key. It usually means that either the required
dependency is not provided or the js-file is not
included.

3
function() {
throw Error("Unknown provider: " + path.join(' <- '));
}
• How the instance of a service object is created
Important!
At this stage instanceCache
dictionary is used by the
getService method.

1
instanceCache

The algorithm of the instantiation:
1.

Check whether the instance is already
created (saved in the instanceCache). If yes –
return it from the instanceCache.
If the value in the cache points to the
INSTANTIATING token, then some other
service is simultaneously trying to instantiate
given object.
If no instance is found in the instanceCache
then the factory function takes the
responsibility of instantiating the object. The
instance is then cached.

2.

2

3.

3

The invoke method pipes the dependency names of the service,
instantiates each one of them with the getService method, then
loads the instantiated dependencies into the args array, iterates
over the args array and calls the provider body function, passing
it each dependency as a parameter.

function(servicename) {
var provider = providerInjector.get(servicename
+providerSuffix);
return instanceInjector.invoke(provider.$get, provider);
}
Provider
function provider(name, provider_) {
if (isFunction(provider_) || isArray(provider_)) {
provider_ = providerInjector.instantiate(provider_);
}
if (!provider_.$get) {
throw Error('Provider ' + name + ' must define $get factory method.');
}
return providerCache[name + providerSuffix] = provider_;
}

•
•

The name parameter is a string containing the name of the provider
The provider_ parameter must be one of the following three types:
1.
2.
3.

A function that returns an object with a $get method.
An array. In this case the last element of this array is always a function (cf. 1-st item of the list) or an object which has the $get method. All
the previous items of the array are treated as arguments to be injected upon the provider instantiation.
An object containing the method $get.
• Points of interest:
1) must define $get method (which in its turn
returns a factory/service object inside itself);
2) Uses providerInjector to retrieve its instance
(calls getService method internally, which in
it’s turn retrieves the instance of the provider
from the providerCache);
3) Once the $get method has been invoked the
instanceInjector will be used to retrieve the
instance of the created service object (from
instanceCache).
Factory
function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); }
The factory defines the body for the $get method of the underlying provider. We can see it by
looking at the above code block. Internally factory calls the provider registration method of the
$provide object, basically being a wrapper over it.
Service
function service(name, constructor) {
return factory(name, ['$injector', function($injector) {
return $injector.instantiate(constructor);
}]);
}

• uses $injector for instantiation;
• uses constructor function for instantiation – it
means that the service function is treated as a
constructor;
• When do I prefer Service to Factory ?
The service is preferable when you want to
define a constructor function to be instantiated
with new.
angular.service(“MyService”, function (){
this.foo;
});
Or
angular.service(“MyService”, MyConstructorFunction);

function MyConstructor(){ this.value }
MyConstructor.prototype.someFunction =
function(){ }

Will eventually be instantiated as :
$injector.instantiate(function (){
this.foo;
});
OR
new (function (){
this.foo;
});
So what is the main difference
between the Factory and the Service?
• Factory wrapper returns an instance of an
object.
• Service wrapper defines a constructor of an
object. This constructor will be later used to
instantiate a return object by the factory.
Value
function value(name, value) { return factory(name, valueFn(value)); }

function valueFn(value) {return function() {return value;};}
•

From the above code we see that the value method is a wrapper over factory
method. Hence a value is just one more layer over a provider registration method.

When to use?
• when you don’t need complicated logic and encapsulation;
• when you want to provide simple object for further injection.
Constant
function constant(name, value) {
providerCache[name] = value;
instanceCache[name] = value;
}

Important!
Both providerCache[constantName] and
instanceCache[constantName] point to
the same instance, which means that the
constant is equally usable during the
config and run stages..

• The constant object value can be accessed and used
during the configuration phase of the application. The
method $get of the providers hasn’t yet been called at
this stage, but the constants don’t need $get to be
called – they are already fully instantiated. So the
constants are the only objects inside the application
scope able to provide custom functionality at this
stage.
• When the application is started a new
instance of the constant object is placed into
providerCache and instanceCache (since no
call to the method $get is needed) .
The constant object
is fully available on
the application
configuration stage.
Good to know that …

•

•

The constant object is not interceptable by the decorator since it
lacks the $get function!
In the Jasmine testing framework using angular mock lib the mock
of the constant is created by using $provide.constant() method.
Decorator
function decorator(serviceName, decorFn) {
var origProvider = providerInjector.get(serviceName + providerSuffix),
orig$get = origProvider.$get;
origProvider.$get = function() {
var origInstance = instanceInjector.invoke(orig$get, origProvider);
return instanceInjector.invoke(decorFn, null, {$delegate: origInstance});
};
}

Get the provider to be
decorated
Save reference to
the original $get
method
Wrap the original
$get method
inside a new one.

• Use decorator to add functionality to the
existing services. Useful in cases when new
functionality has to be added into core
AngularJS services without touching the
source.
What exactly leads to circular
dependency exception.
• Suppose we have the following code:
window.mainApp = angular.module("mainApp", []);
mainApp.run(["mainLogger", function (mainLogger) {
mainLogger.log();
}]);
mainApp.service("mainLogger", [" secondaryLogger", function (secondaryLogger) {
this.log = function() {
console.log(); };
}]);
mainApp.service("secondaryLogger", ["mainLogger", function (mainLogger) {
this.log = function () { console.log(); };
}]);

Both services here are dependent on each other.
When a new service
(mainLogger) is
registered, its name is
first inserted as a key
into instanceCache
with the value pointing
to the INSTANTIATING
token. No actual
provider object is yet
created.

AngularJS then proceeds to creating mainLoggerProvider object: registers it in the providerCache. The
framework detects that the service has a dependency on secondaryLogger service. To resolve this dependency it
needs to create a secondaryLoggerProvider object, register it in the providerCache, and call its $get method in
order to create an instance of the secondaryLogger service (to inject into mainLoggerProvider). At this point the
framework sees the dependency on mainLoggerProvider and honestly tries to get it from the instanceCache or
to instantiate it. As we remember it is already in the instanceCache dictionary still pointing to the INSTANTIATING
token. Exception follows…

More Related Content

What's hot

AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
Eyal Vardi
 
Modules and injector
Modules and injectorModules and injector
Modules and injector
Eyal Vardi
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
Sergey Bolshchikov
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
Keith Bloomfield
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
Barcamp Saigon
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScript
Visual Engineering
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
Anuradha Bandara
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
Eyal Vardi
 
AngularJS Services
AngularJS ServicesAngularJS Services
AngularJS Services
Eyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
Eyal Vardi
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
FalafelSoftware
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
Eyal Vardi
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
Nir Kaufman
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
Visual Engineering
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
Dan Wahlin
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
Eyal Vardi
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
Eyal Vardi
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
Visual Engineering
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
Eyal Vardi
 

What's hot (20)

AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
Modules and injector
Modules and injectorModules and injector
Modules and injector
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScript
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
AngularJS Services
AngularJS ServicesAngularJS Services
AngularJS Services
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 

Similar to AngularJs $provide API internals & circular dependency problem.

ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency Injection
Dzmitry Ivashutsin
 
How to perform debounce in react
How to perform debounce in reactHow to perform debounce in react
How to perform debounce in react
BOSC Tech Labs
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
Pratchaya Suputsopon
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović
JS Belgrade
 
Full Stack Unit Testing
Full Stack Unit TestingFull Stack Unit Testing
Full Stack Unit Testing
GlobalLogic Ukraine
 
Build Widgets
Build WidgetsBuild Widgets
Build Widgets
scottw
 
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part OneAppcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Aaron Saunders
 
Spring Batch in Code - simple DB to DB batch applicaiton
Spring Batch in Code - simple DB to DB batch applicaitonSpring Batch in Code - simple DB to DB batch applicaiton
Spring Batch in Code - simple DB to DB batch applicaiton
tomi vanek
 
Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJS
Jim Lynch
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
Testing React hooks with the new act function
Testing React hooks with the new act functionTesting React hooks with the new act function
Testing React hooks with the new act function
Daniel Irvine
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
joearunraja2
 
Http programming in play
Http programming in playHttp programming in play
Http programming in play
Knoldus Inc.
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJava
Jobaer Chowdhury
 
Policy Injection in ASP.NET using Enterprise Library 3.0
Policy Injection in ASP.NET using Enterprise Library 3.0Policy Injection in ASP.NET using Enterprise Library 3.0
Policy Injection in ASP.NET using Enterprise Library 3.0
PhilWinstanley
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
Jim Lynch
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstitute
Suresh Loganatha
 

Similar to AngularJs $provide API internals & circular dependency problem. (20)

ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency Injection
 
How to perform debounce in react
How to perform debounce in reactHow to perform debounce in react
How to perform debounce in react
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović
 
Servlet11
Servlet11Servlet11
Servlet11
 
Full Stack Unit Testing
Full Stack Unit TestingFull Stack Unit Testing
Full Stack Unit Testing
 
Build Widgets
Build WidgetsBuild Widgets
Build Widgets
 
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part OneAppcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
 
Spring Batch in Code - simple DB to DB batch applicaiton
Spring Batch in Code - simple DB to DB batch applicaitonSpring Batch in Code - simple DB to DB batch applicaiton
Spring Batch in Code - simple DB to DB batch applicaiton
 
Intro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJSIntro to Unit Testing in AngularJS
Intro to Unit Testing in AngularJS
 
Servlet
ServletServlet
Servlet
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
 
Testing React hooks with the new act function
Testing React hooks with the new act functionTesting React hooks with the new act function
Testing React hooks with the new act function
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
 
Http programming in play
Http programming in playHttp programming in play
Http programming in play
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJava
 
Policy Injection in ASP.NET using Enterprise Library 3.0
Policy Injection in ASP.NET using Enterprise Library 3.0Policy Injection in ASP.NET using Enterprise Library 3.0
Policy Injection in ASP.NET using Enterprise Library 3.0
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
 
Rmi
RmiRmi
Rmi
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstitute
 

Recently uploaded

To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 

Recently uploaded (20)

To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 

AngularJs $provide API internals & circular dependency problem.

  • 1. AngularJS. An overview of the $provide’s API and $injector. Yan Yankowski Differences between providers, factories, services, values, constants, and decorators.
  • 2. createInjector Method • providerCache – contains references to instantiated providers (after the body function of the provider has been invoked as the provider’s constructor). All custom factories and services in the application will be converted to providers and registered here on application start. When the providerCache object is created it always has $provide as the 1-st cached provider. This providerCache object also has constants cached. The $get method hasn’t been invoked.
  • 3. • instanceCache – all instantiated providers (i.e. whose method $get has been called) and constants will be cached here for further reusage. A logManagerProvider’s $get method has already been invoked. Hence we see here an instantiated service object providing logging functionality.
  • 4. createInternalInjector Method • How the provider is instantiated 1 providerCache 2 Important! At this stage providerCache dictionary is used by the getService method. The algorithm of the instantiation: 1. Check whether the provider is already instantiated (saved in the providerCache). If yes – return it from the cache. Normally all the providers defined in the application are already there at this point. 2. If the value in the cache points to the INSTANTIATING token, then we are inside the instantiation process of a dependency of some other parentModule . The problem is that we are also dependent on the parentModule and trying to instantiate it as well. A chicken and an egg problem. 3. If no instance is found in the providerCache then the exception will be thrown upon accessing the key. It usually means that either the required dependency is not provided or the js-file is not included. 3 function() { throw Error("Unknown provider: " + path.join(' <- ')); }
  • 5. • How the instance of a service object is created Important! At this stage instanceCache dictionary is used by the getService method. 1 instanceCache The algorithm of the instantiation: 1. Check whether the instance is already created (saved in the instanceCache). If yes – return it from the instanceCache. If the value in the cache points to the INSTANTIATING token, then some other service is simultaneously trying to instantiate given object. If no instance is found in the instanceCache then the factory function takes the responsibility of instantiating the object. The instance is then cached. 2. 2 3. 3 The invoke method pipes the dependency names of the service, instantiates each one of them with the getService method, then loads the instantiated dependencies into the args array, iterates over the args array and calls the provider body function, passing it each dependency as a parameter. function(servicename) { var provider = providerInjector.get(servicename +providerSuffix); return instanceInjector.invoke(provider.$get, provider); }
  • 6. Provider function provider(name, provider_) { if (isFunction(provider_) || isArray(provider_)) { provider_ = providerInjector.instantiate(provider_); } if (!provider_.$get) { throw Error('Provider ' + name + ' must define $get factory method.'); } return providerCache[name + providerSuffix] = provider_; } • • The name parameter is a string containing the name of the provider The provider_ parameter must be one of the following three types: 1. 2. 3. A function that returns an object with a $get method. An array. In this case the last element of this array is always a function (cf. 1-st item of the list) or an object which has the $get method. All the previous items of the array are treated as arguments to be injected upon the provider instantiation. An object containing the method $get.
  • 7. • Points of interest: 1) must define $get method (which in its turn returns a factory/service object inside itself); 2) Uses providerInjector to retrieve its instance (calls getService method internally, which in it’s turn retrieves the instance of the provider from the providerCache); 3) Once the $get method has been invoked the instanceInjector will be used to retrieve the instance of the created service object (from instanceCache).
  • 8. Factory function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); } The factory defines the body for the $get method of the underlying provider. We can see it by looking at the above code block. Internally factory calls the provider registration method of the $provide object, basically being a wrapper over it.
  • 9. Service function service(name, constructor) { return factory(name, ['$injector', function($injector) { return $injector.instantiate(constructor); }]); } • uses $injector for instantiation; • uses constructor function for instantiation – it means that the service function is treated as a constructor;
  • 10. • When do I prefer Service to Factory ? The service is preferable when you want to define a constructor function to be instantiated with new. angular.service(“MyService”, function (){ this.foo; }); Or angular.service(“MyService”, MyConstructorFunction); function MyConstructor(){ this.value } MyConstructor.prototype.someFunction = function(){ } Will eventually be instantiated as : $injector.instantiate(function (){ this.foo; }); OR new (function (){ this.foo; });
  • 11. So what is the main difference between the Factory and the Service? • Factory wrapper returns an instance of an object. • Service wrapper defines a constructor of an object. This constructor will be later used to instantiate a return object by the factory.
  • 12. Value function value(name, value) { return factory(name, valueFn(value)); } function valueFn(value) {return function() {return value;};} • From the above code we see that the value method is a wrapper over factory method. Hence a value is just one more layer over a provider registration method. When to use? • when you don’t need complicated logic and encapsulation; • when you want to provide simple object for further injection.
  • 13. Constant function constant(name, value) { providerCache[name] = value; instanceCache[name] = value; } Important! Both providerCache[constantName] and instanceCache[constantName] point to the same instance, which means that the constant is equally usable during the config and run stages.. • The constant object value can be accessed and used during the configuration phase of the application. The method $get of the providers hasn’t yet been called at this stage, but the constants don’t need $get to be called – they are already fully instantiated. So the constants are the only objects inside the application scope able to provide custom functionality at this stage.
  • 14. • When the application is started a new instance of the constant object is placed into providerCache and instanceCache (since no call to the method $get is needed) . The constant object is fully available on the application configuration stage.
  • 15. Good to know that … • • The constant object is not interceptable by the decorator since it lacks the $get function! In the Jasmine testing framework using angular mock lib the mock of the constant is created by using $provide.constant() method.
  • 16. Decorator function decorator(serviceName, decorFn) { var origProvider = providerInjector.get(serviceName + providerSuffix), orig$get = origProvider.$get; origProvider.$get = function() { var origInstance = instanceInjector.invoke(orig$get, origProvider); return instanceInjector.invoke(decorFn, null, {$delegate: origInstance}); }; } Get the provider to be decorated Save reference to the original $get method Wrap the original $get method inside a new one. • Use decorator to add functionality to the existing services. Useful in cases when new functionality has to be added into core AngularJS services without touching the source.
  • 17. What exactly leads to circular dependency exception. • Suppose we have the following code: window.mainApp = angular.module("mainApp", []); mainApp.run(["mainLogger", function (mainLogger) { mainLogger.log(); }]); mainApp.service("mainLogger", [" secondaryLogger", function (secondaryLogger) { this.log = function() { console.log(); }; }]); mainApp.service("secondaryLogger", ["mainLogger", function (mainLogger) { this.log = function () { console.log(); }; }]); Both services here are dependent on each other.
  • 18. When a new service (mainLogger) is registered, its name is first inserted as a key into instanceCache with the value pointing to the INSTANTIATING token. No actual provider object is yet created. AngularJS then proceeds to creating mainLoggerProvider object: registers it in the providerCache. The framework detects that the service has a dependency on secondaryLogger service. To resolve this dependency it needs to create a secondaryLoggerProvider object, register it in the providerCache, and call its $get method in order to create an instance of the secondaryLogger service (to inject into mainLoggerProvider). At this point the framework sees the dependency on mainLoggerProvider and honestly tries to get it from the instanceCache or to instantiate it. As we remember it is already in the instanceCache dictionary still pointing to the INSTANTIATING token. Exception follows…