-
Notifications
You must be signed in to change notification settings - Fork 55
/
messagequeue.js
72 lines (59 loc) · 2.02 KB
/
messagequeue.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
import Utils from '../util/utils';
import EventEmitter from '../util/eventemitter';
import Logger from '../util/logger';
var MessageQueue = (function() {
function MessageQueue() {
EventEmitter.call(this);
this.messages = [];
}
Utils.inherits(MessageQueue, EventEmitter);
MessageQueue.prototype.count = function() {
return this.messages.length;
};
MessageQueue.prototype.push = function(message) {
this.messages.push(message);
};
MessageQueue.prototype.shift = function() {
return this.messages.shift();
};
MessageQueue.prototype.last = function() {
return this.messages[this.messages.length - 1];
};
MessageQueue.prototype.copyAll = function() {
return this.messages.slice();
};
MessageQueue.prototype.append = function(messages) {
this.messages.push.apply(this.messages, messages);
};
MessageQueue.prototype.prepend = function(messages) {
this.messages.unshift.apply(this.messages, messages);
};
MessageQueue.prototype.completeMessages = function(serial, count, err) {
Logger.logAction(Logger.LOG_MICRO, 'MessageQueue.completeMessages()', 'serial = ' + serial + '; count = ' + count);
err = err || null;
var messages = this.messages;
var first = messages[0];
if(first) {
var startSerial = first.message.msgSerial;
var endSerial = serial + count; /* the serial of the first message that is *not* the subject of this call */
if(endSerial > startSerial) {
var completeMessages = messages.splice(0, (endSerial - startSerial));
for(var i = 0; i < completeMessages.length; i++) {
completeMessages[i].callback(err);
}
}
if(messages.length == 0)
this.emit('idle');
}
};
MessageQueue.prototype.completeAllMessages = function(err) {
this.completeMessages(0, Number.MAX_SAFE_INTEGER || Number.MAX_VALUE, err);
};
MessageQueue.prototype.clear = function() {
Logger.logAction(Logger.LOG_MICRO, 'MessageQueue.clear()', 'clearing ' + this.messages.length + ' messages');
this.messages = [];
this.emit('idle');
};
return MessageQueue;
})();
export default MessageQueue;