-
Notifications
You must be signed in to change notification settings - Fork 7
/
queue.js
199 lines (191 loc) · 5.22 KB
/
queue.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* This module provides queuing functionality with distributed message support.
*/
var connector = require("./connector"),
hub = require("./hub");
var connections = {};
var hubConnections = [];
var leastRecentlyUsed, mostRecentlyUsed;
exports.expirationTime = 15000;
// this gets a queue that can be used to add events and pull events
exports.getQueue = function(clientId){
if(clientId){
if(connections[clientId]){
return connections[clientId];
}
var clientConnection = connections[clientId] = [];
clientConnection.clientId = clientId;
}else{
var clientConnection = [];
}
clientConnection.send = function(message, dontPropagate){
clientConnection.push(message);
clientConnection.emit("message", message);
}
clientConnection.__proto__ = connectionPrototype;
//lastUsedQueue.push
return clientConnection;
};
var startedTimer;
var connectionPrototype = {
setActive : function(active){
if(active){
// remove from inactive queue
if (this.previous) {
// adjust the most recently used connection
if (mostRecentlyUsed === this) {
mostRecentlyUsed = this.previous;
}
this.previous.next = this.next;
if (this.previous === this.next) {
// something went wrong with our queue. quite possibly something has a stale previous or next
// reference
throw new Error("oh no");
}
}
if (this.next) {
// adjust the least recently used connection
if (leastRecentlyUsed === this) {
leastRecentlyUsed = this.next;
}
this.next.previous = this.previous;
}
this.next = this.previous = null;
this.active = true;
}else if(this.active){
// add to inactive queue
this.lastTouched = new Date().getTime();
this.active = false;
if(mostRecentlyUsed && mostRecentlyUsed != this){
mostRecentlyUsed.next = this;
this.previous = mostRecentlyUsed;
}
mostRecentlyUsed = this;
if(!startedTimer && global.setInterval){
startedTimer = true;
if(!leastRecentlyUsed){
leastRecentlyUsed = this;
}
setInterval(function(){
var thresholdTime = new Date().getTime() - exports.expirationTime;
while(leastRecentlyUsed && !leastRecentlyUsed.active && leastRecentlyUsed.lastTouched < thresholdTime){
leastRecentlyUsed.emit("clientClose");
if(leastRecentlyUsed._onmessage && leastRecentlyUsed._onmessage.length){
leastRecentlyUsed.timedOut = true;
}else{
leastRecentlyUsed.close();
}
leastRecentlyUsed = leastRecentlyUsed.next;
if (leastRecentlyUsed) {
leastRecentlyUsed.previous = null;
}
}
}, 1000);
}
}
},
connect: function(){
// connects to the connected set of workers and/or other machines
var clientId = this.clientId;
if(clientId){
hubConnections.forEach(function(connection){
connection.send({
requestedClientId: clientId,
active: true
});
});
this.observe("clientClose", function(){
hubConnections.forEach(function(connection){
connection.send({
requestedClientId: clientId,
active: false
});
});
});
}
},
splice: function(){
this.setActive(true);
this.setActive(false);
return Array.prototype.splice.apply(this, arguments);
},
close: function(){
delete connections[this.clientId];
if(this.closed){
return;
}
this.closed = true;
this.emit("close");
},
observe: function(name, callback, nonClient){
var callbacks = this["_on" + name];
if(!callbacks){
callbacks = this["_on" + name] = [];
}
callbacks.push(callback);
if(name === "message" && !nonClient){
if(!this.startedHere){
this.connect();
}
this.setActive(true);
}
var connection = this;
return {
dismiss: function(){
connection.setActive(false);
callbacks.splice(callbacks.indexOf(callback), 1);
if(callbacks.length == 0 && connection.timedOut){
connection.close();
}
}
}
},
addListener: function(){
return this.observe.apply(this, arguments);
},
emit: function(name, event){
var callbacks = this["_on" + name];
if(callbacks){
callbacks.forEach(function(callback){
callback(event);
});
}
}
}
connectionPrototype.__proto__ = Array.prototype;
connector.observe("connector", function(hubConnection){
hubConnections.push(hubConnection);
hubConnection.on("message", function(message){
if(message.requestedClientId){
var requestedClientId = message.requestedClientId;
if(message.active){
var connection = connections[requestedClientId];
if(connection){
var onMessage = function(message){
if(!message._fromHub){
hubConnection.send({
forClientId: requestedClientId,
message: message
});
}
};
if(connection.length > 0){
connection.splice(0, connection.length).forEach(onMessage);
}
hubConnection["clientId" + requestedClientId] = connection.observe("message", onMessage, true);
}
}else{
var observer = hubConnection["clientId" + message.requestedClientId];
if(observer){
observer.dismiss();
}else{
//print("observer for " + message.requestedClientId + " not found");
}
}
}
if(message.forClientId){
Object.defineProperty(message.message, "_fromHub", {enumerate: false, value: true});
connections[message.forClientId].send(message.message, true);
}
});
});