mirror of
https://github.com/Azornes/Comfyui-LayerForge.git
synced 2026-03-22 05:02:11 -03:00
Add WebSocket-based RAM output for CanvasNode
Introduces a WebSocket-based mechanism for CanvasNode to send and receive canvas image and mask data in RAM, enabling fast, diskless data transfer between frontend and backend. Adds a new WebSocketManager utility, updates CanvasIO to support RAM output mode, and modifies CanvasView to send canvas data via WebSocket before prompt execution. The backend (canvas_node.py) is updated to handle WebSocket data storage and retrieval, with improved locking and cleanup logic. This change improves workflow speed and reliability by avoiding unnecessary disk I/O and ensuring up-to-date canvas data is available during node execution.
This commit is contained in:
@@ -266,9 +266,6 @@ export class Canvas {
|
||||
|
||||
|
||||
|
||||
async saveToServer(fileName) {
|
||||
return this.canvasIO.saveToServer(fileName);
|
||||
}
|
||||
|
||||
async getFlattenedCanvasAsBlob() {
|
||||
return this.canvasLayers.getFlattenedCanvasAsBlob();
|
||||
|
||||
193
js/CanvasIO.js
193
js/CanvasIO.js
@@ -1,5 +1,6 @@
|
||||
import {createCanvas} from "./utils/CommonUtils.js";
|
||||
import {createModuleLogger} from "./utils/LoggerUtils.js";
|
||||
import {webSocketManager} from "./utils/WebSocketManager.js";
|
||||
|
||||
const log = createModuleLogger('CanvasIO');
|
||||
|
||||
@@ -9,35 +10,38 @@ export class CanvasIO {
|
||||
this._saveInProgress = null;
|
||||
}
|
||||
|
||||
async saveToServer(fileName) {
|
||||
if (!window.canvasSaveStates) {
|
||||
window.canvasSaveStates = new Map();
|
||||
}
|
||||
|
||||
const nodeId = this.canvas.node.id;
|
||||
const saveKey = `${nodeId}_${fileName}`;
|
||||
if (this._saveInProgress || window.canvasSaveStates.get(saveKey)) {
|
||||
log.warn(`Save already in progress for node ${nodeId}, waiting...`);
|
||||
return this._saveInProgress || window.canvasSaveStates.get(saveKey);
|
||||
}
|
||||
async saveToServer(fileName, outputMode = 'disk') {
|
||||
if (outputMode === 'disk') {
|
||||
if (!window.canvasSaveStates) {
|
||||
window.canvasSaveStates = new Map();
|
||||
}
|
||||
|
||||
log.info(`Starting saveToServer with fileName: ${fileName} for node: ${nodeId}`);
|
||||
log.debug(`Canvas dimensions: ${this.canvas.width}x${this.canvas.height}`);
|
||||
log.debug(`Number of layers: ${this.canvas.layers.length}`);
|
||||
this._saveInProgress = this._performSave(fileName);
|
||||
window.canvasSaveStates.set(saveKey, this._saveInProgress);
|
||||
|
||||
try {
|
||||
const result = await this._saveInProgress;
|
||||
return result;
|
||||
} finally {
|
||||
this._saveInProgress = null;
|
||||
window.canvasSaveStates.delete(saveKey);
|
||||
log.debug(`Save completed for node ${nodeId}, lock released`);
|
||||
const nodeId = this.canvas.node.id;
|
||||
const saveKey = `${nodeId}_${fileName}`;
|
||||
if (this._saveInProgress || window.canvasSaveStates.get(saveKey)) {
|
||||
log.warn(`Save already in progress for node ${nodeId}, waiting...`);
|
||||
return this._saveInProgress || window.canvasSaveStates.get(saveKey);
|
||||
}
|
||||
|
||||
log.info(`Starting saveToServer (disk) with fileName: ${fileName} for node: ${nodeId}`);
|
||||
this._saveInProgress = this._performSave(fileName, outputMode);
|
||||
window.canvasSaveStates.set(saveKey, this._saveInProgress);
|
||||
|
||||
try {
|
||||
return await this._saveInProgress;
|
||||
} finally {
|
||||
this._saveInProgress = null;
|
||||
window.canvasSaveStates.delete(saveKey);
|
||||
log.debug(`Save completed for node ${nodeId}, lock released`);
|
||||
}
|
||||
} else {
|
||||
// For RAM mode, we don't need the lock/state management as it's synchronous
|
||||
log.info(`Starting saveToServer (RAM) for node: ${this.canvas.node.id}`);
|
||||
return this._performSave(fileName, outputMode);
|
||||
}
|
||||
}
|
||||
|
||||
async _performSave(fileName) {
|
||||
async _performSave(fileName, outputMode) {
|
||||
if (this.canvas.layers.length === 0) {
|
||||
log.warn(`Node ${this.canvas.node.id} has no layers, creating empty canvas`);
|
||||
return Promise.resolve(true);
|
||||
@@ -152,6 +156,15 @@ export class CanvasIO {
|
||||
maskCtx.globalCompositeOperation = 'source-over';
|
||||
maskCtx.drawImage(tempMaskCanvas, 0, 0);
|
||||
}
|
||||
if (outputMode === 'ram') {
|
||||
const imageData = tempCanvas.toDataURL('image/png');
|
||||
const maskData = maskCanvas.toDataURL('image/png');
|
||||
log.info("Returning image and mask data as base64 for RAM mode.");
|
||||
resolve({ image: imageData, mask: maskData });
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Disk Mode (original logic) ---
|
||||
const fileNameWithoutMask = fileName.replace('.png', '_without_mask.png');
|
||||
log.info(`Saving image without mask as: ${fileNameWithoutMask}`);
|
||||
|
||||
@@ -204,7 +217,9 @@ export class CanvasIO {
|
||||
|
||||
if (maskResp.status === 200) {
|
||||
const data = await resp.json();
|
||||
this.canvas.widget.value = fileName;
|
||||
if (this.canvas.widget) {
|
||||
this.canvas.widget.value = fileName;
|
||||
}
|
||||
log.info(`All files saved successfully, widget value set to: ${fileName}`);
|
||||
resolve(true);
|
||||
} else {
|
||||
@@ -228,6 +243,132 @@ export class CanvasIO {
|
||||
});
|
||||
}
|
||||
|
||||
async _renderOutputData() {
|
||||
return new Promise((resolve) => {
|
||||
const { canvas: tempCanvas, ctx: tempCtx } = createCanvas(this.canvas.width, this.canvas.height);
|
||||
const { canvas: maskCanvas, ctx: maskCtx } = createCanvas(this.canvas.width, this.canvas.height);
|
||||
|
||||
// This logic is mostly mirrored from _performSave to ensure consistency
|
||||
tempCtx.fillStyle = '#ffffff';
|
||||
tempCtx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
||||
const visibilityCanvas = document.createElement('canvas');
|
||||
visibilityCanvas.width = this.canvas.width;
|
||||
visibilityCanvas.height = this.canvas.height;
|
||||
const visibilityCtx = visibilityCanvas.getContext('2d', { alpha: true });
|
||||
maskCtx.fillStyle = '#ffffff'; // Start with a white mask (nothing masked)
|
||||
maskCtx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
||||
|
||||
const sortedLayers = this.canvas.layers.sort((a, b) => a.zIndex - b.zIndex);
|
||||
sortedLayers.forEach((layer) => {
|
||||
// Render layer to main canvas
|
||||
tempCtx.save();
|
||||
tempCtx.globalCompositeOperation = layer.blendMode || 'normal';
|
||||
tempCtx.globalAlpha = layer.opacity !== undefined ? layer.opacity : 1;
|
||||
tempCtx.translate(layer.x + layer.width / 2, layer.y + layer.height / 2);
|
||||
tempCtx.rotate(layer.rotation * Math.PI / 180);
|
||||
tempCtx.drawImage(layer.image, -layer.width / 2, -layer.height / 2, layer.width, layer.height);
|
||||
tempCtx.restore();
|
||||
|
||||
// Render layer to visibility canvas for the mask
|
||||
visibilityCtx.save();
|
||||
visibilityCtx.translate(layer.x + layer.width / 2, layer.y + layer.height / 2);
|
||||
visibilityCtx.rotate(layer.rotation * Math.PI / 180);
|
||||
visibilityCtx.drawImage(layer.image, -layer.width / 2, -layer.height / 2, layer.width, layer.height);
|
||||
visibilityCtx.restore();
|
||||
});
|
||||
|
||||
// Create layer visibility mask
|
||||
const visibilityData = visibilityCtx.getImageData(0, 0, this.canvas.width, this.canvas.height);
|
||||
const maskData = maskCtx.getImageData(0, 0, this.canvas.width, this.canvas.height);
|
||||
for (let i = 0; i < visibilityData.data.length; i += 4) {
|
||||
const alpha = visibilityData.data[i + 3];
|
||||
const maskValue = 255 - alpha; // Invert alpha to create the mask
|
||||
maskData.data[i] = maskData.data[i + 1] = maskData.data[i + 2] = maskValue;
|
||||
maskData.data[i + 3] = 255; // Solid mask
|
||||
}
|
||||
maskCtx.putImageData(maskData, 0, 0);
|
||||
|
||||
// Composite the tool mask on top
|
||||
const toolMaskCanvas = this.canvas.maskTool.getMask();
|
||||
if (toolMaskCanvas) {
|
||||
// Create a temp canvas for processing the mask
|
||||
const tempMaskCanvas = document.createElement('canvas');
|
||||
tempMaskCanvas.width = this.canvas.width;
|
||||
tempMaskCanvas.height = this.canvas.height;
|
||||
const tempMaskCtx = tempMaskCanvas.getContext('2d');
|
||||
|
||||
// Clear the canvas
|
||||
tempMaskCtx.clearRect(0, 0, tempMaskCanvas.width, tempMaskCanvas.height);
|
||||
|
||||
// Calculate the correct position to extract the mask
|
||||
const maskX = this.canvas.maskTool.x;
|
||||
const maskY = this.canvas.maskTool.y;
|
||||
|
||||
log.debug(`[renderOutputData] Extracting mask from world position (${maskX}, ${maskY})`);
|
||||
|
||||
const sourceX = Math.max(0, -maskX);
|
||||
const sourceY = Math.max(0, -maskY);
|
||||
const destX = Math.max(0, maskX);
|
||||
const destY = Math.max(0, maskY);
|
||||
|
||||
const copyWidth = Math.min(toolMaskCanvas.width - sourceX, this.canvas.width - destX);
|
||||
const copyHeight = Math.min(toolMaskCanvas.height - sourceY, this.canvas.height - destY);
|
||||
|
||||
if (copyWidth > 0 && copyHeight > 0) {
|
||||
tempMaskCtx.drawImage(
|
||||
toolMaskCanvas,
|
||||
sourceX, sourceY, copyWidth, copyHeight,
|
||||
destX, destY, copyWidth, copyHeight
|
||||
);
|
||||
}
|
||||
|
||||
// Convert the brush mask (white with alpha) to a solid white mask on black background.
|
||||
const tempMaskData = tempMaskCtx.getImageData(0, 0, this.canvas.width, this.canvas.height);
|
||||
for (let i = 0; i < tempMaskData.data.length; i += 4) {
|
||||
const alpha = tempMaskData.data[i + 3];
|
||||
// The painted area (alpha > 0) should become white (255).
|
||||
tempMaskData.data[i] = tempMaskData.data[i+1] = tempMaskData.data[i+2] = alpha;
|
||||
tempMaskData.data[i + 3] = 255; // Solid alpha
|
||||
}
|
||||
tempMaskCtx.putImageData(tempMaskData, 0, 0);
|
||||
|
||||
// Use 'screen' blending mode. This correctly adds the white brush mask
|
||||
// to the existing layer visibility mask. (white + anything = white)
|
||||
maskCtx.globalCompositeOperation = 'screen';
|
||||
maskCtx.drawImage(tempMaskCanvas, 0, 0);
|
||||
}
|
||||
|
||||
const imageDataUrl = tempCanvas.toDataURL('image/png');
|
||||
const maskDataUrl = maskCanvas.toDataURL('image/png');
|
||||
|
||||
resolve({ image: imageDataUrl, mask: maskDataUrl });
|
||||
});
|
||||
}
|
||||
|
||||
async sendDataViaWebSocket(nodeId) {
|
||||
log.info(`Preparing to send data for node ${nodeId} via WebSocket.`);
|
||||
|
||||
const { image, mask } = await this._renderOutputData();
|
||||
|
||||
try {
|
||||
log.info(`Sending data for node ${nodeId}...`);
|
||||
await webSocketManager.sendMessage({
|
||||
type: 'canvas_data',
|
||||
nodeId: String(nodeId),
|
||||
image: image,
|
||||
mask: mask,
|
||||
}, true); // `true` requires an acknowledgment
|
||||
|
||||
log.info(`Data for node ${nodeId} has been sent and acknowledged by the server.`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
log.error(`Failed to send data for node ${nodeId}:`, error);
|
||||
// We can alert the user here or handle it silently.
|
||||
// For now, let's throw to make it clear the process failed.
|
||||
throw new Error(`Failed to get confirmation from server for node ${nodeId}. The workflow might not have the latest canvas data.`);
|
||||
}
|
||||
}
|
||||
|
||||
async addInputToCanvas(inputImage, inputMask) {
|
||||
try {
|
||||
log.debug("Adding input to canvas:", {inputImage});
|
||||
|
||||
172
js/CanvasView.js
172
js/CanvasView.js
@@ -377,8 +377,7 @@ async function createCanvasWidget(node, widget, app) {
|
||||
const img = new Image();
|
||||
img.onload = async () => {
|
||||
canvas.addLayer(img);
|
||||
await saveWithFallback(widget.value);
|
||||
app.graph.runStep();
|
||||
await updateOutput();
|
||||
};
|
||||
img.src = event.target.result;
|
||||
};
|
||||
@@ -392,8 +391,7 @@ async function createCanvasWidget(node, widget, app) {
|
||||
textContent: "Import Input",
|
||||
onclick: async () => {
|
||||
if (await canvas.importLatestImage()) {
|
||||
await saveWithFallback(widget.value);
|
||||
app.graph.runStep();
|
||||
await updateOutput();
|
||||
}
|
||||
}
|
||||
}),
|
||||
@@ -574,8 +572,7 @@ async function createCanvasWidget(node, widget, app) {
|
||||
canvas.updateSelection([newLayer]);
|
||||
canvas.render();
|
||||
canvas.saveState();
|
||||
await saveWithFallback(widget.value);
|
||||
app.graph.runStep();
|
||||
await updateOutput();
|
||||
} catch (error) {
|
||||
log.error("Matting error:", error);
|
||||
alert(`Error during matting process: ${error.message}`);
|
||||
@@ -745,7 +742,8 @@ async function createCanvasWidget(node, widget, app) {
|
||||
const triggerWidget = node.widgets.find(w => w.name === "trigger");
|
||||
|
||||
const updateOutput = async () => {
|
||||
await saveWithFallback(widget.value);
|
||||
// Only increment trigger and run step - don't save to disk here
|
||||
// Saving to disk will happen during execution_start event
|
||||
triggerWidget.value = (triggerWidget.value + 1) % 99999999;
|
||||
app.graph.runStep();
|
||||
};
|
||||
@@ -790,8 +788,9 @@ async function createCanvasWidget(node, widget, app) {
|
||||
canvas.render();
|
||||
};
|
||||
|
||||
canvas.canvas.addEventListener('mouseup', updateOutput);
|
||||
canvas.canvas.addEventListener('mouseleave', updateOutput);
|
||||
// Remove automatic saving on mouse events - only save during execution
|
||||
// canvas.canvas.addEventListener('mouseup', updateOutput);
|
||||
// canvas.canvas.addEventListener('mouseleave', updateOutput);
|
||||
|
||||
|
||||
const mainContainer = $el("div.painterMainContainer", {
|
||||
@@ -922,66 +921,8 @@ async function createCanvasWidget(node, widget, app) {
|
||||
if (!window.canvasExecutionStates) {
|
||||
window.canvasExecutionStates = new Map();
|
||||
}
|
||||
const saveWithFallback = async (fileName) => {
|
||||
try {
|
||||
const uniqueFileName = generateUniqueFileName(fileName, node.id);
|
||||
log.debug(`Attempting to save with unique name: ${uniqueFileName}`);
|
||||
return await canvas.saveToServer(uniqueFileName);
|
||||
} catch (error) {
|
||||
log.warn(`Failed to save with unique name, falling back to original: ${fileName}`, error);
|
||||
return await canvas.saveToServer(fileName);
|
||||
}
|
||||
};
|
||||
|
||||
api.addEventListener("execution_start", async (event) => {
|
||||
const executionData = event.detail || {};
|
||||
const currentPromptId = executionData.prompt_id;
|
||||
|
||||
log.info(`Execution start event for node ${node.id}, prompt_id: ${currentPromptId}`);
|
||||
log.debug(`Widget value: ${widget.value}`);
|
||||
log.debug(`Node inputs: ${node.inputs?.length || 0}`);
|
||||
log.debug(`Canvas layers count: ${canvas.layers.length}`);
|
||||
if (window.canvasExecutionStates.get(node.id)) {
|
||||
log.warn(`Execution already in progress for node ${node.id}, skipping...`);
|
||||
return;
|
||||
}
|
||||
window.canvasExecutionStates.set(node.id, true);
|
||||
|
||||
try {
|
||||
if (canvas.layers.length === 0) {
|
||||
log.warn(`Node ${node.id} has no layers, skipping save to server`);
|
||||
} else {
|
||||
await saveWithFallback(widget.value);
|
||||
log.info(`Canvas saved to server for node ${node.id}`);
|
||||
}
|
||||
|
||||
if (node.inputs[0]?.link) {
|
||||
const linkId = node.inputs[0].link;
|
||||
const inputData = app.nodeOutputs[linkId];
|
||||
log.debug(`Input link ${linkId} has data: ${!!inputData}`);
|
||||
if (inputData) {
|
||||
imageCache.set(linkId, inputData);
|
||||
log.debug(`Input data cached for link ${linkId}`);
|
||||
}
|
||||
} else {
|
||||
log.debug(`No input link found`);
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(`Error during execution for node ${node.id}:`, error);
|
||||
} finally {
|
||||
window.canvasExecutionStates.set(node.id, false);
|
||||
log.debug(`Execution completed for node ${node.id}, flag released`);
|
||||
}
|
||||
});
|
||||
|
||||
const originalSaveToServer = canvas.saveToServer;
|
||||
canvas.saveToServer = async function (fileName) {
|
||||
log.debug(`saveToServer called with fileName: ${fileName}`);
|
||||
log.debug(`Current execution context - node ID: ${node.id}`);
|
||||
const result = await originalSaveToServer.call(this, fileName);
|
||||
log.debug(`saveToServer completed, result: ${result}`);
|
||||
return result;
|
||||
};
|
||||
|
||||
node.canvasWidget = canvas;
|
||||
|
||||
@@ -996,30 +937,111 @@ async function createCanvasWidget(node, widget, app) {
|
||||
}
|
||||
|
||||
|
||||
const canvasNodeInstances = new Map();
|
||||
|
||||
app.registerExtension({
|
||||
name: "Comfy.CanvasNode",
|
||||
|
||||
init() {
|
||||
// Monkey-patch the queuePrompt function to send canvas data via WebSocket before sending the prompt
|
||||
const originalQueuePrompt = app.queuePrompt;
|
||||
app.queuePrompt = async function(number, prompt) {
|
||||
log.info("Preparing to queue prompt...");
|
||||
|
||||
if (canvasNodeInstances.size > 0) {
|
||||
log.info(`Found ${canvasNodeInstances.size} CanvasNode(s). Sending data via WebSocket...`);
|
||||
|
||||
const sendPromises = [];
|
||||
for (const [nodeId, canvasWidget] of canvasNodeInstances.entries()) {
|
||||
// Ensure the node still exists on the graph before sending data
|
||||
if (app.graph.getNodeById(nodeId) && canvasWidget.canvas && canvasWidget.canvas.canvasIO) {
|
||||
log.debug(`Sending data for canvas node ${nodeId}`);
|
||||
// This now returns a promise that resolves upon server ACK
|
||||
sendPromises.push(canvasWidget.canvas.canvasIO.sendDataViaWebSocket(nodeId));
|
||||
} else {
|
||||
// If node doesn't exist, it might have been deleted, so we can clean up the map
|
||||
log.warn(`Node ${nodeId} not found in graph, removing from instances map.`);
|
||||
canvasNodeInstances.delete(nodeId);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Wait for all WebSocket messages to be acknowledged
|
||||
await Promise.all(sendPromises);
|
||||
log.info("All canvas data has been sent and acknowledged by the server.");
|
||||
} catch (error) {
|
||||
log.error("Failed to send canvas data for one or more nodes. Aborting prompt.", error);
|
||||
// IMPORTANT: Stop the prompt from queueing if data transfer fails.
|
||||
// You might want to show a user-facing error here.
|
||||
alert(`CanvasNode Error: ${error.message}`);
|
||||
return; // Stop execution
|
||||
}
|
||||
}
|
||||
|
||||
log.info("All pre-prompt tasks complete. Proceeding with original queuePrompt.");
|
||||
// Proceed with the original queuePrompt logic
|
||||
return originalQueuePrompt.apply(this, arguments);
|
||||
};
|
||||
},
|
||||
|
||||
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
||||
if (nodeType.comfyClass === "CanvasNode") {
|
||||
const onNodeCreated = nodeType.prototype.onNodeCreated;
|
||||
nodeType.prototype.onNodeCreated = async function () {
|
||||
log.info("CanvasNode created, ID:", this.id);
|
||||
nodeType.prototype.onNodeCreated = function () {
|
||||
log.debug("CanvasNode onNodeCreated: Base widget setup.");
|
||||
// Call original onNodeCreated to ensure widgets are created
|
||||
const r = onNodeCreated?.apply(this, arguments);
|
||||
|
||||
const widget = this.widgets.find(w => w.name === "canvas_image");
|
||||
log.debug("Found canvas_image widget:", widget);
|
||||
await createCanvasWidget(this, widget, app);
|
||||
|
||||
// The main initialization is moved to onAdded
|
||||
return r;
|
||||
};
|
||||
|
||||
// onAdded is the most reliable callback for when a node is fully added to the graph and has an ID
|
||||
nodeType.prototype.onAdded = async function() {
|
||||
log.info(`CanvasNode onAdded, ID: ${this.id}`);
|
||||
log.debug(`Available widgets in onAdded:`, this.widgets.map(w => w.name));
|
||||
|
||||
// Prevent re-initialization if the widget already exists
|
||||
if (this.canvasWidget) {
|
||||
log.warn(`CanvasNode ${this.id} already initialized. Skipping onAdded setup.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Now that we are in onAdded, this.id is guaranteed to be correct.
|
||||
// Set the hidden node_id widget's value for backend communication.
|
||||
const nodeIdWidget = this.widgets.find(w => w.name === "node_id");
|
||||
if (nodeIdWidget) {
|
||||
nodeIdWidget.value = String(this.id);
|
||||
log.debug(`Set hidden node_id widget to: ${nodeIdWidget.value}`);
|
||||
} else {
|
||||
log.error("Could not find the hidden node_id widget!");
|
||||
}
|
||||
|
||||
// Create the main canvas widget and register it in our global map
|
||||
// We pass `null` for the widget parameter as we are not using a pre-defined widget.
|
||||
const canvasWidget = await createCanvasWidget(this, null, app);
|
||||
canvasNodeInstances.set(this.id, canvasWidget);
|
||||
log.info(`Registered CanvasNode instance for ID: ${this.id}`);
|
||||
};
|
||||
|
||||
const onRemoved = nodeType.prototype.onRemoved;
|
||||
nodeType.prototype.onRemoved = function () {
|
||||
log.info(`Cleaning up canvas node ${this.id}`);
|
||||
|
||||
// Clean up from our instance map
|
||||
canvasNodeInstances.delete(this.id);
|
||||
log.info(`Deregistered CanvasNode instance for ID: ${this.id}`);
|
||||
|
||||
// Clean up execution state
|
||||
if (window.canvasExecutionStates) {
|
||||
window.canvasExecutionStates.delete(this.id);
|
||||
}
|
||||
|
||||
const tooltip = document.getElementById(`painter-help-tooltip-${this.id}`);
|
||||
if (tooltip) {
|
||||
tooltip.remove();
|
||||
}
|
||||
const backdrop = document.querySelector('.painter-modal-backdrop');
|
||||
if (backdrop && backdrop.contains(this.canvasWidget.canvas)) {
|
||||
if (backdrop && backdrop.contains(this.canvasWidget?.canvas)) {
|
||||
document.body.removeChild(backdrop);
|
||||
}
|
||||
|
||||
|
||||
@@ -125,12 +125,29 @@ export function cloneLayers(layers) {
|
||||
* @returns {string} Sygnatura JSON
|
||||
*/
|
||||
export function getStateSignature(layers) {
|
||||
return JSON.stringify(layers.map(layer => {
|
||||
const sig = {...layer};
|
||||
if (sig.imageId) {
|
||||
sig.imageId = sig.imageId;
|
||||
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
|
||||
y: Math.round(layer.y * 100) / 100,
|
||||
width: Math.round(layer.width * 100) / 100,
|
||||
height: Math.round(layer.height * 100) / 100,
|
||||
rotation: Math.round((layer.rotation || 0) * 100) / 100,
|
||||
zIndex: layer.zIndex,
|
||||
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;
|
||||
}
|
||||
delete sig.image;
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
return sig;
|
||||
}));
|
||||
}
|
||||
|
||||
160
js/utils/WebSocketManager.js
Normal file
160
js/utils/WebSocketManager.js
Normal file
@@ -0,0 +1,160 @@
|
||||
import {createModuleLogger} from "./LoggerUtils.js";
|
||||
|
||||
const log = createModuleLogger('WebSocketManager');
|
||||
|
||||
class WebSocketManager {
|
||||
constructor(url) {
|
||||
this.url = url;
|
||||
this.socket = null;
|
||||
this.messageQueue = [];
|
||||
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.messageIdCounter = 0;
|
||||
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
|
||||
log.debug("WebSocket is already open.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isConnecting) {
|
||||
log.debug("Connection attempt already in progress.");
|
||||
return;
|
||||
}
|
||||
|
||||
this.isConnecting = true;
|
||||
log.info(`Connecting to WebSocket at ${this.url}...`);
|
||||
|
||||
try {
|
||||
this.socket = new WebSocket(this.url);
|
||||
|
||||
this.socket.onopen = () => {
|
||||
this.isConnecting = false;
|
||||
this.reconnectAttempts = 0;
|
||||
log.info("WebSocket connection established.");
|
||||
this.flushMessageQueue();
|
||||
};
|
||||
|
||||
this.socket.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
log.debug("Received message:", data);
|
||||
|
||||
if (data.type === 'ack' && data.nodeId) {
|
||||
const callback = this.ackCallbacks.get(data.nodeId);
|
||||
if (callback) {
|
||||
log.debug(`ACK received for nodeId: ${data.nodeId}, resolving promise.`);
|
||||
callback.resolve(data);
|
||||
this.ackCallbacks.delete(data.nodeId);
|
||||
}
|
||||
}
|
||||
// Handle other incoming messages if needed
|
||||
} catch (error) {
|
||||
log.error("Error parsing incoming WebSocket message:", error);
|
||||
}
|
||||
};
|
||||
|
||||
this.socket.onclose = (event) => {
|
||||
this.isConnecting = false;
|
||||
if (event.wasClean) {
|
||||
log.info(`WebSocket closed cleanly, code=${event.code}, reason=${event.reason}`);
|
||||
} else {
|
||||
log.warn("WebSocket connection died. Attempting to reconnect...");
|
||||
this.handleReconnect();
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
log.error("Failed to create WebSocket connection:", error);
|
||||
this.handleReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
handleReconnect() {
|
||||
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||
this.reconnectAttempts++;
|
||||
log.info(`Reconnect attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts}...`);
|
||||
setTimeout(() => this.connect(), this.reconnectInterval);
|
||||
} else {
|
||||
log.error("Max reconnect attempts reached. Giving up.");
|
||||
}
|
||||
}
|
||||
|
||||
sendMessage(data, requiresAck = false) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const nodeId = data.nodeId;
|
||||
if (requiresAck && !nodeId) {
|
||||
return reject(new Error("A nodeId is required for messages that need acknowledgment."));
|
||||
}
|
||||
|
||||
const message = JSON.stringify(data);
|
||||
|
||||
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
|
||||
this.socket.send(message);
|
||||
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
|
||||
|
||||
this.ackCallbacks.set(nodeId, {
|
||||
resolve: (responseData) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(responseData);
|
||||
},
|
||||
reject: (error) => {
|
||||
clearTimeout(timeout);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
resolve(); // Resolve immediately if no ACK is needed
|
||||
}
|
||||
} 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."));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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`;
|
||||
export const webSocketManager = new WebSocketManager(wsUrl);
|
||||
Reference in New Issue
Block a user