Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 6276142

Browse files
committed
chore(core): create a wrapper to manage async callbacks
1 parent 04d7317 commit 6276142

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

angularFiles.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ angularFiles = {
1111

1212
'src/ng/anchorScroll.js',
1313
'src/ng/animate.js',
14+
'src/ng/asyncCallback.js',
1415
'src/ng/browser.js',
1516
'src/ng/cacheFactory.js',
1617
'src/ng/compile.js',

src/AngularPublic.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
$TemplateCacheProvider,
7474
$TimeoutProvider,
7575
$$RAFProvider,
76-
$AsyncCallbackProvider,
76+
$$AsyncCallbackProvider,
7777
$WindowProvider
7878
*/
7979

@@ -214,7 +214,8 @@ function publishExternalAPI(angular){
214214
$templateCache: $TemplateCacheProvider,
215215
$timeout: $TimeoutProvider,
216216
$window: $WindowProvider,
217-
$$rAF: $$RAFProvider
217+
$$rAF: $$RAFProvider,
218+
$$asyncCallback : $$AsyncCallbackProvider
218219
});
219220
}
220221
]);

src/ng/asyncCallback.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
function $$AsyncCallbackProvider(){
4+
this.$get = ['$$rAF', '$timeout', function($$rAF, $timeout) {
5+
return $$rAF.supported
6+
? function(fn) { return $$rAF(fn); }
7+
: function(fn) {
8+
return $timeout(fn, 0, false);
9+
};
10+
}];
11+
}

src/ngMock/angular-mocks.js

+15
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,20 @@ angular.mock.$RAFDecorator = function($delegate) {
16841684
return rafFn;
16851685
};
16861686

1687+
angular.mock.$AsyncCallbackDecorator = function($delegate) {
1688+
var callbacks = [];
1689+
var addFn = function(fn) {
1690+
callbacks.push(fn);
1691+
};
1692+
addFn.flush = function() {
1693+
angular.forEach(callbacks, function(fn) {
1694+
fn();
1695+
});
1696+
callbacks = [];
1697+
};
1698+
return addFn;
1699+
};
1700+
16871701
/**
16881702
*
16891703
*/
@@ -1718,6 +1732,7 @@ angular.module('ngMock', ['ng']).provider({
17181732
}).config(['$provide', function($provide) {
17191733
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
17201734
$provide.decorator('$$rAF', angular.mock.$RAFDecorator);
1735+
$provide.decorator('$$asyncCallback', angular.mock.$AsyncCallbackDecorator);
17211736
}]);
17221737

17231738
/**

test/ng/asyncCallbackSpec.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
describe('$$asyncCallback', function() {
3+
it('should perform a callback asynchronously', inject(function($$asyncCallback) {
4+
var message = 'hello there ';
5+
$$asyncCallback(function() {
6+
message += 'Angular';
7+
});
8+
9+
expect(message).toBe('hello there ');
10+
$$asyncCallback.flush();
11+
expect(message).toBe('hello there Angular');
12+
}));
13+
14+
describe('mocks', function() {
15+
it('should queue up all async callbacks', inject(function($$asyncCallback) {
16+
var callback = jasmine.createSpy('callback');
17+
$$asyncCallback(callback);
18+
$$asyncCallback(callback);
19+
$$asyncCallback(callback);
20+
expect(callback.callCount).toBe(0);
21+
22+
$$asyncCallback.flush();
23+
expect(callback.callCount).toBe(3);
24+
25+
$$asyncCallback(callback);
26+
$$asyncCallback(callback);
27+
expect(callback.callCount).toBe(3);
28+
29+
$$asyncCallback.flush();
30+
expect(callback.callCount).toBe(5);
31+
}));
32+
});
33+
});

0 commit comments

Comments
 (0)