-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
159 lines (157 loc) · 4.35 KB
/
publish.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
// Generated by CoffeeScript 1.9.2
var send;
send = require('./send');
module.exports = function(options) {
var channels, closechanneliffinshed, interval, intervaltick, messages, removemessageifcomplete, retrytimedoutmessages, startifstopped, stoptickiffinished, timeout;
if (options == null) {
options = {};
}
interval = options.interval, timeout = options.timeout;
if (interval == null) {
interval = 5000;
}
if (timeout == null) {
timeout = 5000;
}
channels = {};
messages = {};
intervaltick = null;
retrytimedoutmessages = function() {
var channel, channelstorecreate, completed, i, len, message, messagestoretry, msgid, name, now, ref, results;
now = process.hrtime();
messagestoretry = {};
for (msgid in messages) {
message = messages[msgid];
if (!(message.started < now + timeout)) {
continue;
}
messagestoretry[msgid] = message;
}
channelstorecreate = [];
for (msgid in messagestoretry) {
message = messagestoretry[msgid];
ref = message.channels;
for (name in ref) {
completed = ref[name];
if (completed) {
continue;
}
channelstorecreate.push(channels[name]);
}
}
for (i = 0, len = channelstorecreate.length; i < len; i++) {
channel = channelstorecreate[i];
channel.socket.close();
}
results = [];
for (msgid in messagestoretry) {
message = messagestoretry[msgid];
message.started = now;
results.push((function() {
var ref1, results1;
ref1 = message.channels;
results1 = [];
for (name in ref1) {
completed = ref1[name];
if (completed) {
continue;
}
channel = channels[name];
results1.push(channel.socket.send(msgid, message.data));
}
return results1;
})());
}
return results;
};
startifstopped = function() {
if (intervaltick != null) {
return;
}
return intervaltick = setInterval(retrytimedoutmessages, interval);
};
closechanneliffinshed = function(name, channel) {
if (Object.keys(channel.messages).length === 0) {
return channel.socket.close();
}
};
removemessageifcomplete = function(msgid, message) {
var callback, completed, i, len, name, ref, ref1;
ref = message.channels;
for (name in ref) {
completed = ref[name];
if (!completed) {
return;
}
}
ref1 = messages[msgid].callbacks;
for (i = 0, len = ref1.length; i < len; i++) {
callback = ref1[i];
callback();
}
delete messages[msgid];
return stoptickiffinished();
};
stoptickiffinished = function() {
if (Object.keys(messages).length === 0) {
clearInterval(intervaltick);
return intervaltick = null;
}
};
return {
register: function(name, addresses) {
if (channels[name] != null) {
channels[name].socket.connect(addresses);
return;
}
return channels[name] = {
socket: send(addresses, function(msgid) {
var channel, message;
channel = channels[name];
if (channel != null) {
delete channel.messages[msgid];
closechanneliffinshed(name, channel);
}
message = messages[msgid];
if ((message != null) && (message.channels[name] != null)) {
message.channels[name] = true;
return removemessageifcomplete(msgid, message);
}
}),
messages: {}
};
},
publish: function(msgid, data, names, oncomplete) {
var callbacks, i, len, name;
callbacks = [];
if (oncomplete != null) {
callbacks.push(oncomplete);
}
if (!(names instanceof Array)) {
names = [names];
}
messages[msgid] = {
started: process.hrtime(),
data: data,
callbacks: callbacks,
channels: {}
};
for (i = 0, len = names.length; i < len; i++) {
name = names[i];
messages[msgid].channels[name] = false;
channels[name].messages[msgid] = true;
channels[name].socket.send(msgid, data);
}
return startifstopped();
},
close: function() {
var _, channel;
for (_ in channels) {
channel = channels[_];
channel.socket.close();
}
channels = {};
return messages = {};
}
};
};