From 7dd11be4445c180f9134f6092a28ad1be627af86 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Sat, 7 Jan 2023 18:57:34 +0100 Subject: [PATCH] feat: new Class --- application/source/types/observablequeue.mjs | 110 +++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 application/source/types/observablequeue.mjs diff --git a/application/source/types/observablequeue.mjs b/application/source/types/observablequeue.mjs new file mode 100644 index 000000000..f395561bb --- /dev/null +++ b/application/source/types/observablequeue.mjs @@ -0,0 +1,110 @@ +/** + * Copyright schukai GmbH and contributors 2022. All Rights Reserved. + * Node module: @schukai/monster + * This file is licensed under the AGPLv3 License. + * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html + */ + +import {Queue} from './queue.mjs'; +import {instanceSymbol, internalSymbol} from '../constants.mjs'; +import {ObserverList} from "./observerlist.mjs"; + +export {ObservableQueue}; + +/** + * An observable queue is a list of items that are processed one after another (first in, first out). + * + * `Queue.add()` and `Queue.clear()` notify all observers. + * + * @externalExample ../../example/types/queue.mjs + * @license AGPLv3 + * @since 3.3.0 + * @copyright schukai GmbH + * @memberOf Monster.Types + * @summary An observable Queue (Fifo) + */ +class ObservableQueue extends Queue { + + /** + * + */ + constructor() { + super(); + this[internalSymbol]= { + observers: new ObserverList() + }; + } + + /** + * This method is called by the `instanceof` operator. + * @returns {symbol} + * @since 2.1.0 + */ + static get [instanceSymbol]() { + return Symbol.for("@schukai/monster/types/observablequeue"); + } + + /** + * Add a new element to the end of the queue. + * + * @param {*} value + * @returns {Queue} + */ + add(value) { + super.add(value); + this.notifyObservers(); + return this; + } + + /** + * remove all entries + * + * @returns {Queue} + */ + clear() { + super.clear(); + this.notifyObservers(); + return this; + } + + /** + * Attach a new observer + * + * @param {Observer} observer + * @returns {ProxyObserver} + */ + attachObserver(observer) { + this[internalSymbol].observers.attach(observer) + return this; + } + + /** + * Detach a observer + * + * @param {Observer} observer + * @returns {ProxyObserver} + */ + detachObserver(observer) { + this[internalSymbol].observers.detach(observer) + return this; + } + + /** + * Notify all observer + * + * @returns {Promise} + */ + notifyObservers() { + return this[internalSymbol].observers.notify(this); + } + + /** + * @param {Observer} observer + * @returns {boolean} + */ + containsObserver(observer) { + return this[internalSymbol].observers.contains(observer) + } + + +} -- GitLab