Skip to content

Commit 8d6dc0b

Browse files
committed
del($eager): removed the support for $eager services
1 parent acbd7cd commit 8d6dc0b

7 files changed

Lines changed: 38 additions & 95 deletions

docs/content/guide/dev_guide.services.creating_services.ngdoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ The `angular.module.ng` method accepts three parameters:
1414
- `$inject` - {Array.<string>} - Array of service ids this service depends on. These services
1515
will be passed as arguments into the factory function in the same order specified in the `$inject`
1616
array. Defaults to `[]`.
17-
- `$eager` - {boolean} - If true, the service factory will be called and the service will be
18-
instantiated when angular boots. If false, the service will be lazily instantiated when it is first
19-
requested during instantiation of a dependant. Defaults to `false`.
2017

2118
The `this` of the factory function is bound to the root scope of the angular application.
2219

docs/content/guide/dev_guide.services.managing_dependencies.ngdoc

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,46 @@ provided by angular's web framework:
1414

1515
<pre>
1616
/**
17-
* batchLog service allows for messages to be queued in memory and flushed
18-
* to the console.log every 50 seconds.
19-
*
20-
* @param {*} message Message to be logged.
21-
*/
22-
angular.module.ng('batchLog', function($defer, $log) {
23-
var messageQueue = [];
24-
25-
function log() {
26-
if (messageQueue.length) {
27-
$log('batchLog messages: ', messageQueue);
28-
messageQueue = [];
29-
}
30-
$defer(log, 50000);
31-
}
17+
* batchLog service allows for messages to be queued in memory and flushed
18+
* to the console.log every 50 seconds.
19+
*
20+
* @param {*} message Message to be logged.
21+
*/
22+
function batchLogModule($provide){
23+
$provide.factory('batchLog', ['$defer', '$log', function($defer, $log) {
24+
var messageQueue = [];
25+
26+
function log() {
27+
if (messageQueue.length) {
28+
$log('batchLog messages: ', messageQueue);
29+
messageQueue = [];
30+
}
31+
$defer(log, 50000);
32+
}
3233

33-
// start periodic checking
34-
log();
34+
// start periodic checking
35+
log();
3536

36-
return function(message) {
37-
messageQueue.push(message);
37+
return function(message) {
38+
messageQueue.push(message);
39+
}
40+
}]);
41+
42+
/**
43+
* routeTemplateMonitor monitors each $route change and logs the current
44+
* template via the batchLog service.
45+
*/
46+
$provide.factory('routeTemplateMonitor',
47+
['$route', 'batchLog', '$rootScope',
48+
function($route, batchLog, $rootScope) {
49+
$rootScope.$on('$afterRouteChange', function() {
50+
batchLog($route.current ? $route.current.template : null);
51+
});
52+
}]);
3853
}
39-
}, {$inject: ['$defer', '$log']});
40-
// note how we declared dependency on built-in $defer and $log services above
4154

42-
/**
43-
* routeTemplateMonitor monitors each $route change and logs the current
44-
* template via the batchLog service.
45-
*/
46-
angular.module.ng('routeTemplateMonitor', function($route, batchLog) {
47-
this.$on('$afterRouteChange', function() {
48-
batchLog($route.current ? $route.current.template : null);
49-
});
50-
}, {$inject: ['$route', 'batchLog'], $eager: true});
55+
// get the main service to kick of the application
56+
angular.injector(batchLogModule).get('routeTemplateMonitor');
5157
</pre>
5258

5359
Things to notice in this example:
@@ -57,11 +63,6 @@ Things to notice in this example:
5763
`console.log` in batches.
5864
* The `routeTemplateMonitor` service depends on the built-in {@link api/angular.module.ng.$route
5965
$route} service as well as our custom `batchLog` service.
60-
* The `routeTemplateMonitor` service is declared to be eager, so that it is started as soon as the
61-
application starts.
62-
* To underline the need for the eager instantiation of the `routeTemplateMonitor` service, nothing
63-
else in the application depends on this service, and in this particular case the factory function
64-
of this service doesn't return anything at all.
6566
* Both of our services use the factory function signature as well as the `$inject` property to
6667
declare their dependencies. It is important that the order of the string identifiers in the array
6768
associated with the `$inject` property is the same as the order of argument names in the signature

docs/content/guide/dev_guide.services.registering_services.ngdoc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,9 @@ angular.module.ng('service id', function() {
3333
var shinyNewServiceInstance;
3434
//factory function body that constructs shinyNewServiceInstance
3535
return shinyNewServiceInstance;
36-
}, {$eager: true});
36+
});
3737
</pre>
3838

39-
While it is tempting to declare services as eager, only in few cases it is actually useful. If you
40-
are unsure whether to make a service eager, it likely doesn't need to be. To be more specific, a
41-
service should be declared as eager only if it fits one of these scenarios:
42-
4339
* Nothing in your application declares this service as its dependency, and this service affects the
4440
state or configuration of the application (e.g. a service that configures `$route` or `$resource`
4541
services)

example/temp.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$route.when('/view2', {controller: MyCtrl, template: 'view2.html'});
1010

1111
function MyCtrl() {};
12-
}, {$inject: ['$route'], $eager: true});
12+
}, {$inject: ['$route']});
1313
</script>
1414
</head>
1515
<body ng:init="$service('$window').$root = this">

src/Injector.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,5 @@ function createInjector(modulesToLoad, moduleRegistry) {
390390

391391
loadModule(modulesToLoad);
392392

393-
// instantiate $eager providers
394-
// for perf we can't do forEach
395-
for(var name in cache) {
396-
var index = name.indexOf(providerSuffix);
397-
if (index == name.length - providerSuffixLength && cache[name].$eager) {
398-
$injector(name.substring(1, index));
399-
}
400-
}
401-
402393
return $injector;
403394
}

test/AngularSpec.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,6 @@ describe('angular', function() {
389389
})('svc2')).toEqual('svc2-svc1');
390390
});
391391

392-
it('should eagerly instantiate a service if $eager is true', function() {
393-
var log = [];
394-
angular.injector(function($provide){
395-
$provide.service('svc1', function() {
396-
this.$get = function(){
397-
log.push('svc1');
398-
}
399-
this.$eager = true;
400-
});
401-
});
402-
expect(log).toEqual(['svc1']);
403-
});
404392
});
405393

406394

test/InjectorSpec.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ describe('injector', function() {
6767
}).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'.");
6868
});
6969

70-
it('should autostart eager services', function() {
71-
var log = '';
72-
injector = createInjector([function($provide){
73-
$provide.service('eager', function() {
74-
this.$eager = true;
75-
this.$get = function(){
76-
log += 'eager;';
77-
return 'foo';
78-
};
79-
});
80-
}]);
81-
expect(log).toEqual('eager;');
82-
expect(injector('eager')).toBe('foo');
83-
});
8470

8571
describe('invoke', function() {
8672
var args;
@@ -440,20 +426,4 @@ describe('injector', function() {
440426
}).toThrow('MyError');
441427
});
442428
});
443-
444-
describe('$eager', function(){
445-
it('should eagerly instantiate a service if $eager is true', function() {
446-
var log = [];
447-
createInjector([function($provide){
448-
$provide.service('svc1', function() {
449-
this.$get = function(){
450-
log.push('svc1');
451-
}
452-
this.$eager = true;
453-
});
454-
}]);
455-
expect(log).toEqual(['svc1']);
456-
});
457-
});
458-
459429
});

0 commit comments

Comments
 (0)