mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
63 lines
12 KiB
JSON
63 lines
12 KiB
JSON
{
|
|
"data": {
|
|
"question": {
|
|
"questionId": "2797",
|
|
"questionFrontendId": "2694",
|
|
"boundTopicId": null,
|
|
"title": "Event Emitter",
|
|
"titleSlug": "event-emitter",
|
|
"content": "<p>Design an <code>EventEmitter</code> 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 <code>EventEmitter</code> should allow for subscribing to events and emitting them.</p>\n\n<p>Your <code>EventEmitter</code> class should have the following two methods:</p>\n\n<ul>\n\t<li><strong>subscribe</strong> - This method takes in two arguments: the name of an event as a string and a callback function. This callback function will later be called when the event is emitted.<br />\n\tAn event should be able to have multiple listeners for the same event. When emitting an event with multiple callbacks, each should be called in the order in which they were subscribed. An array of results should be returned. You can assume no callbacks passed to <code>subscribe</code> are referentially identical.<br />\n\tThe <code>subscribe</code> method should also return an object with an <code>unsubscribe</code> method that enables the user to unsubscribe. When it is called, the callback should be removed from the list of subscriptions and <code>undefined</code> should be returned.</li>\n\t<li><strong>emit</strong> - This method takes in two arguments: the name of an event as a string and an optional array of arguments that will be passed to the callback(s). If there are no callbacks subscribed to the given event, return an empty array. Otherwise, return an array of the results of all callback calls in the order they were subscribed.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nactions = ["EventEmitter", "emit", "subscribe", "subscribe", "emit"], \nvalues = [[], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 6; }"], ["firstEvent"]]\n<strong>Output:</strong> [[],["emitted",[]],["subscribed"],["subscribed"],["emitted",[5,6]]]\n<strong>Explanation:</strong> \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</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nactions = ["EventEmitter", "subscribe", "emit", "emit"], \nvalues = [[], ["firstEvent", "function cb1(...args) { return args.join(','); }"], ["firstEvent", [1,2,3]], ["firstEvent", [3,4,6]]]\n<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["emitted",["3,4,6"]]]\n<strong>Explanation: </strong>Note that the emit method should be able to accept an OPTIONAL array of arguments.\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</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nactions = ["EventEmitter", "subscribe", "emit", "unsubscribe", "emit"], \nvalues = [[], ["firstEvent", "(...args) => args.join(',')"], ["firstEvent", [1,2,3]], [0], ["firstEvent", [4,5,6]]]\n<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["unsubscribed",0],["emitted",[]]]\n<strong>Explanation:</strong>\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</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nactions = ["EventEmitter", "subscribe", "subscribe", "unsubscribe", "emit"], \nvalues = [[], ["firstEvent", "x => x + 1"], ["firstEvent", "x => x + 2"], [0], ["firstEvent", [5]]]\n<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["unsubscribed",0],["emitted",[7]]]\n<strong>Explanation:</strong>\nconst emitter = new EventEmitter();\nconst sub1 = emitter.subscribe("firstEvent", x => x + 1);\nconst sub2 = emitter.subscribe("firstEvent", x => x + 2);\nsub1.unsubscribe(); // undefined\nemitter.emit("firstEvent", [5]); // [7]</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= actions.length <= 10</code></li>\n\t<li><code>values.length === actions.length</code></li>\n\t<li>All test cases are valid, e.g. you don't need to handle scenarios when unsubscribing from a non-existing subscription.</li>\n\t<li>There are only 4 different actions: <code>EventEmitter</code>, <code>emit</code>, <code>subscribe</code>, and <code>unsubscribe</code>.</li>\n\t<li>The <code>EventEmitter</code> action doesn't take any arguments.</li>\n\t<li>The <code>emit</code> 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.</li>\n\t<li>The <code>subscribe</code> action takes 2 arguments, where the first one is the event name and the second is the callback function.</li>\n\t<li>The <code>unsubscribe</code> action takes one argument, which is the 0-indexed order of the subscription made before.</li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Medium",
|
|
"likes": 188,
|
|
"dislikes": 16,
|
|
"isLiked": null,
|
|
"similarQuestions": "[]",
|
|
"exampleTestcases": "[\"EventEmitter\", \"emit\", \"subscribe\", \"subscribe\", \"emit\"]\n[[], [\"firstEvent\"], [\"firstEvent\", \"function cb1() { return 5; }\"], [\"firstEvent\", \"function cb1() { return 6; }\"], [\"firstEvent\"]]\n[\"EventEmitter\", \"subscribe\", \"emit\", \"emit\"]\n[[], [\"firstEvent\", \"function cb1(...args) { return args.join(','); }\"], [\"firstEvent\", [1,2,3]], [\"firstEvent\", [3,4,6]]]\n[\"EventEmitter\", \"subscribe\", \"emit\", \"unsubscribe\", \"emit\"]\n[[], [\"firstEvent\", \"(...args) => args.join(',')\"], [\"firstEvent\", [1,2,3]], [0], [\"firstEvent\", [4,5,6]]]\n[\"EventEmitter\", \"subscribe\", \"subscribe\", \"unsubscribe\", \"emit\"]\n[[], [\"firstEvent\", \"x => x + 1\"], [\"firstEvent\", \"x => x + 2\"], [0], [\"firstEvent\", [5]]]",
|
|
"categoryTitle": "JavaScript",
|
|
"contributors": [],
|
|
"topicTags": [],
|
|
"companyTagStats": null,
|
|
"codeSnippets": [
|
|
{
|
|
"lang": "JavaScript",
|
|
"langSlug": "javascript",
|
|
"code": "class EventEmitter {\n \n /**\n * @param {string} eventName\n * @param {Function} callback\n * @return {Object}\n */\n\tsubscribe(eventName, callback) {\n \t\n\t\treturn {\n\t\t\tunsubscribe: () => {\n\t\t\t\t\n\t\t\t}\n\t\t};\n\t}\n \n /**\n * @param {string} eventName\n * @param {Array} args\n * @return {Array}\n */\n\temit(eventName, args = []) {\n\t\t\n\t}\n}\n\n/**\n * const emitter = new EventEmitter();\n *\n * // Subscribe to the onClick event with onClickCallback\n * function onClickCallback() { return 99 }\n * const sub = emitter.subscribe('onClick', onClickCallback);\n *\n * emitter.emit('onClick'); // [99]\n * sub.unsubscribe(); // undefined\n * emitter.emit('onClick'); // []\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"code": "type Callback = (...args: any[]) => any;\ntype Subscription = {\n unsubscribe: () => void\n}\n\nclass EventEmitter {\n \n\tsubscribe(eventName: string, callback: Callback): Subscription {\n\t\t\n\t\treturn {\n\t\t\tunsubscribe: () => {\n\t\t\t\t\n\t\t\t}\n };\n\t}\n\n\temit(eventName: string, args: any[] = []): any[] {\n\t\t\n\t}\n}\n\n/**\n * const emitter = new EventEmitter();\n *\n * // Subscribe to the onClick event with onClickCallback\n * function onClickCallback() { return 99 }\n * const sub = emitter.subscribe('onClick', onClickCallback);\n *\n * emitter.emit('onClick'); // [99]\n * sub.unsubscribe(); // undefined\n * emitter.emit('onClick'); // []\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"13.4K\", \"totalSubmission\": \"16.6K\", \"totalAcceptedRaw\": 13449, \"totalSubmissionRaw\": 16620, \"acRate\": \"80.9%\"}",
|
|
"hints": [],
|
|
"solution": {
|
|
"id": "1925",
|
|
"canSeeDetail": false,
|
|
"paidOnly": true,
|
|
"hasVideoSolution": false,
|
|
"paidOnlyVideo": true,
|
|
"__typename": "ArticleNode"
|
|
},
|
|
"status": null,
|
|
"sampleTestCase": "[\"EventEmitter\", \"emit\", \"subscribe\", \"subscribe\", \"emit\"]\n[[], [\"firstEvent\"], [\"firstEvent\", \"function cb1() { return 5; }\"], [\"firstEvent\", \"function cb1() { return 6; }\"], [\"firstEvent\"]]",
|
|
"metaData": "{\n \"name\": \"EventEmitter\",\n \"params\": [\n {\n \"type\": \"string[]\",\n \"name\": \"actions\"\n },\n {\n \"type\": \"character[][]\",\n \"name\": \"values\"\n }\n ],\n \"return\": {\n \"type\": \"void\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}",
|
|
"judgerAvailable": true,
|
|
"judgeType": "large",
|
|
"mysqlSchemas": [],
|
|
"enableRunCode": true,
|
|
"enableTestMode": false,
|
|
"enableDebugger": false,
|
|
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
|
"libraryUrl": null,
|
|
"adminUrl": null,
|
|
"challengeQuestion": null,
|
|
"__typename": "QuestionNode"
|
|
}
|
|
}
|
|
} |