{ "data": { "question": { "questionId": "2797", "questionFrontendId": "2694", "boundTopicId": null, "title": "Event Emitter", "titleSlug": "event-emitter", "content": "
Design an EventEmitter
class. This interface is similar (but with some differences) to the one found in Node.js or the Event Target interface of the DOM. The EventEmitter
should allow for subscribing to events and emitting them.
Your EventEmitter
class should have the following two methods:
subscribe
are referentially identical.subscribe
method should also return an object with an unsubscribe
method that enables the user to unsubscribe. When it is called, the callback should be removed from the list of subscriptions and undefined
should be returned.\n
Example 1:
\n\n\nInput: actions = ["EventEmitter", "emit", "subscribe", "subscribe", "emit"], values = [[], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent"]]\nOutput: [[],["emitted",[]],["subscribed"],["subscribed"],["emitted",[5,6]]]\nExplanation: \nconst emitter = new EventEmitter();\nemitter.emit("firstEvent"); // [], no callback are subscribed yet\nemitter.subscribe("firstEvent", function cb1() { return 5; });\nemitter.subscribe("firstEvent", function cb2() { return 6; });\nemitter.emit("firstEvent"); // [5, 6], returns the output of cb1 and cb2\n\n\n
Example 2:
\n\n\nInput: actions = ["EventEmitter", "subscribe", "emit", "emit"], values = [[], ["firstEvent", "function cb1(...args) { return args.join(','); }"], ["firstEvent", [1,2,3]], ["firstEvent", [3,4,6]]]\nOutput: [[],["subscribed"],["emitted",["1,2,3"]],["emitted",["3,4,6"]]]\nExplanation: Note that the emit method should be able to accept an OPTIONAL array of arguents.\n\nconst emitter = new EventEmitter();\nemitter.subscribe("firstEvent, function cb1(...args) { return args.join(','); });\nemitter.emit("firstEvent", [1, 2, 3]); // ["1,2,3"]\nemitter.emit("firstEvent", [3, 4, 6]); // ["3,4,6"]\n\n\n
Example 3:
\n\n\nInput: actions = ["EventEmitter", "subscribe", "emit", "unsubscribe", "emit"], values = [[], ["firstEvent", "(...args) => args.join(',')"], ["firstEvent", [1,2,3]], [0], ["firstEvent", [4,5,6]]]\nOutput: [[],["subscribed"],["emitted",["1,2,3"]],["unsubscribed",0],["emitted",[]]]\nExplanation:\nconst emitter = new EventEmitter();\nconst sub = emitter.subscribe("firstEvent", (...args) => args.join(','));\nemitter.emit("firstEvent", [1, 2, 3]); // ["1,2,3"]\nsub.unsubscribe(); // undefined\nemitter.emit("firstEvent", [4, 5, 6]); // [], there are no subscriptions\n\n\n
\n
Constraints:
\n\n1 <= actions.length <= 10
values.length === actions.length
EventEmitter
, emit
, subscribe
, and unsubscribe
.EventEmitter
action doesn't take any arguments.emit
action takes between either 1 or 2 arguments. The first argument is the name of the event we want to emit, and the 2nd argument is passed to the callback functions.subscribe
action takes 2 arguments, where the first one is the event name and the second is the callback function.unsubscribe
action takes one argument, which is the 0-indexed order of the subscription made before.Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"typescript\": [\"Typescript\", \"TypeScript 4.5.4, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }