codebridge-ai
Version:
Complete fullstack AI coding platform with AST-based code integration and local LLM support. Now with comprehensive web technology support (HTML/CSS/JS) plus JavaScript, Python, Rust, and C++.
21 lines • 3.35 kB
JSON
{
"timestamp": "2025-08-02T16:18:38.506Z",
"model": "deepseek-coder:6.7b",
"language": "javascript",
"scenario": "class-optimization",
"task": "Add off method, once method, and error handling",
"originalCode": "class EventEmitter {\n constructor() {\n this.events = {};\n }\n \n on(event, callback) {\n if (!this.events[event]) {\n this.events[event] = [];\n }\n this.events[event].push(callback);\n }\n \n emit(event, data) {\n if (this.events[event]) {\n this.events[event].forEach(cb => cb(data));\n }\n }\n}",
"rawResponse": "Here is your modified EventEmitter class with added `off`, `once` methods and some error handling:\n\n```javascript\nclass EventEmitter {\n constructor() {\n this.events = {};\n }\n \n on(event, callback) {\n if (typeof callback !== 'function') throw new Error('Callback must be a function');\n \n if (!this.events[event]) {\n this.events[event] = [];\n }\n this.events[event].push(callback);\n }\n \n off(event, callback) {\n if (typeof event !== 'string') throw new Error('Event must be a string');\n \n if (!this.events[event]) return; // If no such event exists, do nothing\n \n this.events[event] = this.events[event].filter(cb => cb !== callback);\n }\n \n once(event, callback) {\n const self = this;\n function fn() {\n self.off(event, fn); // Remove the event listener after it's called\n return callback.apply(this, arguments);\n }\n \n if (typeof callback !== 'function') throw new Error('Callback must be a function');\n \n this.on(event, fn);\n }\n \n emit(event, data) {\n if (!this.events[event]) return; // If no such event exists, do nothing\n \n this.events[event].forEach(cb => cb(data));\n }\n}\n```\nThis class now supports removing a specific callback function from an event using the `off` method, setting up a one-time listener with the `once` method and includes some basic error handling for invalid inputs.\n",
"improvedCode": "javascript\nclass EventEmitter {\n constructor() {\n this.events = {};\n }\n \n on(event, callback) {\n if (typeof callback !== 'function') throw new Error('Callback must be a function');\n \n if (!this.events[event]) {\n this.events[event] = [];\n }\n this.events[event].push(callback);\n }\n \n off(event, callback) {\n if (typeof event !== 'string') throw new Error('Event must be a string');\n \n if (!this.events[event]) return; // If no such event exists, do nothing\n \n this.events[event] = this.events[event].filter(cb => cb !== callback);\n }\n \n once(event, callback) {\n const self = this;\n function fn() {\n self.off(event, fn); // Remove the event listener after it's called\n return callback.apply(this, arguments);\n }\n \n if (typeof callback !== 'function') throw new Error('Callback must be a function');\n \n this.on(event, fn);\n }\n \n emit(event, data) {\n if (!this.events[event]) return; // If no such event exists, do nothing\n \n this.events[event].forEach(cb => cb(data));\n }\n}",
"duration": 10132,
"success": true,
"qualityMetrics": {
"hasCode": true,
"hasErrorHandling": true,
"hasModernSyntax": true,
"score": 0.7,
"rawScore": 0.7,
"maxScore": 1
},
"error": null
}