Skip to content

Commit

Permalink
chore(core): create a wrapper to manage async callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
matsko committed Feb 25, 2014
1 parent 04d7317 commit 6276142
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions angularFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
$TemplateCacheProvider,
$TimeoutProvider,
$$RAFProvider,
$AsyncCallbackProvider,
$$AsyncCallbackProvider,
$WindowProvider
*/

Expand Down Expand Up @@ -214,7 +214,8 @@ function publishExternalAPI(angular){
$templateCache: $TemplateCacheProvider,
$timeout: $TimeoutProvider,
$window: $WindowProvider,
$$rAF: $$RAFProvider
$$rAF: $$RAFProvider,
$$asyncCallback : $$AsyncCallbackProvider
});
}
]);
Expand Down
11 changes: 11 additions & 0 deletions src/ng/asyncCallback.js
Original file line number Diff line number Diff line change
@@ -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);
};
}];
}
15 changes: 15 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
*
*/
Expand Down Expand Up @@ -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);
}]);

/**
Expand Down
33 changes: 33 additions & 0 deletions test/ng/asyncCallbackSpec.js
Original file line number Diff line number Diff line change
@@ -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);
}));
});
});

0 comments on commit 6276142

Please sign in to comment.