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

Commit 7b5e019

Browse files
committed
fix($$rAF): always fallback to a $timeout incase native rAF isn't supported
Closes #6654
1 parent 129e2e0 commit 7b5e019

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/ng/raf.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
function $$RAFProvider(){ //rAF
4-
this.$get = ['$window', function($window) {
4+
this.$get = ['$window', '$timeout', function($window, $timeout) {
55
var requestAnimationFrame = $window.requestAnimationFrame ||
66
$window.webkitRequestAnimationFrame ||
77
$window.mozRequestAnimationFrame;
@@ -10,14 +10,22 @@ function $$RAFProvider(){ //rAF
1010
$window.webkitCancelAnimationFrame ||
1111
$window.mozCancelAnimationFrame;
1212

13-
var raf = function(fn) {
14-
var id = requestAnimationFrame(fn);
15-
return function() {
16-
cancelAnimationFrame(id);
17-
};
18-
};
13+
var rafSupported = !!requestAnimationFrame;
14+
var raf = rafSupported
15+
? function(fn) {
16+
var id = requestAnimationFrame(fn);
17+
return function() {
18+
cancelAnimationFrame(id);
19+
};
20+
}
21+
: function(fn) {
22+
var timer = $timeout(fn, 16.66, false); // 1000 / 60 = 16.666
23+
return function() {
24+
$timeout.cancel(timer);
25+
};
26+
};
1927

20-
raf.supported = !!requestAnimationFrame;
28+
raf.supported = rafSupported;
2129

2230
return raf;
2331
}];

test/ng/rafSpec.js

+32
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ describe('$$rAF', function() {
3131
expect(present).toBe(true);
3232
}));
3333

34+
describe('$timeout fallback', function() {
35+
it("it should use a $timeout incase native rAF isn't suppored", function() {
36+
var timeoutSpy = jasmine.createSpy('callback');
37+
38+
//we need to create our own injector to work around the ngMock overrides
39+
var injector = createInjector(['ng', function($provide) {
40+
$provide.value('$timeout', timeoutSpy);
41+
$provide.decorator('$window', function($delegate) {
42+
$delegate.requestAnimationFrame = false;
43+
$delegate.webkitRequestAnimationFrame = false;
44+
$delegate.mozRequestAnimationFrame = false;
45+
return $delegate;
46+
});
47+
}]);
48+
49+
var $$rAF = injector.get('$$rAF');
50+
expect($$rAF.supported).toBe(false);
51+
52+
var message;
53+
$$rAF(function() {
54+
message = 'on';
55+
});
56+
57+
expect(message).toBeUndefined();
58+
expect(timeoutSpy).toHaveBeenCalled();
59+
60+
timeoutSpy.mostRecentCall.args[0]();
61+
62+
expect(message).toBe('on');
63+
});
64+
});
65+
3466
describe('mocks', function() {
3567
it('should throw an error if no frames are present', inject(function($$rAF) {
3668
if($$rAF.supported) {

0 commit comments

Comments
 (0)