AngularJs Source code deep dive - What does invokeQueue
and configBlocks
do during angularjs bootstrapping? I see they are passed to arguments for runInvokeQueue
function inside loadModules
.
1 Answers
configBlocks
stores the list of services instantiated during module configuration:
angular.module("ng")._configBlocks[0][0]
invokeQueue
stores the list of methods invoked after the module is loaded:
var foo = angular.module("ng");
foo.constant(alert)
foo._invokeQueue[0]
foo._invokeQueue[0][2][0]
The
module
definition is situated at the first lines of AngularJS source. As the documentation says, the life span of everymodule
operation can be divided into two phases. Theconfig
phase and therun
phase. The object exposed by calling the angular.module() constructor provides us with these two methods. The functionality of the run method is pretty obvious out of the source. It simply puts theconfig
function passed to the private list ofrun
blocks. Theconfig
method, on the contrary, is not so obvious. Here is, what theconfig
method does:The
invokeLater
private method inserts the combination ofprovider
and its method to the internal module queue to be invoked later during the injector instantiation. The modules would be totally useless, if it weren't for the injector. TheCreateInjector
function is the concrete implementation and it has the private methodloadModules
.The
loadModules
function does, among other, the kickoff of modules which are passed to it. It returns one array of all therun
blocks of all modules which are dependent on each other. The injector calls them through itsinvoke
method. Last but not least, theloadModules
function, during its iteration through all dependent modules, calls their functions stored at_invokeQueue
and_configBlock
respectively. Therun
blocks are called in the last position. This means all services and methods of the modules are already exposed and ready for use. On the contrary, theconfig
method only works with providers exposed by the module. The constructors of the providers are stored in the_invokeQueue
of the module.Two injectors
The first injector, that is not exposed publicly, is an injector of providers. The second one is an instance injector. The instance injector asks the provider cache for the constructors needed. Then it calls the proper part of the provider and creates an instance. The instance injector searches in the provider cache before asking for the provider injector. The
module.provider
method exposes the provider injector's provider method, which at a closer look says a lot about the life cycle of services.Even other parts of angular constructs, like directives and controllers, are registered as providers in the provider's cache. The $compileProvider itself is then registered like any other provider through
$provide.provider
method. As a matter of fact,module.factory
,module.service
andmodule.provider
store an instance in the provider cache as a first step. But it is an instance injector that we have at our disposal. The instance injector asks the provider cache as a first step and then calls the invoke method, which takes care of the dependencies.
References

- 24,148
- 7
- 127
- 265