/
PacketQueue.js
52 lines (34 loc) · 956 Bytes
/
PacketQueue.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
/**
* Copyright (c) 2013 Yahoo! Inc. All rights reserved.
*
* Copyrights licensed under the MIT License. See the accompanying LICENSE file
* for terms.
*/
/**
* The package queue which emits events.
*/
var util = require('util');
var events = require('events');
function PacketQueue() {
events.EventEmitter.call(this);
this.queue = [];
}
util.inherits(PacketQueue, events.EventEmitter);
PacketQueue.prototype.push = function (packet) {
if (typeof packet !== 'object') {
throw new Error('packet must be a valid object.');
}
this.queue.push(packet);
this.emit('readable');
};
PacketQueue.prototype.unshift = function (packet) {
if (typeof packet !== 'object') {
throw new Error('packet must be a valid object.');
}
this.queue.unshift(packet);
this.emit('readable');
};
PacketQueue.prototype.shift = function () {
return this.queue.shift();
};
module.exports = PacketQueue;