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
This commit is contained in:
dilgenfritz 2025-11-03 19:24:59 -06:00
parent 1e33a465a9
commit a10a6bafef
1 changed files with 29 additions and 8 deletions

View File

@ -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 => {