@markdomkan/js-events
v1.0.5
Published
A simple event gestor for raw js
Downloads
2
Readme
JsEvents
Usage
const JsEvents = require('@markdomkan/js-events');
or ES6 & TS
import JsEvents from '@markdomkan/js-events';
You can extend a class with JsEvents
class MyClass extends JsEvents{
constructor(){
this.on('myCustomEvent', name => console.log(name));
}
triggerMyCustomEvent(){
this.emit('myCustomEvent','Hello World');
}
}
or use as Object. Also can pass an object to class constructor to inject the methods on the object.
customObject = new JsEvents(customObject);
customObject.on('myCustomEvent', name => console.log(name));
customObject.emit('myCustomEvent','Hello World');
Avaiable Methods
Subscribe new event
on(eventName, callbackFunction): this;
Emit event with optional object data
emit(eventName: string, data?: any): this;
Unsubscribe event
delete(eventName: string): this;
Get all subcrited event names
emitters(): string[];
Get all callbacks by event name
listeners(eventName: string): (data: any) => void;
Check if exist passed event
hasListeners(eventName: string): boolean;