Platform Independent Event driven Javascript Code

I want to examine a common pattern in software development, variably called ‘The Observer Pattern‘ or simply event driven design. It helps with the decoupling of  code components which can now communicate with each other without being directly injected or passed as references. For the latter cases, we would speak of aggregation and composition in OOP terms, which are just fancy terms for passing object references around. Javascript however is class-less  and these patterns become relative and even difficult to handle safely so events become even the more interesting.

We want to show how this can be done, while at the same time showing some good practices for approaching object orientation in Node.js.

I will use the events module in combination with inherits to provide for an easy pattern to introduce events in your code. The second can be found in the ‘util’ package of the standard library but here we will use an external package. The reason is that ‘util’ is bloated and will add unnecessary KB in your front end code. So the package itself is essentially one big conditional statement handling the case that the code is run on Node.js or the browser, ensuring portability. On Node.js it uses the prebuilt inherits function of the utils module:

In either case, a function is given a prototype object from which it “inherits” all its properties and functions, essentially augmenting its functionality. The concept here is therefore, to augment any object we have by making its constructor function inherit from the events module.

A basic structure for a module (in this case called FileSearcher) that could serve as the equivalent of a class file in other programming languages can look like this:

All we are exporting is a function, which is forced to server as a constructor by either being called with the new keyword or simply by itself. The first few lines of code will handle either case. For a further explanation of this technique we can look into a previous tutorial.

Our class will search through a file for the appearance of a word and use events to denote its success or its failure.

The complete unoptimized (for readability) code is:

The code will hold all the text data of a given file in memory and will search using a simple algorithm of comparing every character until the end of the data, emitting events each time a match has been found. The event can hold arguments which in our case is the search word itself and the index at which it was found.

This can be used as follows:

Some notes on the ‘events’ module

It is always a good idea to look into the source code of the libraries we are using, so let’s open events.js from the repository above.

On the emit function we can see:

As the comments suggest, the library is not agnostic to the messages being emitted. The string ‘error’ seems to have a special meaning and if no listener is defined it will be thrown as a real error!

Further down this function we find:

When calling emit, we optionally pass a callback function, here named handler. We see that if the objects emitted (passed as arguments to the handler function) are few in number, the code has an optimised handling, but if we have more than 3 then a slower root will be taken. The differences should be minor, but if you think about the fact that emitters are usually linked to streams and optimised function callbacks, it might be something worth considering.

Further down we see:

which means that the two methods are identical.

For more tutorials on Javascript

[catlist name=”Javascript”]

Leave a Reply

Your email address will not be published. Required fields are marked *