Refactor logging and formatting

Improved code readability
This commit is contained in:
Dariusz L
2025-06-27 05:32:11 +02:00
parent be4fae2964
commit 83ce890ef4
16 changed files with 241 additions and 363 deletions

View File

@@ -128,7 +128,7 @@ export function getStateSignature(layers) {
return JSON.stringify(layers.map((layer, index) => {
const sig = {
index: index,
x: Math.round(layer.x * 100) / 100, // Round to avoid floating point precision issues
x: Math.round(layer.x * 100) / 100,
y: Math.round(layer.y * 100) / 100,
width: Math.round(layer.width * 100) / 100,
height: Math.round(layer.height * 100) / 100,
@@ -137,15 +137,11 @@ export function getStateSignature(layers) {
blendMode: layer.blendMode || 'normal',
opacity: layer.opacity !== undefined ? Math.round(layer.opacity * 100) / 100 : 1
};
// Include imageId if available
if (layer.imageId) {
sig.imageId = layer.imageId;
}
// Include image src as fallback identifier
if (layer.image && layer.image.src) {
sig.imageSrc = layer.image.src.substring(0, 100); // First 100 chars to avoid huge signatures
sig.imageSrc = layer.image.src.substring(0, 100);
}
return sig;

View File

@@ -10,8 +10,8 @@ class WebSocketManager {
this.isConnecting = false;
this.reconnectAttempts = 0;
this.maxReconnectAttempts = 10;
this.reconnectInterval = 5000; // 5 seconds
this.ackCallbacks = new Map(); // Store callbacks for messages awaiting ACK
this.reconnectInterval = 5000;
this.ackCallbacks = new Map();
this.messageIdCounter = 0;
this.connect();
@@ -54,7 +54,6 @@ class WebSocketManager {
this.ackCallbacks.delete(data.nodeId);
}
}
// Handle other incoming messages if needed
} catch (error) {
log.error("Error parsing incoming WebSocket message:", error);
}
@@ -73,7 +72,6 @@ class WebSocketManager {
this.socket.onerror = (error) => {
this.isConnecting = false;
log.error("WebSocket error:", error);
// The onclose event will be fired next, which will handle reconnection.
};
} catch (error) {
this.isConnecting = false;
@@ -106,12 +104,11 @@ class WebSocketManager {
log.debug("Sent message:", data);
if (requiresAck) {
log.debug(`Message for nodeId ${nodeId} requires ACK. Setting up callback.`);
// Set a timeout for the ACK
const timeout = setTimeout(() => {
this.ackCallbacks.delete(nodeId);
reject(new Error(`ACK timeout for nodeId ${nodeId}`));
log.warn(`ACK timeout for nodeId ${nodeId}.`);
}, 10000); // 10-second timeout
}, 10000);
this.ackCallbacks.set(nodeId, {
resolve: (responseData) => {
@@ -124,18 +121,14 @@ class WebSocketManager {
}
});
} else {
resolve(); // Resolve immediately if no ACK is needed
resolve();
}
} else {
log.warn("WebSocket not open. Queuing message.");
// Note: The current queueing doesn't support ACK promises well.
// For simplicity, we'll focus on the connected case.
// A more robust implementation would wrap the queued message in a function.
this.messageQueue.push(message);
if (!this.isConnecting) {
this.connect();
}
// For now, we reject if not connected and ACK is required.
if (requiresAck) {
reject(new Error("Cannot send message with ACK required while disconnected."));
}
@@ -145,16 +138,11 @@ class WebSocketManager {
flushMessageQueue() {
log.debug(`Flushing ${this.messageQueue.length} queued messages.`);
// Note: This simple flush doesn't handle ACKs for queued messages.
// This should be acceptable as data is sent right before queueing a prompt,
// at which point the socket should ideally be connected.
while (this.messageQueue.length > 0) {
const message = this.messageQueue.shift();
this.socket.send(message);
}
}
}
// Create a singleton instance of the WebSocketManager
const wsUrl = `ws://${window.location.host}/layerforge/canvas_ws`;
const wsUrl = `ws:
export const webSocketManager = new WebSocketManager(wsUrl);