/
queue.js
132 lines (115 loc) · 2.61 KB
/
queue.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
/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */
"use strict";
var util = require('util');
var Emitter = require('events').EventEmitter;
exports.create = function (options) {
var _options = {
'timeout' : 0,
'maxitem' : -1,
};
for (var i in options) {
_options[i] = options[i];
}
/**
* @序列
*/
var _sequence = [];
/* {{{ private function build() */
/**
* To build a timeout item into _sequence
*/
var build = function (item, tmout, _self) {
var tmout = ~~(tmout || _options.timeout);
var _item = [item, 0];
if (tmout) {
_item[1] = setTimeout(function () {
var i = _sequence.indexOf(_item);
if (i > -1) {
_sequence.splice(i, 1);
_self.emit('timeout', item, tmout, i);
}
}, tmout);
}
return _item;
};
/* }}} */
/* {{{ private function parse() */
/**
* To parse and return a build-in item
*
* @ access private
* @ param {Array} item
* @ return {Object} or undefined
*/
var parse = function (item) {
if (item && Array.isArray(item)) {
clearTimeout(item[1]);
item = item[0];
}
return item;
};
/* }}} */
var Queue = function () {
Emitter.call(this);
};
util.inherits(Queue, Emitter);
/* {{{ public prototype push() */
/**
* Push an item into sequence
*
* @ access public
* @ param {Object} item
* @ param {Integer} tmout
* @ return {Number} length of the sequence
*/
Queue.prototype.push = function (item, tmout) {
if (_options.maxitem > 0 && _sequence.length >= _options.maxitem) {
this.emit('full', _sequence.length, _options.maxitem);
return -1;
}
var n = _sequence.push(build(item, tmout, this));
if (1 === n) {
this.emit('fill');
}
return n;
};
/* }}} */
/* {{{ public prototype shift() */
/**
* shift an item from the sequence
*
* @ access public
* @ return {Object} item or undefined
*/
Queue.prototype.shift = function () {
return parse(_sequence.shift());
};
/* }}} */
/* {{{ public prototype pop() */
/**
* Pop up an item from the sequence
*/
Queue.prototype.pop = function () {
return parse(_sequence.pop());
};
/* }}} */
/* {{{ public prototype size() */
Queue.prototype.size = function () {
return _sequence.length;
};
/* }}} */
/* {{{ public prototype clean() */
/**
* Clean the sequence up
*/
Queue.prototype.clean = function () {
_sequence.forEach(function (item) {
if (item[1]) {
clearTimeout(item[1]);
}
});
_sequence = [];
};
/* }}} */
return new Queue();
};