@schukai/monster
Version:
Monster is a simple library for creating fast, robust and lightweight websites.
111 lines (100 loc) • 2.45 kB
JavaScript
/**
* Copyright © Volker Schukai 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 Volker Schukai.
*
* SPDX-License-Identifier: AGPL-3.0
*/
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.
*
* @since 3.3.0
* @copyright Volker Schukai
* @summary An observable Queue (Fifo)
*/
class ObservableQueue extends Queue {
/**
*
*/
constructor() {
super();
this[internalSymbol] = {
observers: new ObserverList(),
};
}
/**
* This method is called by the `instanceof` operator.
* @return {symbol}
*/
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/types/observablequeue");
}
/**
* Add a new element to the end of the queue.
*
* @param {*} value
* @return {Queue}
*/
add(value) {
super.add(value);
this.notifyObservers();
return this;
}
/**
* remove all entries
*
* @return {Queue}
*/
clear() {
super.clear();
this.notifyObservers();
return this;
}
/**
* Attach a new observer
*
* @param {Observer} observer
* @return {ProxyObserver}
*/
attachObserver(observer) {
this[internalSymbol].observers.attach(observer);
return this;
}
/**
* Detach a observer
*
* @param {Observer} observer
* @return {ProxyObserver}
*/
detachObserver(observer) {
this[internalSymbol].observers.detach(observer);
return this;
}
/**
* Notify all observer
*
* @return {Promise}
*/
notifyObservers() {
return this[internalSymbol].observers.notify(this);
}
/**
* @param {Observer} observer
* @return {boolean}
*/
containsObserver(observer) {
return this[internalSymbol].observers.contains(observer);
}
}