-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·60 lines (49 loc) · 1.47 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
/**
* Created by gaurav on 6/7/15.
*/
/**
* Created by gaurav on 11/5/15.
*/
var eventCache = [];
var CACHE_SIZE = null;
var internals = {
mCallback : null,
bindTo: null
};
module.exports = internals.GoodCallback= function(events, config){
internals.mCallback = config.mCallback;
internals.bindTo = config.bindTo;
CACHE_SIZE = config.cache_size || 3;
};
var onEmitterStopped = function(){
// TODO: release occupied connection if any.
internals.bindTo==null?
internals.mCallback.apply(eventCache):
internals.mCallback.call(internals.bindTo, eventCache);
eventCache = null;
};
var provideCallback = function(chunk){
if(eventCache.length >= CACHE_SIZE-1){
// cache has reached the limit
var tempStorage = eventCache;
eventCache = [];
internals.bindTo==null?
internals.mCallback.apply(tempStorage):
internals.mCallback.call(internals.bindTo, tempStorage);
}else{
eventCache.push(chunk);
}
};
var onDataAvailable = function(chunk){
console.log('Event: %s ', chunk.event);
provideCallback(chunk);
};
internals.GoodCallback.prototype.init = function(readStream, emitter, callback){
emitter.on('stop', onEmitterStopped);
readStream.on('readable', function(){
//console.log('There is something to read');
});
var boundedOnDataAvailable = onDataAvailable.bind(this);
readStream.on('data', boundedOnDataAvailable);
callback();
};