Skip to content
Snippets Groups Projects
Verified Commit 7dd11be4 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: new Class

parent 8cfa2f1c
No related branches found
No related tags found
No related merge requests found
/**
* 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)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment