In: Computer Science
Problem Description
You have been asked to develop basic Event Broker functionality for a piece of home automation software.
Your system needs to:
Events & Handlers:
Your Task
Your task is to plan and implement a JavaScript module for the system described above.
Your code will be run in a NodeJS environment as a module similar to this:
const bkr = require('./broker'); let handler1 = function() { /* do stuff... */ } let handler2 = function() { /* do stuff... */ } bkr.subscribe('event1',handler1); bkr.subscribe('event1',handler2); bkr.subscribe('event2',handler2); bkr.emit('event1'); bkr.emit('event2'); bkr.unsubscribe('event1',handler1);
Your module needs to support 3 functions:
First, plan your system
Finally, implement your code.
Key Requirements
Be sure to adhere to the following requirements.
Not doing so may result in a reduction or complete loss of
marks.
The custom library is created with which you can subscribe to events and emit the events. The library also supports unsubscribing from the event. The whole program is done in pure java script. No external libraries are used.
Broker.js
function Broker() {
this.event = {};
}
// This method will register a handler for an event.
Broker.prototype.subscribe = function (eventName, handler) {
// We will check if the event already exist in the event object, if so add to the array of handlers
if(this.event[eventName])
{
const handlerArray=this.event[eventName];
handlerArray.push(handler);
this.event[eventName]=handlerArray;
}
else
{
// Create a new handler array and push the new handler to register it
const handlerArray = [];
handlerArray.push(handler);
this.event[eventName]=handlerArray;
}
}
// This method will deregister a handler for an event.
Broker.prototype.unsubscribe = function (eventName, handler) {
if(this.event[eventName])
{
// We remove the handler from the event handler array
const handlerArray=this.event[eventName];
let handlerIndex = handlerArray.indexOf(handler);
if(handlerIndex > -1)
handlerArray.splice(handlerIndex, 1);
this.event[eventName]=handlerArray;
}
}
// This method will emit the event
Broker.prototype.emit = function (eventName) {
handlerArray=this.event[eventName];
// We iterate through each handlers in the array and invoke each of them
for (let i = 0 ; i < handlerArray.length; i++)
{
handlerArray[i]();
}
}
module.exports = new Broker();
BrokerDriver.js
var bkr = require('./broker');
var handler1 = function() {
console.log("handler1");
}
var handler2 = function() {
console.log("handler2");
}
console.log("Suscribing event1 with handler 1");
bkr.subscribe('event1',handler1);
console.log("Suscribing event1 with handler 2");
bkr.subscribe('event1',handler2);
console.log("Suscribing event2 with handler 2");
bkr.subscribe('event2',handler2);
console.log("Emitting event 1");
bkr.emit('event1');
console.log("Emitting event 2");
bkr.emit('event2');
console.log("Unsuscribing event1 with handler 1");
bkr.unsubscribe('event1',handler1);
console.log("Emitting event 1");
bkr.emit('event1');
Output