UNPKG

@ordojs/dev-tools

Version:

Advanced developer tools for OrdoJS with component inspector, AST explorer, and performance profiling

164 lines (162 loc) 3.98 kB
import { EventEmitter } from 'events'; // src/hmr/index.ts var EnhancedHMR = class extends EventEmitter { updates; isRunning; port; statePreservation; /** * Create a new EnhancedHMR instance * * @param port - WebSocket port for enhanced HMR * @param statePreservation - Enable state preservation */ constructor(port = 24684, statePreservation = true) { super(); this.updates = /* @__PURE__ */ new Map(); this.isRunning = false; this.port = port; this.statePreservation = statePreservation; } /** * Start the enhanced HMR */ async start() { if (this.isRunning) { console.warn("Enhanced HMR is already running"); return; } try { await this.startWebSocketServer(); this.isRunning = true; console.log(`Enhanced HMR started on port ${this.port}`); this.emit("started"); } catch (error) { console.error("Failed to start enhanced HMR:", error); this.emit("error", error); throw error; } } /** * Stop the enhanced HMR */ async stop() { if (!this.isRunning) { console.warn("Enhanced HMR is not running"); return; } try { await this.stopWebSocketServer(); this.isRunning = false; console.log("Enhanced HMR stopped"); this.emit("stopped"); } catch (error) { console.error("Failed to stop enhanced HMR:", error); this.emit("error", error); throw error; } } /** * Send an HMR update * * @param update - HMR update information */ sendUpdate(update) { const updateId = this.generateUpdateId(update); this.updates.set(updateId, update); this.emit("updateSent", { updateId, update }); } /** * Get update by ID * * @param updateId - Update identifier * @returns HMR update or undefined */ getUpdate(updateId) { return this.updates.get(updateId); } /** * Get all updates * * @returns Array of all updates */ getAllUpdates() { return Array.from(this.updates.values()); } /** * Clear all updates */ clearUpdates() { this.updates.clear(); this.emit("updatesCleared"); } /** * Get HMR statistics * * @returns HMR statistics */ getHMRStats() { const updates = Array.from(this.updates.values()); const updatesByType = {}; const updatesByFile = {}; let totalUpdateTime = 0; for (const update of updates) { updatesByType[update.type] = (updatesByType[update.type] || 0) + 1; updatesByFile[update.filePath] = (updatesByFile[update.filePath] || 0) + 1; totalUpdateTime += Date.now() - update.timestamp; } const averageUpdateTime = updates.length > 0 ? totalUpdateTime / updates.length : 0; return { totalUpdates: updates.length, updatesByType, updatesByFile, averageUpdateTime }; } /** * Enable state preservation */ enableStatePreservation() { this.statePreservation = true; this.emit("statePreservationEnabled"); } /** * Disable state preservation */ disableStatePreservation() { this.statePreservation = false; this.emit("statePreservationDisabled"); } /** * Check if state preservation is enabled * * @returns True if state preservation is enabled */ isStatePreservationEnabled() { return this.statePreservation; } /** * Generate update ID * * @param update - HMR update * @returns Update ID */ generateUpdateId(update) { return `${update.type}-${update.filePath}-${update.timestamp}`; } /** * Start WebSocket server for enhanced HMR communication */ async startWebSocketServer() { console.log("Starting WebSocket server for enhanced HMR..."); } /** * Stop WebSocket server */ async stopWebSocketServer() { console.log("Stopping WebSocket server for enhanced HMR..."); } }; export { EnhancedHMR }; //# sourceMappingURL=index.mjs.map //# sourceMappingURL=index.mjs.map