This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +48
-8
lines changed
2 files changed +48
-8
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
function $$RAFProvider ( ) { //rAF
4
- this . $get = [ '$window' , function ( $window ) {
4
+ this . $get = [ '$window' , '$timeout' , function ( $window , $timeout ) {
5
5
var requestAnimationFrame = $window . requestAnimationFrame ||
6
6
$window . webkitRequestAnimationFrame ||
7
7
$window . mozRequestAnimationFrame ;
@@ -10,14 +10,22 @@ function $$RAFProvider(){ //rAF
10
10
$window . webkitCancelAnimationFrame ||
11
11
$window . mozCancelAnimationFrame ;
12
12
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
+ } ;
19
27
20
- raf . supported = ! ! requestAnimationFrame ;
28
+ raf . supported = rafSupported ;
21
29
22
30
return raf ;
23
31
} ] ;
Original file line number Diff line number Diff line change @@ -31,6 +31,38 @@ describe('$$rAF', function() {
31
31
expect ( present ) . toBe ( true ) ;
32
32
} ) ) ;
33
33
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
+
34
66
describe ( 'mocks' , function ( ) {
35
67
it ( 'should throw an error if no frames are present' , inject ( function ( $$rAF ) {
36
68
if ( $$rAF . supported ) {
You can’t perform that action at this time.
0 commit comments