Class for emitting events
npm
npm install events-constructor
yarn
yarn add events-constructor
import Events from 'events-constructor';
const eventNames = ['event1', 'event2'];
const events = new Events(initEventNames);
events.on('event1', data => {
console.log('event1 is called with data:', data);
});
events.trigger('event1', 'some data');
Add listener to event
events.on('event1', data => {
console.log('event1 is called with data:', data);
});
Add a listener that will be called only once per event
events.once('event1', data => {
console.log('event1 is called with data:', data);
});
Remove listener from event
events.off('event1', data => {
console.log('event1 is called with data:', data);
});
Trigger event with data
events.trigger('event1', 'some data');
Remove all listeners
events.removeEventHandlers();
Get all triggers
events.triggers;
Disable event handlers. The trigger method will not produce any effect.
events.on('event1', data => {
console.log('event1 is called with data:', data); //it's not called
});
events.deactivate();
events.trigger('event1', 'some data');
Enable event handlers.
events.on('event1', data => {
console.log('event1 is called with data:', data);
});
events.deactivate();
events.trigger('event1', 'some data'); // no effect
events.activate();
events.trigger('event1', 'some data'); // handler is called
Method for delegate triggers to otherEventsSource(for example)
events.eachTriggers((trigger, eventName) => otherEventsSource.on(eventName, trigger));
npm test
👤 Krivega Dmitriy
- Website: https://krivega.com
- Github: @Krivega
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Copyright © 2020 Krivega Dmitriy.
This project is MIT licensed.