{"ScriptPreparationCode":null,"TestCases":[{"Name":"pubSub","Code":"class PubSub {\r\n constructor() {\r\n // Keep track of all \u0060onMessage()\u0060 listeners with easy lookup by subscription id.\r\n this.subscriberOnMsg = {};\r\n // Keep track of the topic for each subscription id for easier cleanup.\r\n this.subscriberTopics = {};\r\n // Keep track of all topics and subscriber ids for each topic.\r\n this.topics = {};\r\n }\r\n\r\n /**\r\n * Subscribe to messages being published in the given topic.\r\n * @param {string} topic - Name of the channel/topic where messages are published.\r\n * @param {Function} onMessage - Function called whenever new messages on the topic are published.\r\n * @returns {string} ID of this subscription.\r\n */\r\n subscribe(topic, onMessage) {\r\n // Validate inputs\r\n if (typeof topic !== \u0022string\u0022) throw new Error(\u0022Topic must be a string.\u0022);\r\n if (typeof onMessage !== \u0022function\u0022)\r\n throw new Error(\u0022onMessage must be a function.\u0022);\r\n // Each subscription has a unique id\r\n const subID = Date.now();\r\n // Create or Update the topic\r\n if (!(topic in this.topics)) {\r\n // New topic\r\n this.topics[topic] = [subID];\r\n } else {\r\n // Topic exists\r\n this.topics[topic].push(subID);\r\n }\r\n // Store onMessage and topic separately for faster lookup\r\n this.subscriberOnMsg[subID] = onMessage;\r\n this.subscriberTopics[subID] = topic;\r\n // Return the subscription id\r\n return subID;\r\n }\r\n\r\n /**\r\n * Publish messages on a topic for all subscribers to receive.\r\n * @param {string} topic - The topic where the message is sent.\r\n * @param {Object} message - The message to send. Only object format is supported.\r\n */\r\n publish(topic, message) {\r\n if (typeof topic !== \u0022string\u0022) throw new Error(\u0022Topic must be a string.\u0022);\r\n if (typeof message !== \u0022object\u0022) {\r\n throw new Error(\u0022Message must be an object.\u0022);\r\n }\r\n // If topic exists, post messages\r\n if (topic in this.topics) {\r\n const subIDs = this.topics[topic];\r\n subIDs.forEach((id) =\u003E {\r\n if (id in this.subscriberOnMsg) {\r\n this.subscriberOnMsg[id](message);\r\n }\r\n });\r\n }\r\n }\r\n\r\n /**\r\n * Unsubscribe for a given subscription id.\r\n * @param {string} id - Subscription id\r\n */\r\n unsubscribe(id) {\r\n // Validate inputs\r\n if (typeof id !== \u0022string\u0022 || !validateUUID(id)) {\r\n throw new Error(\u0022ID must be a valid UUID.\u0022);\r\n }\r\n // If the id exists in our subscriptions then clear it.\r\n if (id in this.subscriberOnMsg \u0026\u0026 id in this.subscriberTopics) {\r\n // Delete message listener\r\n delete this.subscriberOnMsg[id];\r\n // Remove id from the topics tracker\r\n const topic = this.subscriberTopics[id];\r\n // Cleanup topics\r\n if (topic \u0026\u0026 topic in this.topics) {\r\n const idx = this.topics[topic].findIndex((tID) =\u003E tID === id);\r\n if (idx \u003E -1) {\r\n this.topics[topic].splice(idx, 1);\r\n }\r\n // If there are no more listeners, clean up the topic as well\r\n if (this.topics[topic].length === 0) {\r\n delete this.topics[topic];\r\n }\r\n }\r\n // Delete the topic for this id\r\n delete this.subscriberTopics[id];\r\n }\r\n }\r\n}\r\n\r\n// Example usage:\r\nconst results = [];\r\nconst PS = new PubSub();\r\n\r\nconst subId = PS.subscribe(\u0022myTopic\u0022, (message) =\u003E {\r\n console.log({ message });\r\n results.push(message);\r\n});\r\n\r\nPS.publish(\u0022myTopic\u0022, { message: \u0022Hello World\u0022 });\r\nlet i = 10000;\r\nwhile (--i) {\r\n PS.publish(\u0022myTopic\u0022, { message: \u0022Hello World\u0022 });\r\n}","IsDeferred":false},{"Name":"custom event","Code":"class PubSub {\r\n constructor() {\r\n // Create a DOM element to serve as the event bus\r\n this.eventBus = document.createElement(\u0022div\u0022);\r\n }\r\n\r\n /**\r\n * Subscribe to events with a given topic.\r\n * @param {string} topic - The topic to subscribe to.\r\n * @param {Function} callback - The callback function to execute when the event is published.\r\n * @returns {Function} - A function to unsubscribe from the topic.\r\n */\r\n subscribe(topic, callback) {\r\n // Create a unique event type for the topic\r\n const eventType = \u0060pubsub_${topic}\u0060;\r\n\r\n // Define the event handler function\r\n const eventHandler = (event) =\u003E {\r\n callback(event.detail);\r\n };\r\n\r\n // Add the event handler to the event bus\r\n this.eventBus.addEventListener(eventType, eventHandler);\r\n\r\n // Return an unsubscribe function\r\n return () =\u003E {\r\n this.eventBus.removeEventListener(eventType, eventHandler);\r\n };\r\n }\r\n\r\n /**\r\n * Publish an event with a given topic and data.\r\n * @param {string} topic - The topic of the event.\r\n * @param {any} data - The data to send with the event.\r\n */\r\n publish(topic, data) {\r\n // Create a unique event type for the topic\r\n const eventType = \u0060pubsub_${topic}\u0060;\r\n\r\n // Create a custom event with the data\r\n const customEvent = new CustomEvent(eventType, {\r\n detail: data,\r\n });\r\n\r\n // Dispatch the custom event on the event bus\r\n this.eventBus.dispatchEvent(customEvent);\r\n }\r\n}\r\n\r\n// Example usage:\r\nconst PS = new PubSub();\r\nconst results = [];\r\n\r\n// Subscribe to an event\r\nPS.subscribe(\u0022myTopic\u0022, (message) =\u003E {\r\n console.log({ message });\r\n results.push(message);\r\n});\r\n\r\nlet i = 10000;\r\nwhile (--i) {\r\n PS.publish(\u0022myTopic\u0022, { message: \u0022Hello World\u0022 });\r\n}","IsDeferred":false}]}