MessageQueue.js
1.79 KB
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
// Generated by CoffeeScript 1.8.0
var MessageQueue, utils, _;
_ = require('underscore');
utils = require('./utils');
module.exports = utils.registerClass(MessageQueue = (function() {
function MessageQueue() {
this.messages = [];
this.closed = false;
this.callback = null;
this.timer = null;
_.bindAll(this, '_timerExpired', '_updated');
}
MessageQueue.prototype.shutdown = function() {
if (this.closed) {
return;
}
this.closed = true;
if (this.timer) {
clearTimeout(this.timer);
}
if (this.callback) {
this.callback.call(null, this.messages);
}
this.callback = null;
this.messages = null;
return this.timer = null;
};
MessageQueue.prototype.push = function(message) {
if (this.closed) {
return;
}
this.messages.push(message);
return process.nextTick(this._updated);
};
MessageQueue.prototype.pullAll = function(timeout, callback) {
if (this.closed) {
return callback.call(null, null);
}
if (this.callback) {
return callback.call(null, []);
}
if (this.messages.length) {
callback.call(null, this.messages);
this.messages = [];
return;
}
this.callback = callback;
return this.timer = setTimeout(this._timerExpired, timeout);
};
MessageQueue.prototype._timerExpired = function() {
return this._updated();
};
MessageQueue.prototype._updated = function() {
var callback, messages;
if (this.closed) {
return;
}
if (!this.callback) {
return;
}
callback = this.callback;
messages = this.messages;
if (this.timer) {
clearTimeout(this.timer);
}
this.callback = null;
this.messages = [];
this.timer = null;
return callback.call(null, messages);
};
return MessageQueue;
})());