|
23 | 23 | } |
24 | 24 | ]) |
25 | 25 |
|
26 | | - .factory('$firebaseUtils', ["$q", "$timeout", "firebaseBatchDelay", |
27 | | - function($q, $timeout, firebaseBatchDelay) { |
| 26 | + .factory('$firebaseUtils', ["$q", "$timeout", "$rootScope", |
| 27 | + function($q, $timeout, $rootScope) { |
28 | 28 |
|
29 | 29 | // ES6 style promises polyfill for angular 1.2.x |
30 | 30 | // Copied from angular 1.3.x implementation: https://github.com/angular/angular.js/blob/v1.3.5/src/ng/q.js#L539 |
|
50 | 50 |
|
51 | 51 | var utils = { |
52 | 52 | /** |
53 | | - * Returns a function which, each time it is invoked, will pause for `wait` |
54 | | - * milliseconds before invoking the original `fn` instance. If another |
55 | | - * request is received in that time, it resets `wait` up until `maxWait` is |
56 | | - * reached. |
| 53 | + * Returns a function which, each time it is invoked, will gather up the values until |
| 54 | + * the next "tick" in the Angular compiler process. Then they are all run at the same |
| 55 | + * time to avoid multiple cycles of the digest loop. Internally, this is done using $evalAsync() |
57 | 56 | * |
58 | | - * Unlike a debounce function, once wait is received, all items that have been |
59 | | - * queued will be invoked (not just once per execution). It is acceptable to use 0, |
60 | | - * which means to batch all synchronously queued items. |
61 | | - * |
62 | | - * The batch function actually returns a wrap function that should be called on each |
63 | | - * method that is to be batched. |
64 | | - * |
65 | | - * <pre><code> |
66 | | - * var total = 0; |
67 | | - * var batchWrapper = batch(10, 100); |
68 | | - * var fn1 = batchWrapper(function(x) { return total += x; }); |
69 | | - * var fn2 = batchWrapper(function() { console.log(total); }); |
70 | | - * fn1(10); |
71 | | - * fn2(); |
72 | | - * fn1(10); |
73 | | - * fn2(); |
74 | | - * console.log(total); // 0 (nothing invoked yet) |
75 | | - * // after 10ms will log "10" and then "20" |
76 | | - * </code></pre> |
77 | | - * |
78 | | - * @param {int} wait number of milliseconds to pause before sending out after each invocation |
79 | | - * @param {int} maxWait max milliseconds to wait before sending out, defaults to wait * 10 or 100 |
| 57 | + * @param {Function} action |
| 58 | + * @param {Object} [context] |
80 | 59 | * @returns {Function} |
81 | 60 | */ |
82 | | - batch: function(wait, maxWait) { |
83 | | - wait = typeof('wait') === 'number'? wait : firebaseBatchDelay; |
84 | | - if( !maxWait ) { maxWait = wait*10 || 100; } |
85 | | - var queue = []; |
86 | | - var start; |
87 | | - var cancelTimer; |
88 | | - var runScheduledForNextTick; |
89 | | - |
90 | | - // returns `fn` wrapped in a function that queues up each call event to be |
91 | | - // invoked later inside fo runNow() |
92 | | - function createBatchFn(fn, context) { |
93 | | - if( typeof(fn) !== 'function' ) { |
94 | | - throw new Error('Must provide a function to be batched. Got '+fn); |
95 | | - } |
96 | | - return function() { |
97 | | - var args = Array.prototype.slice.call(arguments, 0); |
98 | | - queue.push([fn, context, args]); |
99 | | - resetTimer(); |
100 | | - }; |
101 | | - } |
102 | | - |
103 | | - // clears the current wait timer and creates a new one |
104 | | - // however, if maxWait is exceeded, calls runNow() on the next tick. |
105 | | - function resetTimer() { |
106 | | - if( cancelTimer ) { |
107 | | - cancelTimer(); |
108 | | - cancelTimer = null; |
109 | | - } |
110 | | - if( start && Date.now() - start > maxWait ) { |
111 | | - if(!runScheduledForNextTick){ |
112 | | - runScheduledForNextTick = true; |
113 | | - utils.compile(runNow); |
114 | | - } |
115 | | - } |
116 | | - else { |
117 | | - if( !start ) { start = Date.now(); } |
118 | | - cancelTimer = utils.wait(runNow, wait); |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - // Clears the queue and invokes all of the functions awaiting notification |
123 | | - function runNow() { |
124 | | - cancelTimer = null; |
125 | | - start = null; |
126 | | - runScheduledForNextTick = false; |
127 | | - var copyList = queue.slice(0); |
128 | | - queue = []; |
129 | | - angular.forEach(copyList, function(parts) { |
130 | | - parts[0].apply(parts[1], parts[2]); |
| 61 | + batch: function(action, context) { |
| 62 | + return function() { |
| 63 | + var args = Array.prototype.slice.call(arguments, 0); |
| 64 | + $rootScope.$evalAsync(function() { |
| 65 | + action.apply(context, args); |
131 | 66 | }); |
132 | | - } |
133 | | - |
134 | | - return createBatchFn; |
| 67 | + }; |
135 | 68 | }, |
136 | 69 |
|
137 | 70 | /** |
|
492 | 425 | */ |
493 | 426 | VERSION: '0.0.0', |
494 | 427 |
|
495 | | - batchDelay: firebaseBatchDelay, |
496 | 428 | allPromises: $q.all.bind($q) |
497 | 429 | }; |
498 | 430 |
|
|
0 commit comments