ObservableQueue

A FIFO queue that notifies observers whenever its contents change.

Import
the javascript logo
import { ObservableQueue } from "@schukai/monster/source/types/observablequeue.mjs";
Source
the git logo
Package
the npm logo
Since
3.3.0

ObservableQueue combines FIFO queue semantics with observer notifications. It is useful when values should be processed in order while the surrounding system reacts immediately to queue changes.

Common uses are message buffers, job queues and transport layers where new items arrive asynchronously.

Observe Queue Changes While Items Are Added

import { Observer } from "@schukai/monster/source/types/observer.mjs";
import { ObservableQueue } from "@schukai/monster/source/types/observablequeue.mjs";

const output = document.getElementById("queue-output");
const queue = new ObservableQueue();
let notifications = 0;

queue.attachObserver(
    new Observer(function () {
        notifications += 1;
        output.textContent = JSON.stringify(
            {
                size: this.getLength(),
                notifications,
            },
            null,
            2,
        );
    }),
);

document.getElementById("queue-run").addEventListener("click", () => {
    notifications = 0;
    queue.clear();
    queue.add("first");
    queue.add("second");
});
Open in playground

Exported

ObservableQueue

Derived from

Queue

Options

The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the Queue.

Option
Type
Default
Description
-/-

Properties

The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the Queue.

Methods

The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the Queue.

Constructor

constructor()

Behavioral methods

add(value)
Parameters
  • value {*}: value
Returns
  • {Queue}
Add a new element to the end of the queue.
clear()
Returns
  • {Queue}
remove all entries

Static methods

[instanceSymbol]()
Returns
  • {symbol}
This method is called by the instanceof operator.

Other methods

attachObserver(observer)
Parameters
  • observer {observer}: observer
Returns
  • {ProxyObserver}
Attach a new observer
containsObserver(observer)
Parameters
  • observer {observer}: observer
Returns
  • {boolean}
detachObserver(observer)
Parameters
  • observer {observer}: observer
Returns
  • {ProxyObserver}
Detach a observer
notifyObservers()
Returns
  • {Promise}
Notify all observer

Events

This component does not fire any public events. It may fire events that are inherited from its parent classes.

The current width of the area is too small to display the content correctly.