Created
January 17, 2020 00:54
-
-
Save feargswalsh92/e73240f9cb37af97ea83417460936a1e to your computer and use it in GitHub Desktop.
Implementation of node.js Event Emitter class for Pramp Interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventEmitter { | |
constructor(event) { | |
this._events = {}; | |
} | |
on = (event, listener) => { | |
if (typeof listener === "function") { | |
this._events[event] = []; | |
this._events[event].push(listener); | |
} else { | |
throw new Error( | |
" The listener argument must be of type Function. Received type undefined" | |
); | |
} | |
return this.eventEmitter; | |
}; | |
// Adds a one time listener to the event. This listener is invoked only the next time the event is fired, after which it is removed. | |
once = (event, listener) => { | |
this._events[event].push({ listener: listener }); | |
// Returns emitter, so calls can be chained. | |
return this.eventEmitter; | |
}; | |
// Execute each of the listeners in order with the supplied arguments. Returns true if the event had listeners, false otherwise. | |
// emit | |
emit = (event, ...args) => { | |
for (let i = 0; i < this._events[event].length; i++) { | |
if (typeof this._events[event][i] === "function") { | |
this._events[event][i](args); | |
} else if (this._events[event][i] && this._events[event][i].listener) { | |
this._events[event][i].listener(...args); | |
delete this._events[event][i]; | |
} | |
} | |
if (this._events[event].length) { | |
return true; | |
} | |
return false; | |
}; | |
//Removes a listener from the listener array for the specified event. Caution − It changes the array indices in the listener array behind the listener. removeListener will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified event, then removeListener must be called multiple times to remove each instance. Returns emitter, so calls can be chained | |
off = (event, responseToEvent) => { | |
const eventArray = this._events[event]; | |
let i = 0; | |
let deleteCount = 0; | |
if (typeof eventArray !== "undefined") { | |
while (deleteCount < 1) { | |
// console.log(eventArray[i] && typeof eventArray[i] === 'function'); | |
if (typeof eventArray[i] === "function") { | |
eventArray.splice(i, 1); | |
deleteCount++; | |
} | |
i++; | |
} | |
} | |
return this.eventEmitter; | |
}; | |
} | |
var eventEmitter = new EventEmitter(); | |
function responseToEvent(msg) { | |
console.log(msg); | |
} | |
eventEmitter.on("pramp", responseToEvent); | |
eventEmitter.once("pramp", function(msg) { | |
console.log(msg + " just once!"); | |
}); | |
eventEmitter.emit("pramp", "1st"); | |
eventEmitter.emit("pramp", "2nd"); | |
eventEmitter.off("pramp", responseToEvent); | |
eventEmitter.emit("pramp", "3rd"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment