From a10a6bafef71363d7a879b7dd27cfa9e845e177b Mon Sep 17 00:00:00 2001 From: dilgenfritz Date: Mon, 3 Nov 2025 19:24:59 -0600 Subject: [PATCH] Fix popup image manager async handling error FIXED ERROR: 'files.filter is not a function' in popupImageManager.js ISSUE: - getLinkedImages() was calling electronAPI.readDirectory() without handling async/sync results - readDirectory() can return Promise or Array depending on implementation - Code assumed synchronous array result, causing TypeError when Promise returned SOLUTION: - Updated getLinkedImages() to handle both sync and async results from readDirectory() - Made getLinkedImages(), getRandomPeriodicImage(), and showPeriodicPopup() async - Added proper error handling for async directory reading - Matches the pattern used successfully in game.js RESULT: Popup images should now load properly from linked directories without errors --- src/features/images/popupImageManager.js | 37 +++++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/features/images/popupImageManager.js b/src/features/images/popupImageManager.js index 45d64db..980e52d 100644 --- a/src/features/images/popupImageManager.js +++ b/src/features/images/popupImageManager.js @@ -71,8 +71,12 @@ class PopupImageManager { const { minInterval, maxInterval } = this.periodicSystem; const interval = Math.random() * (maxInterval - minInterval) + minInterval; - this.periodicSystem.interval = setTimeout(() => { - this.showPeriodicPopup(); + this.periodicSystem.interval = setTimeout(async () => { + try { + await this.showPeriodicPopup(); + } catch (error) { + console.error('Error showing periodic popup:', error); + } this.scheduleNextPeriodicPopup(); }, interval); @@ -82,12 +86,12 @@ class PopupImageManager { /** * Show a periodic random popup */ - showPeriodicPopup() { + async showPeriodicPopup() { if (!this.periodicSystem.isActive) { return; } - const image = this.getRandomPeriodicImage(); + const image = await this.getRandomPeriodicImage(); if (!image) { console.log('⚠️ No images available for periodic popup'); return; @@ -99,9 +103,9 @@ class PopupImageManager { /** * Get random image for periodic popups (from linked directories and individual files) */ - getRandomPeriodicImage() { + async getRandomPeriodicImage() { // Get images from the same sources as the main library - const allImages = this.getLinkedImages(); + const allImages = await this.getLinkedImages(); if (allImages.length === 0) { console.log('⚠️ No linked images available for popups'); @@ -150,7 +154,7 @@ class PopupImageManager { /** * Get all images from linked directories and individual files (same as main library) */ - getLinkedImages() { + async getLinkedImages() { const allImages = []; try { @@ -185,7 +189,24 @@ class PopupImageManager { for (const dir of linkedDirs) { try { if (window.electronAPI.readDirectory) { - const files = window.electronAPI.readDirectory(dir.path); + const result = window.electronAPI.readDirectory(dir.path); + + let files = []; + // Handle both sync and async results + if (result && typeof result.then === 'function') { + try { + files = await result; + } catch (asyncError) { + console.error(`Error loading images from directory ${dir.path}:`, asyncError); + continue; + } + } else if (Array.isArray(result)) { + files = result; + } else { + console.warn(`Unexpected result type from readDirectory for ${dir.path}:`, typeof result); + continue; + } + const imageFiles = files.filter(file => imageExtensions.test(file.name)); imageFiles.forEach(file => {