UNPKG

621 BJavaScriptView Raw
1// --------------------
2// lose-cls-context module
3// --------------------
4
5// init queue
6var queue = [];
7
8// runs fns in queue
9function 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
19function clearQueueLater() {
20 setImmediate(clearQueue);
21}
22
23// start running queue
24clearQueueLater();
25
26// add fn to queue
27module.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};