1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-10 11:43:01 +03:00
Files
node-redis/lib/queue.js
Jonas Dohse 3b6ab9e820 Allow garbage collection of processed queue items
Set processed queue items to `null` to allow garbage collection of these
items. The command queue contains callbacks. They contain the stack of
the current fiber, which can be large. Allow earlier garbage collection
of these stacks by removing the reference to the queue items.
2015-05-17 19:06:30 +00:00

63 lines
1.4 KiB
JavaScript

// Queue class adapted from Tim Caswell's pattern library
// http://github.com/creationix/pattern/blob/master/lib/pattern/queue.js
function Queue() {
this.tail = [];
this.head = [];
this.offset = 0;
}
Queue.prototype.shift = function () {
if (this.offset === this.head.length) {
var tmp = this.head;
tmp.length = 0;
this.head = this.tail;
this.tail = tmp;
this.offset = 0;
if (this.head.length === 0) {
return;
}
}
var item = this.head[this.offset];
this.head[this.offset] = null;
this.offset++;
return item;
};
Queue.prototype.push = function (item) {
return this.tail.push(item);
};
Queue.prototype.forEach = function (fn, thisv) {
var array = this.head.slice(this.offset), i, il;
array.push.apply(array, this.tail);
if (thisv) {
for (i = 0, il = array.length; i < il; i += 1) {
fn.call(thisv, array[i], i, array);
}
} else {
for (i = 0, il = array.length; i < il; i += 1) {
fn(array[i], i, array);
}
}
return array;
};
Queue.prototype.getLength = function () {
return this.head.length - this.offset + this.tail.length;
};
Object.defineProperty(Queue.prototype, "length", {
get: function () {
return this.getLength();
}
});
if (typeof module !== "undefined" && module.exports) {
module.exports = Queue;
}