-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
205 lines (143 loc) · 4.19 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
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
200
201
202
203
204
205
var msgStream = require('mage-message-stream.js');
/**
* @deprecated
* @type {Object}
*/
exports.transports = msgStream.transports;
function MsgServer(eventManager) {
this.futureLog = {}; // queues up events for soon or immediate emission
this.expectedMsgId = null;
this.stream = null;
this.sessionKey = null;
this.eventManager = eventManager;
}
/**
* Queues up messages for later emission
* @param {Object} messages
*/
MsgServer.prototype.addMessages = function (messages) {
if (!messages) {
return;
}
if (typeof messages !== 'object') {
throw new TypeError('Messages passed must be an object');
}
var msgIds = Object.keys(messages);
for (var i = 0; i < msgIds.length; i += 1) {
var msgId = msgIds[i];
var msgIdNum = parseInt(msgId, 10);
// register the message into the futureLog for later emission
this.futureLog[msgId] = messages[msgId];
// tell the message stream it may confirm this message as delivered
if (this.stream && this.stream.confirm) {
this.stream.confirm(msgId);
}
// make sure we are expecting the lowest possible msgId first
if (msgIdNum !== 0 && (this.expectedMsgId === null || msgIdNum < this.expectedMsgId)) {
this.expectedMsgId = msgIdNum;
}
}
};
/**
* Forgets about all currently registered messages. Required after a session key change.
*/
MsgServer.prototype.resetFutureLog = function () {
this.expectedMsgId = null;
this.futureLog = {};
};
MsgServer.prototype.emitEvents = function (msgId) {
var messages = this.futureLog[msgId];
delete this.futureLog[msgId];
// Emit the events in the message pack.
if (messages) {
this.eventManager.emitEvents(messages);
}
};
/**
* Emits as many messages as can be emitted without creating gaps in the flow of msgId keys
*/
MsgServer.prototype.emitFutureLog = function () {
// Keep emitting until we encounter a gap, or futureLog has simply gone empty
while (this.expectedMsgId && this.futureLog.hasOwnProperty(this.expectedMsgId)) {
// Early increment expectedMsgId, so that even if an event listener were to throw, the next
// time we call emitFutureLog, we know that we won't be expecting an old ID.
var msgId = this.expectedMsgId;
this.expectedMsgId += 1;
this.emitEvents(msgId);
}
// finally emit any events that don't have an ID and thus don't need confirmation and lack order
if (this.futureLog.hasOwnProperty('0')) {
this.emitEvents('0');
}
};
/**
* Kills the stream connection. Can be resumed later by calling start().
*/
MsgServer.prototype.abort = function () {
if (this.stream) {
this.stream.abort();
}
};
/**
* Starts or resumes (after abort() had been called) the stream connection.
*/
MsgServer.prototype.start = function () {
if (!this.stream) {
throw new Error('The message stream has not yet been set up');
}
this.stream.start();
};
/**
* Configures the message stream's transport types
*
* @param {Object} cfg
* @return {boolean} Returns true if succeeded to set up a transport, false otherwise.
*/
MsgServer.prototype.setupMessageStream = function (cfg) {
if (!cfg) {
return false;
}
var that = this;
var confirmIds = [];
// instantiate the event stream if needed
if (this.stream) {
confirmIds = this.stream.getUnconfirmed();
this.stream.destroy();
this.stream = null;
}
var stream = msgStream.create(cfg);
if (!stream) {
return false;
}
stream.on('error', function (error) {
console.warn('Error from message stream transport:', error);
});
stream.on('delivery', function (messages) {
try {
that.addMessages(messages);
that.emitFutureLog();
} catch (error) {
console.error('Error during message stream event emission:', error);
}
});
if (this.sessionKey) {
stream.setSessionKey(this.sessionKey);
}
for (var i = 0; i < confirmIds.length; i += 1) {
stream.confirm(confirmIds[i]);
}
this.stream = stream;
return true;
};
MsgServer.prototype.setSessionKey = function (sessionKey) {
if (!this.stream) {
throw new Error('The message stream has not yet been set up');
}
// Make sure any lingering messages are wiped out
if (sessionKey !== this.sessionKey) {
this.resetFutureLog();
this.sessionKey = sessionKey;
}
this.stream.setSessionKey(sessionKey);
};
module.exports = MsgServer;