Node event emitter class
This will be a post on the node event emitter class for making custom events in nodejs. It can come in handy now and then to make my own custom events, and attach handers for them, I just need to know where and when to call the emit method in my code when a custom event happens. However maybe it would be best to learn by doing, and to do so it might be best to just jump ahead to the code examples here on the node event emitter class.
This is a nodejs core build in module that is in node itself, so no npm package of any kind needs to be installed to get started with this, you just need node itself.
1 - Basic node event emitter class example
So for a basic example of the node event emitter class, here is an example that just involves stepping an object property. Each time the object property is stepped an update event is emitted, and a overload event is also emitted when that property reaches a defined max value.
So To set up my own events I just need to require in the events module that exports a constructor method that can be used to create an instance of the node event emitter class. Once I have that emitter class instance I can call the emit method of that instance in my code where I want a kind of event to happen, I then pass a name for the event as the first argument, followed by one or more arguments for it.
|
|
So when this example starts it counts up to 10 at which point the overload event is used to set the count back to zero. This is a silly pointless example, but you get the general idea. The node event emitter class is a way to go about defining my own events. I just need to have a way to go about calling the emit method where and when doing so is called for, setting a name, and passing arguments that are needed.
2 - Game board example of a node event emitter
Okay great now that we have the boring part out of the way lets do something fun, or at least something where there is a potential for fun anyway.
In this example I worked out a basic game board module. It contains the beginnings of two classes one for a unit, and another for a game board that will also function as a unit collection class. The Board class will contain an instance of the node event emitter class that I can use to define some events for certain events, such as when a unit moves, or goes out of bounds for the game board.
2.1 - The game board lib
So here is the game board module that I put together for this post. Here I define my Unit Class, and my Board class, and have just a plain object that is exported as public api. The public api of this module is just two references to the Unit and Board Classes.
In the Board class Constructor I created an instance of a node event emitter, and make that one of the properties of a Board class instance. That way when I use the Class in a project I can define event handlers for the Board class instance for certain events of interest that will happen as I move a unit around the board.
|
|
There is way more that could be added when it comes to turning this into a module that would be used in an actual game project. However for the sake of the theme of this post you get the idea. With the node event emitter class I can add all kinds of events for various events that are custom tailer to the nature of the module that I am making.
2.2 - A simple demo
So now for a simple demo of this Board Class in action. In a new javaScript file I require in my board class module. I then create an instance of the Board Class, and then make a single unit for it. I then add some handlers to the node event emitter class instance that is attached to the events property of the Broad Class instance.
|
|
When I run this example I get the expected behavior the unit-move event fires each time for the Unit class instance I created with the uid property of player. The unit-out-of-bounds event fires once when the player unit goes out of bounds, and an error event happens when I attempt to get and move a unit with a value of null.
So things are working as I would expect so far, but the module is still lacking many must needed features. It would also be nice to expand the Unit class as well. I am not sure I would care to put a great deal of time into this though, I thing I would prefer to make a class like this that would work okay on the client side. However you get the idea when it comes to using the node event emitter class it is a way to go about defining some events for a project like this.