diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/AngularPublic.js | 1 | ||||
| -rw-r--r-- | src/ng/defer.js | 6 | ||||
| -rw-r--r-- | src/ng/timeout.js | 87 | ||||
| -rw-r--r-- | src/ngMock/angular-mocks.js | 26 |
4 files changed, 119 insertions, 1 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js index a9124482..54536b43 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -125,6 +125,7 @@ function publishExternalAPI(angular){ $q: $QProvider, $sniffer: $SnifferProvider, $templateCache: $TemplateCacheProvider, + $timeout: $TimeoutProvider, $window: $WindowProvider }); } diff --git a/src/ng/defer.js b/src/ng/defer.js index f2a893bc..b5dc8844 100644 --- a/src/ng/defer.js +++ b/src/ng/defer.js @@ -3,6 +3,8 @@ /** * @ngdoc function * @name angular.module.ng.$defer + * @deprecated Made obsolete by $timeout service. Please migrate your code. This service will be + * removed with 1.0 final. * @requires $browser * * @description @@ -29,7 +31,9 @@ * @returns {boolean} Returns `true` if the task hasn't executed yet and was successfuly canceled. */ function $DeferProvider(){ - this.$get = ['$rootScope', '$browser', function($rootScope, $browser) { + 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); diff --git a/src/ng/timeout.js b/src/ng/timeout.js new file mode 100644 index 00000000..ac92bf8c --- /dev/null +++ b/src/ng/timeout.js @@ -0,0 +1,87 @@ +'use strict'; + + +function $TimeoutProvider() { + this.$get = ['$rootScope', '$browser', '$q', '$exceptionHandler', + function($rootScope, $browser, $q, $exceptionHandler) { + var deferreds = {}; + + + /** + * @ngdoc function + * @name angular.module.ng.$timeout + * @requires $browser + * + * @description + * Angular's wrapper for `window.setTimeout`. The `fn` function is wrapped into a try/catch + * block and delegates any exceptions to + * {@link angular.module.ng.$exceptionHandler $exceptionHandler} service. + * + * The return value of registering a timeout function is a promise which will be resolved when + * the timeout is reached and the timeout function is executed. + * + * To cancel a the timeout request, call `$timeout.cancel(promise)`. + * + * In tests you can use {@link angular.module.ngMock.$timeout `$timeout.flush()`} to + * synchronously flush the queue of deferred functions. + * + * @param {function()} fn A function, who's execution should be delayed. + * @param {number=} [delay=0] Delay in milliseconds. + * @param {boolean=} [invokeApply=true] If set to false skips model dirty checking, otherwise + * will invoke `fn` within the {@link angular.module.ng.$rootScope.Scope#$apply $apply} block. + * @returns {*} Promise that will be resolved when the timeout is reached. The value this + * promise will be resolved with is the return value of the `fn` function. + */ + function timeout(fn, delay, invokeApply) { + var deferred = $q.defer(), + promise = deferred.promise, + skipApply = (isDefined(invokeApply) && !invokeApply), + timeoutId, cleanup; + + timeoutId = $browser.defer(function() { + try { + deferred.resolve(fn()); + } catch(e) { + deferred.reject(e); + $exceptionHandler(e); + } + + if (!skipApply) $rootScope.$apply(); + }, delay); + + cleanup = function() { + delete deferreds[promise.$$timeoutId]; + }; + + promise.$$timeoutId = timeoutId; + deferreds[timeoutId] = deferred; + promise.then(cleanup, cleanup); + + return promise; + } + + + /** + * @ngdoc function + * @name angular.module.ng.$timeout#cancel + * @methodOf angular.module.ng.$timeout + * + * @description + * Cancels a task associated with the `promise`. As a result of this the promise will be + * resolved with a rejection. + * + * @param {Promise} promise Promise returned by the `$timeout` function. + * @returns {boolean} Returns `true` if the task hasn't executed yet and was successfully + * canceled. + */ + timeout.cancel = function(promise) { + if (promise.$$timeoutId in deferreds) { + deferreds[promise.$$timeoutId].reject('canceled'); + return $browser.defer.cancel(promise.$$timeoutId); + } + return false; + }; + + return timeout; + }]; +} diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 8b5d100a..ef1833e2 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1328,6 +1328,25 @@ function MockXhr() { this.abort = angular.noop; } + +/** + * @ngdoc function + * @name angular.module.ngMock.$timeout + * @description + * + * This service is just a simple decorator for {@link angular.module.ng.$timeout $timeout} service + * that adds a "flush" method. + */ + +/** + * @ngdoc method + * @name angular.module.ngMock.$timeout#flush + * @methodOf angular.module.ngMock.$timeout + * @description + * + * Flushes the queue of pending tasks. + */ + /** * @ngdoc overview * @name angular.module.ngMock @@ -1341,6 +1360,13 @@ angular.module('ngMock', ['ng']).provider({ $exceptionHandler: angular.mock.$ExceptionHandlerProvider, $log: angular.mock.$LogProvider, $httpBackend: angular.mock.$HttpBackendProvider +}).config(function($provide) { + $provide.decorator('$timeout', function($delegate, $browser) { + $delegate.flush = function() { + $browser.defer.flush(); + }; + return $delegate; + }); }); |
