@schukai/monster
Version:
Monster is a simple library for creating fast, robust and lightweight websites.
105 lines (94 loc) • 2.25 kB
JavaScript
/**
* Copyright © schukai GmbH and all contributing authors, {{copyRightYear}}. All rights reserved.
* Node module: @schukai/monster
*
* This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
* The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
*
* For those who do not wish to adhere to the AGPLv3, a commercial license is available.
* Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
* For more information about purchasing a commercial license, please contact schukai GmbH.
*
* SPDX-License-Identifier: AGPL-3.0
*/
import { Base } from "./base.mjs";
import { instanceSymbol } from "../constants.mjs";
export { Queue };
/**
* A queue is a list of items that are processed one after another (first in, first out).
*
* With a queue you can add items to the end of the list `Queue.add()` and remove items from the beginning of the list `Queue.pop()`.
*
* With `Queue.peek()` you can get the first item without removing it from the list.
*
* You can create the instance via `new Queue()`.
*
* @license AGPLv3
* @since 1.4.0
* @summary A Queue (Fifo)
*/
class Queue extends Base {
/**
*
*/
constructor() {
super();
this.data = [];
}
/**
* This method is called by the `instanceof` operator.
* @return {symbol}
* @since 2.1.0
*/
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/types/queue");
}
/**
* @return {boolean}
*/
isEmpty() {
return this.data.length === 0;
}
/**
* Read the element at the front of the queue without removing it.
*
* @return {*}
*/
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.data[0];
}
/**
* Add a new element to the end of the queue.
*
* @param {*} value
* @return {Queue}
*/
add(value) {
this.data.push(value);
return this;
}
/**
* remove all entries
*
* @return {Queue}
*/
clear() {
this.data = [];
return this;
}
/**
* Remove the element at the front of the queue
* If the queue is empty, return undefined.
*
* @return {*}
*/
poll() {
if (this.isEmpty()) {
return undefined;
}
return this.data.shift();
}
}