Browsing through http://microjs.com, I see lots of libraries labelled "event emitters". I like to think I know my way around the basics of the Javascript language pretty well, but I really have no idea what an "event emitter" is or does.
Anyone care to enlighten me? It sounds interesting...
EventEmitter Class When a new listener is added, 'newListener' event is fired and when a listener is removed, 'removeListener' event is fired. EventEmitter provides multiple properties like on and emit. on property is used to bind a function with the event and emit is used to fire an event.
The EventEmitter class can be used to create and handle custom events module. Default Value: false It automatically captures rejections. Listening events: Before emits any event, it must register functions(callbacks) to listen to the events.
Node. js allows us to create and handle custom events easily by using events module. Event module includes EventEmitter class which can be used to raise and handle custom events.
🎊 Event Emitters in Angular 🎊Data flows into your component via property bindings and flows out of your component through event bindings. If you want your component to notify his parent about something you can use the Output decorator with EventEmitter to create a custom event.
It triggers an event to which anyone can listen. Different libraries offer different implementations and for different purposes, but the basic idea is to provide a framework for issuing events and subscribing to them.
Example from jQuery:
// Subscribe to event. $('#foo').bind('click', function() { alert("Click!"); }); // Emit event. $('#foo').trigger('click');
However, with jQuery in order to emit an event you need to have a DOM object, and cannot emit events from an arbitrary object. This is where event-emitter becomes useful. Here's some pseudo-code to demo custom events (the exact same pattern as above):
// Create custom object which "inherits" from emitter. Keyword "extend" is just a pseudo-code. var myCustomObject = {}; extend(myCustomObject , EventEmitter); // Subscribe to event. myCustomObject.on("somethingHappened", function() { alert("something happened!"); }); // Emit event. myCustomObject.emit("somethingHappened");
In node.js an event can be described simply as a string with a corresponding callback. An event can be "emitted" (or in other words, the corresponding callback be called) multiple times or you can choose to only listen for the first time it is emitted.
Example:-
var example_emitter = new (require('events').EventEmitter); example_emitter.on("test", function () { console.log("test"); }); example_emitter.on("print", function (message) { console.log(message); }); example_emitter.emit("test"); example_emitter.emit("print", "message"); example_emitter.emit("unhandled"); > var example_emitter = new (require('events').EventEmitter); {} > example_emitter.on("test", function () { console.log("test"); }); { _events: { test: [Function] } } > example_emitter.on("print", function (message) { console.log(message); }); { _events: { test: [Function], print: [Function] } } > example_emitter.emit("test"); test //console.log'd true //return value > example_emitter.emit("print", "message"); message //console.log'd true //return value > example_emitter.emit("unhandled"); false //return value
This demonstates all the basic functionality of an EventEmitter
. The on or addListener
method (basically the subscription method) allows you to choose the event to watch for and the callback to be called. The emit
method (the publish method), on the other hand, allows you to "emit" an event, which causes all callbacks registered to the event to 'fire', (get called).
From the source What are Event Emitters?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With