| 1 | // A messy and tightly coupled event processing system with poor separation of concerns |
| 2 | // Difficulty: 4 |
| 3 | |
| 4 | type EventType = "click" | "hover" | "scroll" | "keypress" | "resize" | "load"; |
| 5 | type EventCallback = (data: any) => void; |
| 6 | |
| 7 | // Global state - everything is mixed together |
| 8 | const globalState = { |
| 9 | count: 0, |
| 10 | lastEvent: null as any, |
| 11 | isActive: false, |
| 12 | username: "", |
| 13 | token: "", |
| 14 | preferences: { theme: "light", fontSize: 12, notifications: true }, |
| 15 | eventHistory: [] as string[], |
| 16 | userData: null as any, |
| 17 | }; |
| 18 | |
| 19 | // God class that handles everything |
| 20 | class EventManager { |
| 21 | private events: Map<EventType, EventCallback[]> = new Map(); |
| 22 | private static instance: EventManager; |
| 23 | private isProcessing = false; |
| 24 | private processingQueue: Array<{ type: EventType; data: any }> = []; |
| 25 | |
| 26 | private constructor() { |
| 27 | // Initialize default handlers |
| 28 | this.events.set("click", []); |
| 29 | this.events.set("hover", []); |
| 30 | this.events.set("scroll", []); |
| 31 | this.events.set("keypress", []); |
| 32 | this.events.set("resize", []); |
| 33 | this.events.set("load", []); |
| 34 | |
| 35 | // Add default behavior |
| 36 | this.on("click", (data) => { |
| 37 | console.log("Default click handler:", data); |
| 38 | globalState.count++; |
| 39 | globalState.lastEvent = { type: "click", data, timestamp: Date.now() }; |
| 40 | globalState.eventHistory.push(`click:${JSON.stringify(data)}`); |
| 41 | |
| 42 | // Side effects directly in handler |
| 43 | this.updateUI(); |
| 44 | |
| 45 | // Mixes concerns by also handling auth |
| 46 | if (data.target && data.target.id === "login-button") { |
| 47 | this.login(globalState.username, "hardcoded-password"); |
| 48 | } |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | // Singleton pattern |
| 53 | public static getInstance(): EventManager { |
| 54 | if (!EventManager.instance) { |
| 55 | EventManager.instance = new EventManager(); |
| 56 | } |
| 57 | return EventManager.instance; |
| 58 | } |
| 59 | |
| 60 | public on(eventType: EventType, callback: EventCallback): void { |
| 61 | const handlers = this.events.get(eventType) || []; |
| 62 | handlers.push(callback); |
| 63 | this.events.set(eventType, handlers); |
| 64 | } |
| 65 | |
| 66 | public emit(eventType: EventType, data: any): void { |
| 67 | globalState.lastEvent = { type: eventType, data, timestamp: Date.now() }; |
| 68 | |
| 69 | if (this.isProcessing) { |
| 70 | this.processingQueue.push({ type: eventType, data }); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | this.isProcessing = true; |
| 75 | |
| 76 | try { |
| 77 | const handlers = this.events.get(eventType) || []; |
| 78 | for (const handler of handlers) { |
| 79 | handler(data); |
| 80 | |
| 81 | // Random state modifications scattered throughout code |
| 82 | globalState.eventHistory.push(`${eventType}:${JSON.stringify(data)}`); |
| 83 | } |
| 84 | |
| 85 | // UI Update with every event |
| 86 | this.updateUI(); |
| 87 | |
| 88 | // Process queue |
| 89 | while (this.processingQueue.length > 0) { |
| 90 | const next = this.processingQueue.shift()!; |
| 91 | const queueHandlers = this.events.get(next.type) || []; |
| 92 | for (const handler of queueHandlers) { |
| 93 | handler(next.data); |
| 94 | } |
| 95 | } |
| 96 | } finally { |
| 97 | this.isProcessing = false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Mixed UI concerns with event handling |
| 102 | private updateUI(): void { |
| 103 | // Directly manipulates DOM |
| 104 | const countElement = document.getElementById("count"); |
| 105 | if (countElement) { |
| 106 | countElement.textContent = String(globalState.count); |
| 107 | } |
| 108 | |
| 109 | // Apply theme |
| 110 | document.body.className = globalState.preferences.theme; |
| 111 | |
| 112 | // Update last event display |
| 113 | const lastEventElement = document.getElementById("lastEvent"); |
| 114 | if (lastEventElement && globalState.lastEvent) { |
| 115 | lastEventElement.textContent = JSON.stringify(globalState.lastEvent); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Authentication logic mixed with event handling |
| 120 | private login(username: string, password: string): void { |
| 121 | console.log(`Attempting login for ${username}...`); |
| 122 | |
| 123 | // Simulate API call |
| 124 | setTimeout(() => { |
| 125 | globalState.isActive = true; |
| 126 | globalState.token = `token-${Math.random().toString(36).substring(7)}`; |
| 127 | globalState.userData = { |
| 128 | name: username, |
| 129 | role: "user", |
| 130 | loginTime: new Date(), |
| 131 | }; |
| 132 | |
| 133 | // Update UI after login |
| 134 | this.updateUI(); |
| 135 | |
| 136 | // Emit another event |
| 137 | this.emit("load", { user: username }); |
| 138 | }, 1000); |
| 139 | } |
| 140 | |
| 141 | // Analytics mixed in |
| 142 | public trackEvent(category: string, action: string, label?: string): void { |
| 143 | const eventData = { category, action, label, timestamp: Date.now() }; |
| 144 | console.log("Tracking event:", eventData); |
| 145 | |
| 146 | // Update global state |
| 147 | globalState.eventHistory.push(`analytics:${JSON.stringify(eventData)}`); |
| 148 | |
| 149 | // Send to analytics service |
| 150 | if (globalState.isActive) { |
| 151 | this.sendToAnalyticsService(eventData); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private sendToAnalyticsService(data: any): void { |
| 156 | // Pretend API call |
| 157 | console.log("Sending to analytics service:", data); |
| 158 | } |
| 159 | |
| 160 | // Utility functions mixed in |
| 161 | public formatEventHistory(): string { |
| 162 | return globalState.eventHistory |
| 163 | .map((event, index) => `${index + 1}. ${event}`) |
| 164 | .join("\n"); |
| 165 | } |
| 166 | |
| 167 | // Settings management mixed in |
| 168 | public updatePreference( |
| 169 | key: "theme" | "fontSize" | "notifications", |
| 170 | value: any, |
| 171 | ): void { |
| 172 | globalState.preferences[key] = value; |
| 173 | this.updateUI(); |
| 174 | this.trackEvent("preferences", "update", `${key}:${value}`); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Export singleton |
| 179 | export const eventManager = EventManager.getInstance(); |
| 180 | |
| 181 | // Helper functions that directly use global state |
| 182 | export function getCurrentCount(): number { |
| 183 | return globalState.count; |
| 184 | } |
| 185 | |
| 186 | export function resetCount(): void { |
| 187 | globalState.count = 0; |
| 188 | eventManager.emit("click", { action: "reset" }); |
| 189 | } |
| 190 | |
| 191 | export function setUsername(name: string): void { |
| 192 | globalState.username = name; |
| 193 | } |
| 194 | |
| 195 | // Example usage |
| 196 | document.addEventListener("DOMContentLoaded", () => { |
| 197 | eventManager.emit("load", { page: "home" }); |
| 198 | |
| 199 | // Add event listeners |
| 200 | document.addEventListener("click", (e) => { |
| 201 | eventManager.emit("click", { |
| 202 | target: e.target, |
| 203 | x: e.clientX, |
| 204 | y: e.clientY, |
| 205 | }); |
| 206 | }); |
| 207 | |
| 208 | document.addEventListener("keypress", (e) => { |
| 209 | eventManager.emit("keypress", { key: e.key, code: e.code }); |
| 210 | }); |
| 211 | }); |