-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (60 loc) · 1.73 KB
/
index.js
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
// Generated by CoffeeScript 1.7.1
(function() {
var es, isFunction, _;
es = require('event-stream');
_ = require('lodash');
isFunction = function(f) {
return {}.toString.call(f) === '[object Function]';
};
module.exports = function(opts, callback) {
var buffer, bufferOrder, debouncedCallback, generateAggregatedStream;
if (isFunction(opts)) {
callback = opts;
opts = {};
}
if (!isFunction(callback)) {
throw new Error("Invalid callback provided: " + callback);
}
if (opts.debounce == null) {
opts.debounce = 50;
}
debouncedCallback = null;
if (opts.debounce === 0) {
debouncedCallback = function() {
return callback(generateAggregatedStream());
};
} else {
debouncedCallback = _.debounce(function() {
return callback(generateAggregatedStream());
}, opts.debounce);
}
bufferOrder = [];
buffer = {};
generateAggregatedStream = function() {
var nextReadIndex, thisBuffer, thisBufferOrder;
thisBufferOrder = _.clone(bufferOrder);
thisBuffer = _.clone(buffer);
nextReadIndex = 0;
return es.readable(function(count, endRead) {
while (count > 0) {
if (nextReadIndex >= thisBufferOrder.length) {
this.emit('end');
break;
}
this.emit('data', thisBuffer[thisBufferOrder[nextReadIndex]]);
nextReadIndex++;
count--;
}
return endRead();
});
};
return es.map(function(file, sendThrough) {
if (buffer[file.path] == null) {
bufferOrder.push(file.path);
}
buffer[file.path] = file;
debouncedCallback();
return sendThrough(null, file);
});
};
}).call(this);