Custom Quick & Easy Java Script Event Emitter no need of any third party library. From this you register multiple events with call back function & can be able to emit event multiple times with data. And pragmatically mute & unmute event which you created.
Download am.emmiter.js file from this repo and import am.emmiter.js file in you project
<script src="am.emmiter.js"></script>
Or do
npm install am-event-emmiter
Refer index.html in this repo
var eventEmitter = new amEventEmitter();
eventEmitter.register('event1', function(){
console.log('Event1 emitted');
});
eventEmitter.emit('event1');
consoles
Event1 emitted
eventEmitter.register('event2', function(data){
console.log('event2 emitted with data',data);
});
eventEmitter.emit('event2',{'name' : 'Jhon Potter'});
eventEmitter.register('event3', function(data){
console.log('event3 emitted!! first register',data);
});
eventEmitter.register('event3', function(data){
console.log('event3 emitted!! second register',data);
});
eventEmitter.emit('event3',{'name' : 'Jhon Potter'});
eventEmitter.register('event4', function(data){
console.log('event4 emitted!! ',data);
});
eventEmitter.emit('event4',{'name' : 'Jhon Potter'});
eventEmitter.emit('event4',{'name' : 'Harry Potter'});
Mutting & UnMutting event. call mute method to disable event emit & call unmute method to enable back
eventEmitter.register('event5', function(data){
console.log('event5 emitted!! ',data);
});
eventEmitter.emit('event5',{'name' : 'Jhon Potter'});
eventEmitter.mute('event5');
eventEmitter.emit('event5',{'name' : 'Harry Potter'});
eventEmitter.unmute('event5');
eventEmitter.emit('event5',{'name' : 'Mark welson'});
Wrap register
, emit
, mute
and unmute
method with try & catch block.
try{
eventEmitter.register('event4', function(data){
console.log('event4 emitted!! ',data);
});
} catch(e){
console.log('Error',e);
}