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

Commit 04d7317

Browse files
committed
chore(core): introduce a wrapper for requestAnimationFrame
1 parent 2cd87db commit 04d7317

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

angularFiles.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ angularFiles = {
2626
'src/ng/log.js',
2727
'src/ng/parse.js',
2828
'src/ng/q.js',
29+
'src/ng/raf.js',
2930
'src/ng/rootScope.js',
3031
'src/ng/sanitizeUri.js',
3132
'src/ng/sce.js',

src/AngularPublic.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
$SnifferProvider,
7373
$TemplateCacheProvider,
7474
$TimeoutProvider,
75+
$$RAFProvider,
76+
$AsyncCallbackProvider,
7577
$WindowProvider
7678
*/
7779

@@ -211,7 +213,8 @@ function publishExternalAPI(angular){
211213
$sniffer: $SnifferProvider,
212214
$templateCache: $TemplateCacheProvider,
213215
$timeout: $TimeoutProvider,
214-
$window: $WindowProvider
216+
$window: $WindowProvider,
217+
$$rAF: $$RAFProvider
215218
});
216219
}
217220
]);

src/ng/raf.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
function $$RAFProvider(){ //rAF
4+
this.$get = ['$window', function($window) {
5+
var requestAnimationFrame = $window.requestAnimationFrame ||
6+
$window.webkitRequestAnimationFrame;
7+
8+
var cancelAnimationFrame = $window.cancelAnimationFrame ||
9+
$window.webkitCancelAnimationFrame;
10+
11+
var raf = function(fn) {
12+
var id = requestAnimationFrame(fn);
13+
return function() {
14+
cancelAnimationFrame(id);
15+
};
16+
};
17+
18+
raf.supported = !!requestAnimationFrame;
19+
20+
return raf;
21+
}];
22+
}

src/ngMock/angular-mocks.js

+29
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,34 @@ angular.mock.$TimeoutDecorator = function($delegate, $browser) {
16561656
return $delegate;
16571657
};
16581658

1659+
angular.mock.$RAFDecorator = function($delegate) {
1660+
var queue = [];
1661+
var rafFn = function(fn) {
1662+
var index = queue.length;
1663+
queue.push(fn);
1664+
return function() {
1665+
queue.splice(index, 1);
1666+
};
1667+
};
1668+
1669+
rafFn.supported = $delegate.supported;
1670+
1671+
rafFn.flush = function() {
1672+
if(queue.length === 0) {
1673+
throw new Error('No rAF callbacks present');
1674+
}
1675+
1676+
var length = queue.length;
1677+
for(var i=0;i<length;i++) {
1678+
queue[i]();
1679+
}
1680+
1681+
queue = [];
1682+
};
1683+
1684+
return rafFn;
1685+
};
1686+
16591687
/**
16601688
*
16611689
*/
@@ -1689,6 +1717,7 @@ angular.module('ngMock', ['ng']).provider({
16891717
$rootElement: angular.mock.$RootElementProvider
16901718
}).config(['$provide', function($provide) {
16911719
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
1720+
$provide.decorator('$$rAF', angular.mock.$RAFDecorator);
16921721
}]);
16931722

16941723
/**

test/ng/rafSpec.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
describe('$$rAF', function() {
4+
it('should queue and block animation frames', inject(function($$rAF) {
5+
if(!$$rAF.supported) return;
6+
7+
var message;
8+
$$rAF(function() {
9+
message = 'yes';
10+
});
11+
12+
expect(message).toBeUndefined();
13+
$$rAF.flush();
14+
expect(message).toBe('yes');
15+
}));
16+
17+
it('should provide a cancellation method', inject(function($$rAF) {
18+
if(!$$rAF.supported) return;
19+
20+
var present = true;
21+
var cancel = $$rAF(function() {
22+
present = false;
23+
});
24+
25+
expect(present).toBe(true);
26+
cancel();
27+
28+
try {
29+
$$rAF.flush();
30+
} catch(e) {};
31+
expect(present).toBe(true);
32+
}));
33+
34+
describe('mocks', function() {
35+
it('should throw an error if no frames are present', inject(function($$rAF) {
36+
if($$rAF.supported) {
37+
var failed = false;
38+
try {
39+
$$rAF.flush();
40+
} catch(e) {
41+
failed = true;
42+
}
43+
expect(failed).toBe(true);
44+
}
45+
}));
46+
});
47+
});

0 commit comments

Comments
 (0)