A simple built-in observer toolkit for small JS projects.
npm i obsrvble
subscribe(observer)
Subcribe an observer to the observable object
unsubscribe(observer)
Unsubscribe an oberver from the observable object
unsubscribeAll()
Unsubscribe all the observers from the observable object
dispatch(event, ...d)
Dispatch an event and resolve the concerned observers
Creates a new observer
constructor(event, resolver)
The event associated with the observer
get event()
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!");
Because. I did not found any simple KISS observer / observable toolkit on NPM. This is my 🎁 to the community, cheers!