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

Commit

Permalink
fix($$rAF): always fallback to a $timeout incase native rAF isn't sup…
Browse files Browse the repository at this point in the history
…ported

Closes #6654
  • Loading branch information
matsko committed Mar 14, 2014
1 parent a41a2a1 commit ee8e4a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/ng/raf.js
Original file line number Diff line number Diff line change
@@ -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;
}];
Expand Down
32 changes: 32 additions & 0 deletions test/ng/rafSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ee8e4a9

Please sign in to comment.