Observer
An asynchronous observer that batches updates per subject and callback.
import { Observer } from "@schukai/monster/source/types/observer.mjs";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)]);
});Exported
ObserverDerived from
BaseOptions
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.
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)callback{function}: The function to execute on update.args{...*}: Additional arguments to pass to the callback.
Behavioral methods
addTag(tag)tag{string}: tag
- {Observer}
removeTag(tag)tag{string}: tag
- {Observer}
update(subject)subject{object}: The subject object that triggered the update.
- {promise<*>}: promise that resolves with the return value of the callback,
State query methods
hasTag(tag)tag{string}: tag
- {boolean}
Structural methods
getTags()- {string[]}
Static methods
[instanceSymbol]()2.1.0- {symbol}
instanceof operator.Other methods
arguments()callback()Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.