UNPKG

1.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Queue = void 0;
4var common_1 = require("./common");
5var Queue = /** @class */ (function () {
6 function Queue(_items, _limit) {
7 if (_items === void 0) { _items = []; }
8 if (_limit === void 0) { _limit = null; }
9 this._items = _items;
10 this._limit = _limit;
11 this._evictListeners = [];
12 this.onEvict = common_1.pushTo(this._evictListeners);
13 }
14 Queue.prototype.enqueue = function (item) {
15 var items = this._items;
16 items.push(item);
17 if (this._limit && items.length > this._limit)
18 this.evict();
19 return item;
20 };
21 Queue.prototype.evict = function () {
22 var item = this._items.shift();
23 this._evictListeners.forEach(function (fn) { return fn(item); });
24 return item;
25 };
26 Queue.prototype.dequeue = function () {
27 if (this.size())
28 return this._items.splice(0, 1)[0];
29 };
30 Queue.prototype.clear = function () {
31 var current = this._items;
32 this._items = [];
33 return current;
34 };
35 Queue.prototype.size = function () {
36 return this._items.length;
37 };
38 Queue.prototype.remove = function (item) {
39 var idx = this._items.indexOf(item);
40 return idx > -1 && this._items.splice(idx, 1)[0];
41 };
42 Queue.prototype.peekTail = function () {
43 return this._items[this._items.length - 1];
44 };
45 Queue.prototype.peekHead = function () {
46 if (this.size())
47 return this._items[0];
48 };
49 return Queue;
50}());
51exports.Queue = Queue;
52//# sourceMappingURL=queue.js.map
\No newline at end of file