Skip to content

Just a simple observer / observable toolkit

Notifications You must be signed in to change notification settings

TeddyGandon/obsrvble

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo

OBSRVBLE

A simple built-in observer toolkit for small JS projects.

Usage

npm i obsrvble

API doc

Observable

subscribe

subscribe(observer) Subcribe an observer to the observable object

unsubscribe

unsubscribe(observer) Unsubscribe an oberver from the observable object

unsubscribeAll

unsubscribeAll() Unsubscribe all the observers from the observable object

dispatch

dispatch(event, ...d) Dispatch an event and resolve the concerned observers

Observer

constructor

Creates a new observer constructor(event, resolver)

get event

The event associated with the observer get event()

Example

Create an observable object:

import {Observable} from "obsrvble";

export class Foo extends Observable {
    callThisToDispatch(bar) {
        this.dispatch("myEvent", bar);
    }
}

Subscribe an observer:

import {Observer} from "obsrvble";

const foo = new Foo();
foo.subscribe(new Observer(
    'myEvent',
    params => {
        // Do something here
        console.log(params[0]); // hello!
    }
));
foo.callThisToDispatch("hello!");

Why?

Because. I did not found any simple KISS observer / observable toolkit on NPM. This is my 🎁 to the community, cheers!