aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatias Niemelä2014-03-14 12:01:45 -0400
committerMatias Niemelä2014-03-14 13:48:37 -0400
commitee8e4a946ed8f943e00846b88d8d51c0b2cd1fab (patch)
treeae7c8e617d9473c376b4e5634da563b083243ace
parenta41a2a1d2ce20f86ac2709592e4ada527160e580 (diff)
downloadangular.js-ee8e4a946ed8f943e00846b88d8d51c0b2cd1fab.tar.bz2
fix($$rAF): always fallback to a $timeout incase native rAF isn't supported
Closes #6654
-rw-r--r--src/ng/raf.js24
-rw-r--r--test/ng/rafSpec.js32
2 files changed, 48 insertions, 8 deletions
diff --git a/src/ng/raf.js b/src/ng/raf.js
index f85ee12a..0bc43f34 100644
--- a/src/ng/raf.js
+++ b/src/ng/raf.js
@@ -1,21 +1,29 @@
'use strict';
function $$RAFProvider(){ //rAF
- this.$get = ['$window', function($window) {
+ this.$get = ['$window', '$timeout', function($window, $timeout) {
var requestAnimationFrame = $window.requestAnimationFrame ||
$window.webkitRequestAnimationFrame;
var cancelAnimationFrame = $window.cancelAnimationFrame ||
$window.webkitCancelAnimationFrame;
- var raf = function(fn) {
- var id = requestAnimationFrame(fn);
- return function() {
- cancelAnimationFrame(id);
- };
- };
+ var rafSupported = !!requestAnimationFrame;
+ var raf = rafSupported
+ ? function(fn) {
+ var id = requestAnimationFrame(fn);
+ return function() {
+ cancelAnimationFrame(id);
+ };
+ }
+ : function(fn) {
+ var timer = $timeout(fn, 16.66, false); // 1000 / 60 = 16.666
+ return function() {
+ $timeout.cancel(timer);
+ };
+ };
- raf.supported = !!requestAnimationFrame;
+ raf.supported = rafSupported;
return raf;
}];
diff --git a/test/ng/rafSpec.js b/test/ng/rafSpec.js
index 6c15e2d2..8bf76efd 100644
--- a/test/ng/rafSpec.js
+++ b/test/ng/rafSpec.js
@@ -31,6 +31,38 @@ describe('$$rAF', function() {
expect(present).toBe(true);
}));
+ describe('$timeout fallback', function() {
+ it("it should use a $timeout incase native rAF isn't suppored", function() {
+ var timeoutSpy = jasmine.createSpy('callback');
+
+ //we need to create our own injector to work around the ngMock overrides
+ var injector = createInjector(['ng', function($provide) {
+ $provide.value('$timeout', timeoutSpy);
+ $provide.decorator('$window', function($delegate) {
+ $delegate.requestAnimationFrame = false;
+ $delegate.webkitRequestAnimationFrame = false;
+ $delegate.mozRequestAnimationFrame = false;
+ return $delegate;
+ });
+ }]);
+
+ var $$rAF = injector.get('$$rAF');
+ expect($$rAF.supported).toBe(false);
+
+ var message;
+ $$rAF(function() {
+ message = 'on';
+ });
+
+ expect(message).toBeUndefined();
+ expect(timeoutSpy).toHaveBeenCalled();
+
+ timeoutSpy.mostRecentCall.args[0]();
+
+ expect(message).toBe('on');
+ });
+ });
+
describe('mocks', function() {
it('should throw an error if no frames are present', inject(function($$rAF) {
if($$rAF.supported) {