-
Notifications
You must be signed in to change notification settings - Fork 240
/
in_flight.js
66 lines (50 loc) · 1.35 KB
/
in_flight.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
var utils = require('utilities')
, EventEmitter = require('events').EventEmitter
, InFlight
, InFlightEntry;
var _timeout = null;
InFlightEntry = function (props) {
this.id = utils.string.uuid();
utils.mixin(this, props);
EventEmitter.call(this);
};
InFlightEntry.prototype = Object.create(EventEmitter.prototype);
InFlight = function () {
this.entries = new utils.SortedCollection();
};
InFlight.prototype = new (function () {
this.addEntry = function (data) {
var entry = new InFlightEntry(data);
this.entries.addItem(entry.id, entry);
if (_timeout) {
setTimeout(function () {
entry.emit('timeout', _timeout);
// If there's no controller to respond, at least
// output something useful
if (!entry.controller) {
entry.response.send('Request timed out', 504);
}
}, _timeout);
}
return entry;
};
this.setEntry = function (id, obj) {
this.entries.setItem(id, obj);
};
this.removeEntry = function (id) {
this.entries.removeItem(id);
};
this.getEntry = function (id) {
return this.entries.getItem(id);
};
this.getCount = function () {
return this.entries.count;
};
this.each = function (handler) {
this.entries.each(handler);
};
this.setTimeout = function (t) {
_timeout = t;
};
})();
module.exports = new InFlight();