Monster.Types. Observer

An observer manages a callback function

The update method is called with the subject object as this pointer. For this reason the callback should not be an arrow function, because it gets this pointer of its own context.

Include this class in your project with the following code:

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

The callback function is passed as the first argument to the constructor.

new Observer(()=>{
    // this is not subject
})

new Observer(function() {
    // this is subject
})

Additional arguments can be passed to the callback. To do this, simply specify them.

Observer(function(a, b, c) {
    console.log(a, b, c); // ↦ "a", 2, true
}, "a", 2, true)

The callback function must have as many parameters as arguments are given.

Constructor

new Observer(callback, …args)

Parameters:
NameTypeAttributesDescription
callbackfunction
args*<repeatable>
Since
  • 1.0.0
License
  • AGPLv3
Example
import {Observer} from '@schukai/monster/source/types/observer.mjs';

const observer = new Observer(function (a, b, c) {
    console.log(this, a, b, c); // ↦ "a", 2, true 
}, "a", 2, true);

observer.update({value: true}).then(() => {
});
// ↦ {value: true} "a" 2 true

Members

(static) instanceSymbol

This method is called by the instanceof operator.

This method is called by the instanceof operator.

Since
  • 2.1.0

Methods

addTag(tag) → {Observer}

Parameters:
NameTypeDescription
tagstring
Returns:
Type: 
Observer

getTags() → {Array}

Returns:
Type: 
Array

hasTag(tag) → {boolean}

Parameters:
NameTypeDescription
tagstring
Returns:
Type: 
boolean

removeTag(tag) → {Observer}

Parameters:
NameTypeDescription
tagstring
Returns:
Type: 
Observer

update(subject) → {Promise}

Parameters:
NameTypeDescription
subjectobject
Returns:
Type: 
Promise