-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlistener.js
More file actions
133 lines (106 loc) · 2.58 KB
/
Copy pathlistener.js
File metadata and controls
133 lines (106 loc) · 2.58 KB
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
// listener
// A queue listener
// builtin
var EventEmitter = require('events').EventEmitter,
util = require('util');
// vendor
var _ = require('underscore');
/*
Example Usage
var listener = new Listener(blocking_operation, 10); // 10 maximum callbacks at once
listener.on('message', function (err, element, done) {
if (err) {
// do something
} else {
// do something else
}
done();
})
.start();
// some time later
listener.end();
*/
// -- Listener type --
// func - a function accepting a callback of error and element
// max_exec - A maximum number of elements executing
function Listener(func, max_out) {
this._func = func;
this._max = max_out || 0;
this._out = 0;
this._end = false;
this._ended = false;
this._listening = false;
var self = this;
this.on('done', function () {
self._out = Math.max(self._out - 1, 0);
if (self._end && self._out === 0 && !self._listenning) {
self.emit('end'); // We are ready to end
}
self.start();
});
this.on('end', function () {
this._ended = true;
});
}
util.inherits(Listener, EventEmitter);
Listener.prototype.ready = function () {
this.start()
this.emit('ready')
}
Listener.prototype.start = function () {
if (this._listening || this._end) {
// dont listen twice or listen if we've ended
return;
}
this._listening = true;
this._requeue();
return this;
};
Listener.prototype.end = function () {
this._ended = this._ended || this._out === 0;
if (this._ended) {
// Idempotency
this.emit('end');
}
this._end = true;
return this;
};
Listener.prototype.done = function () {
this.emit('done');
};
Listener.prototype._create_done = function () {
var self = this,
called = false;
return function () {
if (!called) {
self.emit('done');
called = true;
}
};
};
Listener.prototype._iteration = function iteration () {
var self = this;
// Check for end or max out
if (!self._end && (self._max === 0 || self._out < self._max)) {
// Call the blocking function
self._func(function (err, element) {
if (err) {
self.emit('error', err);
} else if (element) {
self._out++;
self.emit('message', element, self._create_done());
}
// Requeue the iteration
self._requeue();
});
} else {
self._listening = false;
if (self._end && self._out === 0) {
self.emit('end'); // We are ready to end
}
}
};
Listener.prototype._requeue = function () {
process.nextTick(_.bind(this._iteration, this));
};
module.exports = Listener;