Observer

An asynchronous observer that batches updates per subject and callback.

Import
the javascript logo
import { Observer } from "@schukai/monster/source/types/observer.mjs";
Source
the git logo
Package
the npm logo
Since
1.0.0

Observer is the asynchronous callback primitive behind Monster's observable structures. It batches updates per subject inside one microtask cycle so duplicate triggers collapse into one execution.

Use it when update notifications should stay cheap and consistent even if a subject changes several times in quick succession.

Schedule A Deduplicated Observer Update

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

const output = document.getElementById("observer-output");
let runs = 0;

const observer = new Observer(function () {
    runs += 1;
    output.textContent = `runs: ${runs}`;
});

document.getElementById("observer-run").addEventListener("click", async () => {
    runs = 0;
    output.textContent = "scheduled";
    const subject = { name: "demo" };
    await Promise.all([observer.update(subject), observer.update(subject)]);
});
Open in playground

Exported

Observer

Derived from

Base

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 Base.

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 Base.

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 Base.

Constructor

constructor(callback,args)
Parameters
  • callback {function}: The function to execute on update.
  • args {...*}: Additional arguments to pass to the callback.
No description available.

Behavioral methods

addTag(tag)
Parameters
  • tag {string}: tag
Returns
  • {Observer}
No description available.
removeTag(tag)
Parameters
  • tag {string}: tag
Returns
  • {Observer}
No description available.
update(subject)
Parameters
  • subject {object}: The subject object that triggered the update.
Returns
  • {promise<*>}: promise that resolves with the return value of the callback,
Schedules the callback for execution with the given subject. If multiple updates for the same subject are requested in the same event loop tick, the callback is only executed once. All callers will receive the same promise. or rejects if the callback throws an error.

State query methods

hasTag(tag)
Parameters
  • tag {string}: tag
Returns
  • {boolean}
No description available.

Structural methods

getTags()
Returns
  • {string[]}
No description available.

Static methods

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

Other methods

arguments()
No description available.
callback()
No description available.

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.