diff --git a/js/CanvasIO.js b/js/CanvasIO.js index 833e480..75f58f7 100644 --- a/js/CanvasIO.js +++ b/js/CanvasIO.js @@ -197,16 +197,23 @@ export class CanvasIO { } async _renderOutputData() { log.info("=== RENDERING OUTPUT DATA FOR COMFYUI ==="); - // Check if layers have valid images loaded - const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (layersWithoutImages.length > 0) { - log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); - // Wait a bit for images to load - await new Promise(resolve => setTimeout(resolve, 100)); - // Check again - const stillIncomplete = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (stillIncomplete.length > 0) { - throw new Error(`Canvas not ready: ${stillIncomplete.length} layer(s) still have incomplete image data. Try clicking on a layer to force initialization, or wait a moment and try again.`); + // Check if layers have valid images loaded, with retry logic + const maxRetries = 5; + const retryDelay = 200; + for (let attempt = 0; attempt < maxRetries; attempt++) { + const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); + if (layersWithoutImages.length === 0) { + break; // All images loaded + } + if (attempt === 0) { + log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); + } + if (attempt < maxRetries - 1) { + await new Promise(resolve => setTimeout(resolve, retryDelay)); + } + else { + // Last attempt failed + throw new Error(`Canvas not ready after ${maxRetries} attempts: ${layersWithoutImages.length} layer(s) still have incomplete image data. Try waiting a moment and running again.`); } } // Użyj zunifikowanych funkcji z CanvasLayers diff --git a/src/CanvasIO.ts b/src/CanvasIO.ts index c7708a8..3212b0e 100644 --- a/src/CanvasIO.ts +++ b/src/CanvasIO.ts @@ -218,17 +218,26 @@ export class CanvasIO { async _renderOutputData(): Promise<{ image: string, mask: string }> { log.info("=== RENDERING OUTPUT DATA FOR COMFYUI ==="); - // Check if layers have valid images loaded - const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (layersWithoutImages.length > 0) { - log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); - // Wait a bit for images to load - await new Promise(resolve => setTimeout(resolve, 100)); + // Check if layers have valid images loaded, with retry logic + const maxRetries = 5; + const retryDelay = 200; - // Check again - const stillIncomplete = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (stillIncomplete.length > 0) { - throw new Error(`Canvas not ready: ${stillIncomplete.length} layer(s) still have incomplete image data. Try clicking on a layer to force initialization, or wait a moment and try again.`); + for (let attempt = 0; attempt < maxRetries; attempt++) { + const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); + + if (layersWithoutImages.length === 0) { + break; // All images loaded + } + + if (attempt === 0) { + log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); + } + + if (attempt < maxRetries - 1) { + await new Promise(resolve => setTimeout(resolve, retryDelay)); + } else { + // Last attempt failed + throw new Error(`Canvas not ready after ${maxRetries} attempts: ${layersWithoutImages.length} layer(s) still have incomplete image data. Try waiting a moment and running again.`); } }