Permalink
Browse files

fix($defer): remove deprecated $defer service

  • Loading branch information...
IgorMinar committed Jun 12, 2012
1 parent 74fa65e commit 9af7a9198e2d30608ea6c40eedde03e44a6ef569
View
@@ -13,7 +13,6 @@ angularFiles = {
'src/ng/cacheFactory.js',
'src/ng/compile.js',
'src/ng/controller.js',
'src/ng/defer.js',
'src/ng/document.js',
'src/ng/exceptionHandler.js',
'src/ng/interpolate.js',
@@ -50,15 +50,15 @@ provided by Angular's web framework:
* @param {*} message Message to be logged.
*/
function batchLogModule($provide){
$provide.factory('batchLog', ['$defer', '$log', function($defer, $log) {
$provide.factory('batchLog', ['$timeout', '$log', function($timeout, $log) {
var messageQueue = [];
function log() {
if (messageQueue.length) {
$log('batchLog messages: ', messageQueue);
messageQueue = [];
}
$defer(log, 50000);
$timeout(log, 50000);
}
// start periodic checking
@@ -88,7 +88,7 @@ provided by Angular's web framework:
Things to notice in this example:
* The `batchLog` service depends on the built-in {@link api/ng.$defer $defer} and
* The `batchLog` service depends on the built-in {@link api/ng.$timeout $timeout} and
{@link api/ng.$log $log} services, and allows messages to be logged into the
`console.log` in batches.
* The `routeTemplateMonitor` service depends on the built-in {@link api/ng.$route
@@ -259,7 +259,7 @@ the `$digest` phase. This delay is desirable, since it coalesces multiple model
api/ng.$rootScope.Scope#$apply scope.$apply()}. (Angular apis do this
implicitly, so no extra `$apply` call is needed when doing synchronous work in controllers,
or asynchronous work with {@link api/ng.$http $http} or {@link
api/ng.$defer $defer} services.
api/ng.$timeout $timeout} services.
4. **Mutation observation**
View
@@ -109,7 +109,6 @@ function publishExternalAPI(angular){
$browser: $BrowserProvider,
$cacheFactory: $CacheFactoryProvider,
$controller: $ControllerProvider,
$defer: $DeferProvider,
$document: $DocumentProvider,
$exceptionHandler: $ExceptionHandlerProvider,
$filter: $FilterProvider,
@@ -198,7 +198,7 @@ directive.ngEmbedApp = ['$templateCache', '$browser', '$rootScope', '$location',
}];
this.html5Mode = angular.noop;
});
$provide.decorator('$defer', ['$rootScope', '$delegate', function($rootScope, $delegate) {
$provide.decorator('$timeout', ['$rootScope', '$delegate', function($rootScope, $delegate) {
return angular.extend(function(fn, delay) {
if (delay && delay > 50) {
return setTimeout(function() {
View
@@ -1,49 +0,0 @@
'use strict';
/**
* @ngdoc function
* @name ng.$defer
* @deprecated Made obsolete by $timeout service. Please migrate your code. This service will be
* removed with 1.0 final.
* @requires $browser
*
* @description
* Delegates to {@link ng.$browser#defer $browser.defer}, but wraps the `fn` function
* into a try/catch block and delegates any exceptions to
* {@link ng.$exceptionHandler $exceptionHandler} service.
*
* In tests you can use `$browser.defer.flush()` to flush the queue of deferred functions.
*
* @param {function()} fn A function, who's execution should be deferred.
* @param {number=} [delay=0] of milliseconds to defer the function execution.
* @returns {*} DeferId that can be used to cancel the task via `$defer.cancel()`.
*/
/**
* @ngdoc function
* @name ng.$defer#cancel
* @methodOf ng.$defer
*
* @description
* Cancels a defered task identified with `deferId`.
*
* @param {*} deferId Token returned by the `$defer` function.
* @returns {boolean} Returns `true` if the task hasn't executed yet and was successfuly canceled.
*/
function $DeferProvider(){
this.$get = ['$rootScope', '$browser', '$log', function($rootScope, $browser, $log) {
$log.warn('$defer service has been deprecated, migrate to $timeout');
function defer(fn, delay) {
return $browser.defer(function() {
$rootScope.$apply(fn);
}, delay);
}
defer.cancel = function(deferId) {
return $browser.defer.cancel(deferId);
};
return defer;
}];
}
@@ -27,11 +27,6 @@ angular.mock = {};
*
* The api of this service is the same as that of the real {@link ng.$browser $browser}, except
* that there are several helper methods available which can be used in tests.
*
* The following apis can be used in tests:
*
* - $browser.defer — enables testing of code that uses
* {@link ng.$defer $defer} for executing functions via the `setTimeout` api.
*/
angular.mock.$BrowserProvider = function() {
this.$get = function(){
View
@@ -1,114 +0,0 @@
'use strict';
describe('$defer', function() {
beforeEach(module(function($provide) {
$provide.factory('$exceptionHandler', function(){
return jasmine.createSpy('$exceptionHandler');
});
$provide.value('$log', {warn: noop});
}));
it('should delegate functions to $browser.defer', inject(function($defer, $browser, $exceptionHandler) {
var counter = 0;
$defer(function() { counter++; });
expect(counter).toBe(0);
$browser.defer.flush();
expect(counter).toBe(1);
expect(function() {$browser.defer.flush();}).toThrow('No deferred tasks to be flushed');
expect(counter).toBe(1);
expect($exceptionHandler).not.toHaveBeenCalled();
}));
it('should delegate exception to the $exceptionHandler service', inject(function($defer, $browser, $exceptionHandler) {
$defer(function() {throw "Test Error";});
expect($exceptionHandler).not.toHaveBeenCalled();
$browser.defer.flush();
expect($exceptionHandler).toHaveBeenCalledWith("Test Error");
}));
it('should call $apply after each callback is executed', inject(function($defer, $browser, $rootScope) {
var applySpy = this.spyOn($rootScope, '$apply').andCallThrough();
$defer(function() {});
expect(applySpy).not.toHaveBeenCalled();
$browser.defer.flush();
expect(applySpy).toHaveBeenCalled();
applySpy.reset(); //reset the spy;
$defer(function() {});
$defer(function() {});
$browser.defer.flush();
expect(applySpy.callCount).toBe(2);
}));
it('should call $apply even if an exception is thrown in callback', inject(function($defer, $browser, $rootScope) {
var applySpy = this.spyOn($rootScope, '$apply').andCallThrough();
$defer(function() {throw "Test Error";});
expect(applySpy).not.toHaveBeenCalled();
$browser.defer.flush();
expect(applySpy).toHaveBeenCalled();
}));
it('should allow you to specify the delay time', inject(function($defer, $browser) {
var defer = this.spyOn($browser, 'defer');
$defer(noop, 123);
expect(defer.callCount).toEqual(1);
expect(defer.mostRecentCall.args[1]).toEqual(123);
}));
it('should return a cancelation token', inject(function($defer, $browser) {
var defer = this.spyOn($browser, 'defer').andReturn('xxx');
expect($defer(noop)).toEqual('xxx');
}));
describe('cancel', function() {
it('should cancel tasks', inject(function($defer, $browser) {
var task1 = jasmine.createSpy('task1'),
task2 = jasmine.createSpy('task2'),
task3 = jasmine.createSpy('task3'),
token1, token3;
token1 = $defer(task1);
$defer(task2);
token3 = $defer(task3, 333);
$defer.cancel(token3);
$defer.cancel(token1);
$browser.defer.flush();
expect(task1).not.toHaveBeenCalled();
expect(task2).toHaveBeenCalledOnce();
expect(task3).not.toHaveBeenCalled();
}));
it('should return true if a task was succesffuly canceled', inject(function($defer, $browser) {
var task1 = jasmine.createSpy('task1'),
task2 = jasmine.createSpy('task2'),
token1, token2;
token1 = $defer(task1);
$browser.defer.flush();
token2 = $defer(task2);
expect($defer.cancel(token1)).toBe(false);
expect($defer.cancel(token2)).toBe(true);
}));
});
});
@@ -989,7 +989,7 @@ describe('ngMockE2E', function() {
describe('autoflush', function() {
it('should flush responses via $defer', inject(function($browser) {
it('should flush responses via $browser.defer', inject(function($browser) {
hb.when('GET', '/foo').respond('bar');
hb('GET', '/foo', null, callback);

0 comments on commit 9af7a91

Please sign in to comment.