aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatias Niemelä2014-02-24 19:21:20 -0500
committerMatias Niemelä2014-02-24 21:23:10 -0500
commit62761428eff3a53e69367449eb81869e59e75e39 (patch)
tree40ce8ffa232cc38c821c78c29b0b811c45e406db
parent04d7317cdd95ba00783389f89f6e9a7e1fc418f8 (diff)
downloadangular.js-62761428eff3a53e69367449eb81869e59e75e39.tar.bz2
chore(core): create a wrapper to manage async callbacks
-rwxr-xr-xangularFiles.js1
-rw-r--r--src/AngularPublic.js5
-rw-r--r--src/ng/asyncCallback.js11
-rw-r--r--src/ngMock/angular-mocks.js15
-rw-r--r--test/ng/asyncCallbackSpec.js33
5 files changed, 63 insertions, 2 deletions
diff --git a/angularFiles.js b/angularFiles.js
index 3cbc7ece..1647ba48 100755
--- a/angularFiles.js
+++ b/angularFiles.js
@@ -11,6 +11,7 @@ angularFiles = {
'src/ng/anchorScroll.js',
'src/ng/animate.js',
+ 'src/ng/asyncCallback.js',
'src/ng/browser.js',
'src/ng/cacheFactory.js',
'src/ng/compile.js',
diff --git a/src/AngularPublic.js b/src/AngularPublic.js
index 3870c519..0c02adec 100644
--- a/src/AngularPublic.js
+++ b/src/AngularPublic.js
@@ -73,7 +73,7 @@
$TemplateCacheProvider,
$TimeoutProvider,
$$RAFProvider,
- $AsyncCallbackProvider,
+ $$AsyncCallbackProvider,
$WindowProvider
*/
@@ -214,7 +214,8 @@ function publishExternalAPI(angular){
$templateCache: $TemplateCacheProvider,
$timeout: $TimeoutProvider,
$window: $WindowProvider,
- $$rAF: $$RAFProvider
+ $$rAF: $$RAFProvider,
+ $$asyncCallback : $$AsyncCallbackProvider
});
}
]);
diff --git a/src/ng/asyncCallback.js b/src/ng/asyncCallback.js
new file mode 100644
index 00000000..8bede73a
--- /dev/null
+++ b/src/ng/asyncCallback.js
@@ -0,0 +1,11 @@
+'use strict';
+
+function $$AsyncCallbackProvider(){
+ this.$get = ['$$rAF', '$timeout', function($$rAF, $timeout) {
+ return $$rAF.supported
+ ? function(fn) { return $$rAF(fn); }
+ : function(fn) {
+ return $timeout(fn, 0, false);
+ };
+ }];
+}
diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js
index efde0f3a..bcd6cc1f 100644
--- a/src/ngMock/angular-mocks.js
+++ b/src/ngMock/angular-mocks.js
@@ -1684,6 +1684,20 @@ angular.mock.$RAFDecorator = function($delegate) {
return rafFn;
};
+angular.mock.$AsyncCallbackDecorator = function($delegate) {
+ var callbacks = [];
+ var addFn = function(fn) {
+ callbacks.push(fn);
+ };
+ addFn.flush = function() {
+ angular.forEach(callbacks, function(fn) {
+ fn();
+ });
+ callbacks = [];
+ };
+ return addFn;
+};
+
/**
*
*/
@@ -1718,6 +1732,7 @@ angular.module('ngMock', ['ng']).provider({
}).config(['$provide', function($provide) {
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
$provide.decorator('$$rAF', angular.mock.$RAFDecorator);
+ $provide.decorator('$$asyncCallback', angular.mock.$AsyncCallbackDecorator);
}]);
/**
diff --git a/test/ng/asyncCallbackSpec.js b/test/ng/asyncCallbackSpec.js
new file mode 100644
index 00000000..f9bbe781
--- /dev/null
+++ b/test/ng/asyncCallbackSpec.js
@@ -0,0 +1,33 @@
+'use strict';
+describe('$$asyncCallback', function() {
+ it('should perform a callback asynchronously', inject(function($$asyncCallback) {
+ var message = 'hello there ';
+ $$asyncCallback(function() {
+ message += 'Angular';
+ });
+
+ expect(message).toBe('hello there ');
+ $$asyncCallback.flush();
+ expect(message).toBe('hello there Angular');
+ }));
+
+ describe('mocks', function() {
+ it('should queue up all async callbacks', inject(function($$asyncCallback) {
+ var callback = jasmine.createSpy('callback');
+ $$asyncCallback(callback);
+ $$asyncCallback(callback);
+ $$asyncCallback(callback);
+ expect(callback.callCount).toBe(0);
+
+ $$asyncCallback.flush();
+ expect(callback.callCount).toBe(3);
+
+ $$asyncCallback(callback);
+ $$asyncCallback(callback);
+ expect(callback.callCount).toBe(3);
+
+ $$asyncCallback.flush();
+ expect(callback.callCount).toBe(5);
+ }));
+ });
+});