Nowadays there are a lot of open source libraries that you can use within your projects. They're pretty nice worked and allow you to do a lot of things to know when things happen via events. They allow you normally add a callback to certainly events triggered internally. Have you ever wondered, what's the right and easiest way to built such a library that allows to use callbacks ? Well, whether if this is the right way to do it is not the absolute truth, but a lot of libraries rely on the Event Emitter class of Node.js.
All objects that emit events are instances of the EventEmitter class, these objects expose an eventEmitter.on()
function that allows one or more functions to be attached to named events emitted by the object. Typically, event names are camel-cased strings but any valid JavaScript property key can be used.
In this article you will learn how to use them for both ECMAScript 5 and 6.
ES5
If you're using ECMAScript 5, the usage of event emitters couldn't be so clear for beginners in Javascript:
When EventEmitter.call(this)
is executed during the creation of an instance from YourLibrary
, it appends properties declared from the EventEmitter constructor to YourLibrary
. Then the inherits function inherits the prototype methods from one constructor into another (your constructor YourLibrary and the super constructor EventEmitter), in this way the prototype of your constructor will be set to a new object created from superConstructor.
As your library obviously won't offer the same methods of EventEmitter you need to add your own functions to YourLibrary by using prototyping before or after the module.exports
line:
The previous function adds the testAsyncMethod
to your library, this method expects some data as first argument and it will sent again through the data event, emitted with the inherited method emit from the EventEmitter class. In this way YourLibrary
uses the event manager of Node.js and it can be used to create organized and easy to read code:
Although to handle asynchronous events can be a little bit tricky, everything will make sense at the end, so if you don't understand it yet, be patient and analyse the example carefully.
ES6
With EcmaScript 6, this task has been really simplified and its pretty easier to handle and understand than with ES5. However it works in the same way: by using the extends keyword, your class can extend the EventEmitter inheriting obviously its method and therefore be able to use the emit method to trigger an event:
Then YourLibrary
can be easily used from another file:
As you can see, it's pretty easier than handling the same code with ES6.
Example using ECMAScript 5
If you didn't get it with the introduction, don't worry, we're sure that handling the example as a library in the real life (or something similar) will help you to understand. In this example, imagine that we are going to use a Download Manager library, this library is really simple, it offers a way to download a file from your server asynchronously. Using the Event Emitters class of Node.js, you are able to know when the download of the file finishes and allows you to know the progress of the download.
The example doesn't download anything really, we are just simulating a download using the setInterval
(for progress
event) and the setTimeout
(for downloadSuccess
event) function. Create the fileDownloader.js
in some folder of your workspace and add the following code on it:
Then the Download Manager can be used as follows (in this example on the index.js
file):
And executing the script using node index.js
you'll get the following output:
Example using ECMAScript 6
For the example in ES6, we'll use the same idea of the first example in ES5, imagine that we are going to use a Download Manager library, this library is really simple, it offers a way to download a file from your server asynchronously. Using the Event Emitters class of Node.js, you are able to know when the download of the file finishes and allows you to know the progress of the download.
The example doesn't download anything really, we are just simulating a download using the setInterval
(for progress
event) and the setTimeout
(for downloadSuccess
event) function. Create the fileDownloader.js
in some folder of your workspace and add the following code on it:
The download manager can be used in other files (index.js
):
Executing the script using node index.js
you'll get the following output (same as the example in ES5):
As you are using the EventEmitter class of Node.js you are able to use all the methods of this class in your library, so don't forget to check the docs of Node.js to know more about the Event Emitters.
Happy coding !
Comments
Post a Comment