types/queue.js

import {Monster, Base} from './base.js';

/**
 * you can call the method via the monster namespace `new Monster.Types.Queue()`.
 *
 * ```
 * <script type="module">
 * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/queue.js';
 * console.log(new Monster.Types.Queue())
 * </script>
 * ```
 *
 * Alternatively, you can also integrate this function individually.
 *
 * ```
 * <script type="module">
 * import {ID} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/types/queue.js';
 * console.log(new Queue())
 * </script>
 * ```
 *
 * @since 1.4.0
 * @copyright schukai GmbH
 * @memberOf Monster/Types
 */
class Queue extends Base { 

    /**
     *
     */
    constructor() {
        super();
        this.data = [];
    }


    /**
     * @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
     * @returns {Queue}
     */
    add(value) {
        this.data.push(value)
        return this;
    }

    /**
     * remove all entries
     *
     * @returns {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();
    }


}

Monster.assignToNamespace('Monster.Types', Queue);
export {Monster, Queue}