1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"use strict";
var utils = require("./utils");
module.exports = function batchProcessorMaker(options) {
options = options || {};
var reporter = options.reporter;
var asyncProcess = utils.getOption(options, "async", true);
var autoProcess = utils.getOption(options, "auto", true);
if(autoProcess && !asyncProcess) {
reporter && reporter.warn("Invalid options combination. auto=true and async=false is invalid. Setting async=true.");
asyncProcess = true;
}
var batch = Batch();
var asyncFrameHandler;
var isProcessing = false;
function addFunction(level, fn) {
if(!isProcessing && autoProcess && asyncProcess && batch.size() === 0) {
// Since this is async, it is guaranteed to be executed after that the fn is added to the batch.
// This needs to be done before, since we're checking the size of the batch to be 0.
processBatchAsync();
}
batch.add(level, fn);
}
function processBatch() {
// Save the current batch, and create a new batch so that incoming functions are not added into the currently processing batch.
// Continue processing until the top-level batch is empty (functions may be added to the new batch while processing, and so on).
isProcessing = true;
while (batch.size()) {
var processingBatch = batch;
batch = Batch();
processingBatch.process();
}
isProcessing = false;
}
function forceProcessBatch(localAsyncProcess) {
if (isProcessing) {
return;
}
if(localAsyncProcess === undefined) {
localAsyncProcess = asyncProcess;
}
if(asyncFrameHandler) {
cancelFrame(asyncFrameHandler);
asyncFrameHandler = null;
}
if(localAsyncProcess) {
processBatchAsync();
} else {
processBatch();
}
}
function processBatchAsync() {
asyncFrameHandler = requestFrame(processBatch);
}
function clearBatch() {
batch = {};
batchSize = 0;
topLevel = 0;
bottomLevel = 0;
}
function cancelFrame(listener) {
// var cancel = window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame || window.clearTimeout;
var cancel = clearTimeout;
return cancel(listener);
}
function requestFrame(callback) {
// var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || function(fn) { return window.setTimeout(fn, 20); };
var raf = function(fn) { return setTimeout(fn, 0); };
return raf(callback);
}
return {
add: addFunction,
force: forceProcessBatch
};
};
function Batch() {
var batch = {};
var size = 0;
var topLevel = 0;
var bottomLevel = 0;
function add(level, fn) {
if(!fn) {
fn = level;
level = 0;
}
if(level > topLevel) {
topLevel = level;
} else if(level < bottomLevel) {
bottomLevel = level;
}
if(!batch[level]) {
batch[level] = [];
}
batch[level].push(fn);
size++;
}
function process() {
for(var level = bottomLevel; level <= topLevel; level++) {
var fns = batch[level];
for(var i = 0; i < fns.length; i++) {
var fn = fns[i];
fn();
}
}
}
function getSize() {
return size;
}
return {
add: add,
process: process,
size: getSize
};
}