mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-19 00:06:30 +08:00
fix($$rAF): always fallback to a $timeout incase native rAF isn't supported
Closes #6654
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
function $$RAFProvider(){ //rAF
|
||||
this.$get = ['$window', function($window) {
|
||||
this.$get = ['$window', '$timeout', function($window, $timeout) {
|
||||
var requestAnimationFrame = $window.requestAnimationFrame ||
|
||||
$window.webkitRequestAnimationFrame ||
|
||||
$window.mozRequestAnimationFrame;
|
||||
@@ -10,14 +10,22 @@ function $$RAFProvider(){ //rAF
|
||||
$window.webkitCancelAnimationFrame ||
|
||||
$window.mozCancelAnimationFrame;
|
||||
|
||||
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;
|
||||
}];
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user