1 | // --------------------
|
2 | // lose-cls-context module
|
3 | // --------------------
|
4 |
|
5 | // init queue
|
6 | var queue = [];
|
7 |
|
8 | // runs fns in queue
|
9 | function clearQueue() {
|
10 | // get item at front of queue and call fn
|
11 | var fn = queue.shift();
|
12 | if (fn) fn();
|
13 |
|
14 | // run again on next tick
|
15 | clearQueueLater();
|
16 | }
|
17 |
|
18 | // schedules running fns in queue on next tick
|
19 | function clearQueueLater() {
|
20 | setImmediate(clearQueue);
|
21 | }
|
22 |
|
23 | // start running queue
|
24 | clearQueueLater();
|
25 |
|
26 | // add fn to queue
|
27 | module.exports = function(fn) {
|
28 | if (!fn || typeof fn != 'function') throw new Error('fn must be a function');
|
29 |
|
30 | queue.push(fn.bind(this));
|
31 | };
|