Observer

Monster.Types. Observer

An observer manages a callback function

You can call the method via the monster namespace new Monster.Types.Observer().

import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/monster.js';
new Monster.Types.Observer()

Alternatively, you can also integrate this function individually.

import {Observer} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/types/observer.js';
new Observer()

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 the this pointer of its own context.

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:
Name Type Attributes Description
callback function
args * <repeatable>
Since:
  • 1.0.0
Example
import {Observer} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/types/observer.js';

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

Methods

# addTag(tag) → {Observer}

Parameters:
Name Type Description
tag string
Returns:
Type
Observer

# getTags() → {Array}

Returns:
Type
Array

# hasTag(tag) → {boolean}

Parameters:
Name Type Description
tag string
Returns:
Type
boolean

# removeTag(tag) → {Observer}

Parameters:
Name Type Description
tag string
Returns:
Type
Observer

# update(subject) → {Promise}

Parameters:
Name Type Description
subject object
Returns:
Type
Promise