{"ScriptPreparationCode":null,"TestCases":[{"Name":"pubSub","Code":"class PubSub {\r\n constructor() {\r\n // Keep track of all subscribers using a Map\r\n this.subscribers = new Map();\r\n // Keep track of all topics using a Set\r\n this.topics = new Set();\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\r\n // Generate a unique subscription ID\r\n const subID = Date.now().toString();\r\n\r\n // Add the subscriber to the set of subscribers for the topic\r\n if (!this.subscribers.has(topic)) {\r\n this.subscribers.set(topic, new Map());\r\n }\r\n this.subscribers.get(topic).set(subID, onMessage);\r\n\r\n // Add the topic to the set of topics\r\n this.topics.add(topic);\r\n\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 // Validate inputs\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\r\n // If topic exists, post messages to subscribers\r\n if (this.subscribers.has(topic)) {\r\n const subscribers = this.subscribers.get(topic);\r\n for (const [subID, onMessage] of subscribers.entries()) {\r\n onMessage(message);\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 // Iterate through topics and remove the subscriber\r\n for (const topic of this.topics) {\r\n const subscribers = this.subscribers.get(topic);\r\n if (subscribers \u0026\u0026 subscribers.has(id)) {\r\n subscribers.delete(id);\r\n if (subscribers.size === 0) {\r\n this.subscribers.delete(topic);\r\n this.topics.delete(topic);\r\n }\r\n return;\r\n }\r\n }\r\n }\r\n}\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}]}