22
22
* @param {object } $log window.console or an object with the same interface.
23
23
* @param {object } $sniffer $sniffer service
24
24
*/
25
- function Browser ( window , document , $log , $sniffer ) {
26
- var ALL_TASKS_TYPE = '$$all$$' ,
27
- DEFAULT_TASK_TYPE = '$$default$$' ;
28
-
25
+ function Browser ( window , document , $log , $sniffer , $$taskTrackerFactory ) {
29
26
var self = this ,
30
27
location = window . location ,
31
28
history = window . history ,
32
29
setTimeout = window . setTimeout ,
33
30
clearTimeout = window . clearTimeout ,
34
31
pendingDeferIds = { } ,
35
- outstandingRequestCounts = { } ,
36
- outstandingRequestCallbacks = [ ] ;
32
+ taskTracker = $$taskTrackerFactory ( $log ) ;
37
33
38
34
self . isMock = false ;
39
35
40
- // TODO(vojta): remove this temporary api
41
- self . $$completeOutstandingRequest = completeOutstandingRequest ;
42
- self . $$incOutstandingRequestCount = incOutstandingRequestCount ;
43
-
44
- /**
45
- * Executes the `fn` function and decrements the appropriate `outstandingRequestCounts` counter.
46
- * If the counter reaches 0, all the corresponding `outstandingRequestCallbacks` are executed.
47
- * @param {Function } fn - The function to execute.
48
- * @param {string= } [taskType=DEFAULT_TASK_TYPE] The type of task that is being completed.
49
- */
50
- function completeOutstandingRequest ( fn , taskType ) {
51
- taskType = taskType || DEFAULT_TASK_TYPE ;
52
- try {
53
- fn ( ) ;
54
- } finally {
55
- decOutstandingRequestCount ( taskType ) ;
56
-
57
- var countForType = outstandingRequestCounts [ taskType ] ;
58
- var countForAll = outstandingRequestCounts [ ALL_TASKS_TYPE ] ;
59
-
60
- // If at least one of the queues (`ALL_TASKS_TYPE` or `taskType`) is empty, run callbacks.
61
- if ( ! countForAll || ! countForType ) {
62
- var getNextCallback = ! countForAll ? getLastCallback : getLastCallbackForType ;
63
- var nextCb ;
64
-
65
- while ( ( nextCb = getNextCallback ( taskType ) ) ) {
66
- try {
67
- nextCb ( ) ;
68
- } catch ( e ) {
69
- $log . error ( e ) ;
70
- }
71
- }
72
- }
73
- }
74
- }
75
-
76
- function decOutstandingRequestCount ( taskType ) {
77
- taskType = taskType || DEFAULT_TASK_TYPE ;
78
- if ( outstandingRequestCounts [ taskType ] ) {
79
- outstandingRequestCounts [ taskType ] -- ;
80
- outstandingRequestCounts [ ALL_TASKS_TYPE ] -- ;
81
- }
82
- }
83
-
84
- function incOutstandingRequestCount ( taskType ) {
85
- taskType = taskType || DEFAULT_TASK_TYPE ;
86
- outstandingRequestCounts [ taskType ] = ( outstandingRequestCounts [ taskType ] || 0 ) + 1 ;
87
- outstandingRequestCounts [ ALL_TASKS_TYPE ] = ( outstandingRequestCounts [ ALL_TASKS_TYPE ] || 0 ) + 1 ;
88
- }
89
-
90
- function getLastCallback ( ) {
91
- var cbInfo = outstandingRequestCallbacks . pop ( ) ;
92
- return cbInfo && cbInfo . cb ;
93
- }
94
-
95
- function getLastCallbackForType ( taskType ) {
96
- for ( var i = outstandingRequestCallbacks . length - 1 ; i >= 0 ; -- i ) {
97
- var cbInfo = outstandingRequestCallbacks [ i ] ;
98
- if ( cbInfo . type === taskType ) {
99
- outstandingRequestCallbacks . splice ( i , 1 ) ;
100
- return cbInfo . cb ;
101
- }
102
- }
103
- }
36
+ //////////////////////////////////////////////////////////////
37
+ // Task-tracking API
38
+ //////////////////////////////////////////////////////////////
104
39
105
- function getHash ( url ) {
106
- var index = url . indexOf ( '#' ) ;
107
- return index === - 1 ? '' : url . substr ( index ) ;
108
- }
40
+ // TODO(vojta): remove this temporary api
41
+ self . $$completeOutstandingRequest = taskTracker . completeTask ;
42
+ self . $$incOutstandingRequestCount = taskTracker . incTaskCount ;
109
43
110
- /**
111
- * @private
112
- * TODO(vojta): prefix this method with $$ ?
113
- * @param {function() } callback Function that will be called when no outstanding request.
114
- * @param {string= } [taskType=ALL_TASKS_TYPE] The type of tasks that will be waited for.
115
- */
116
- self . notifyWhenNoOutstandingRequests = function ( callback , taskType ) {
117
- taskType = taskType || ALL_TASKS_TYPE ;
118
- if ( ! outstandingRequestCounts [ taskType ] ) {
119
- callback ( ) ;
120
- } else {
121
- outstandingRequestCallbacks . push ( { type : taskType , cb : callback } ) ;
122
- }
123
- } ;
44
+ // TODO(vojta): prefix this method with $$ ?
45
+ self . notifyWhenNoOutstandingRequests = taskTracker . notifyWhenNoPendingTasks ;
124
46
125
47
//////////////////////////////////////////////////////////////
126
48
// URL API
@@ -140,6 +62,11 @@ function Browser(window, document, $log, $sniffer) {
140
62
141
63
cacheState ( ) ;
142
64
65
+ function getHash ( url ) {
66
+ var index = url . indexOf ( '#' ) ;
67
+ return index === - 1 ? '' : url . substr ( index ) ;
68
+ }
69
+
143
70
/**
144
71
* @name $browser#url
145
72
*
@@ -367,12 +294,12 @@ function Browser(window, document, $log, $sniffer) {
367
294
var timeoutId ;
368
295
369
296
delay = delay || 0 ;
370
- taskType = taskType || DEFAULT_TASK_TYPE ;
297
+ taskType = taskType || taskTracker . DEFAULT_TASK_TYPE ;
371
298
372
- incOutstandingRequestCount ( taskType ) ;
299
+ taskTracker . incTaskCount ( taskType ) ;
373
300
timeoutId = setTimeout ( function ( ) {
374
301
delete pendingDeferIds [ timeoutId ] ;
375
- completeOutstandingRequest ( fn , taskType ) ;
302
+ taskTracker . completeTask ( fn , taskType ) ;
376
303
} , delay ) ;
377
304
pendingDeferIds [ timeoutId ] = taskType ;
378
305
@@ -395,7 +322,7 @@ function Browser(window, document, $log, $sniffer) {
395
322
var taskType = pendingDeferIds [ deferId ] ;
396
323
delete pendingDeferIds [ deferId ] ;
397
324
clearTimeout ( deferId ) ;
398
- completeOutstandingRequest ( noop , taskType ) ;
325
+ taskTracker . completeTask ( noop , taskType ) ;
399
326
return true ;
400
327
}
401
328
return false ;
@@ -405,8 +332,8 @@ function Browser(window, document, $log, $sniffer) {
405
332
406
333
/** @this */
407
334
function $BrowserProvider ( ) {
408
- this . $get = [ '$window' , '$log' , '$sniffer' , '$document' ,
409
- function ( $window , $log , $sniffer , $document ) {
410
- return new Browser ( $window , $document , $log , $sniffer ) ;
411
- } ] ;
335
+ this . $get = [ '$window' , '$log' , '$sniffer' , '$document' , '$$taskTrackerFactory' ,
336
+ function ( $window , $log , $sniffer , $document , $$taskTrackerFactory ) {
337
+ return new Browser ( $window , $document , $log , $sniffer , $$taskTrackerFactory ) ;
338
+ } ] ;
412
339
}
0 commit comments