EventEmitter Class Lab

23.4% Acceptance

Learn and test your skills in implementing an EventEmitter class, similar to the one found in Node.js or the Event Target interface of the DOM. This class is important for creating event-driven applications and involves subscribing to events, emitting them, handling optional parameters, and managing subscriptions.

The main focus of this lab is to implement the EventEmitter class with the following two methods:

  • subscribe - This method will accept two arguments: the name of an event as a string and a callback function. When the event is emitted, the callback function will be executed. Events can have multiple listeners and their callbacks must be invoked in the order they were added. The method should return an object with an unsubscribe method that a user can use to unsubscribe.
  • emit - This method will receive the name of an event and an optional array of arguments that will be provided to the associated callbacks. If no callbacks are subscribed to the event, an empty array should be returned, otherwise, an array with the results of all callbacks should be returned, in order of their addition.

Here are some examples to help you understand the desired EventEmitter class implementation:

Example 1:

const emitter = new EventEmitter(); emitter.emit("firstEvent"); // [], no callbacks are subscribed yet emitter.subscribe("firstEvent", function cb1() { return 5; }); emitter.subscribe("firstEvent", function cb2() { return 6; }); emitter.emit("firstEvent"); // [5, 6], returns the output of cb1 and cb2

Example 2:

const emitter = new EventEmitter(); emitter.subscribe("firstEvent", function cb1(...args) { return args.join(','); }); emitter.emit("firstEvent", [1, 2, 3]); // ["1,2,3"] emitter.emit("firstEvent", [3, 4, 6]); // ["3,4,6"]

Note that the emit method should be able to accept an OPTIONAL array of arguments.

Example 3:

const emitter = new EventEmitter(); const sub = emitter.subscribe("firstEvent", (...args) => args.join(',')); emitter.emit("firstEvent", [1, 2, 3]); // ["1,2,3"] sub.unsubscribe(); // undefined emitter.emit("firstEvent", [4, 5, 6]); // [], there are no subscriptions

The challenges in this lab will guide you through testing and ensuring that your EventEmitter class implementation is correct and meets the specified requirements. Good luck and happy coding!