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:
parent
1e33a465a9
commit
a10a6bafef
|
|
@ -71,8 +71,12 @@ class PopupImageManager {
|
||||||
const { minInterval, maxInterval } = this.periodicSystem;
|
const { minInterval, maxInterval } = this.periodicSystem;
|
||||||
const interval = Math.random() * (maxInterval - minInterval) + minInterval;
|
const interval = Math.random() * (maxInterval - minInterval) + minInterval;
|
||||||
|
|
||||||
this.periodicSystem.interval = setTimeout(() => {
|
this.periodicSystem.interval = setTimeout(async () => {
|
||||||
this.showPeriodicPopup();
|
try {
|
||||||
|
await this.showPeriodicPopup();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error showing periodic popup:', error);
|
||||||
|
}
|
||||||
this.scheduleNextPeriodicPopup();
|
this.scheduleNextPeriodicPopup();
|
||||||
}, interval);
|
}, interval);
|
||||||
|
|
||||||
|
|
@ -82,12 +86,12 @@ class PopupImageManager {
|
||||||
/**
|
/**
|
||||||
* Show a periodic random popup
|
* Show a periodic random popup
|
||||||
*/
|
*/
|
||||||
showPeriodicPopup() {
|
async showPeriodicPopup() {
|
||||||
if (!this.periodicSystem.isActive) {
|
if (!this.periodicSystem.isActive) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = this.getRandomPeriodicImage();
|
const image = await this.getRandomPeriodicImage();
|
||||||
if (!image) {
|
if (!image) {
|
||||||
console.log('⚠️ No images available for periodic popup');
|
console.log('⚠️ No images available for periodic popup');
|
||||||
return;
|
return;
|
||||||
|
|
@ -99,9 +103,9 @@ class PopupImageManager {
|
||||||
/**
|
/**
|
||||||
* Get random image for periodic popups (from linked directories and individual files)
|
* 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
|
// Get images from the same sources as the main library
|
||||||
const allImages = this.getLinkedImages();
|
const allImages = await this.getLinkedImages();
|
||||||
|
|
||||||
if (allImages.length === 0) {
|
if (allImages.length === 0) {
|
||||||
console.log('⚠️ No linked images available for popups');
|
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)
|
* Get all images from linked directories and individual files (same as main library)
|
||||||
*/
|
*/
|
||||||
getLinkedImages() {
|
async getLinkedImages() {
|
||||||
const allImages = [];
|
const allImages = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -185,7 +189,24 @@ class PopupImageManager {
|
||||||
for (const dir of linkedDirs) {
|
for (const dir of linkedDirs) {
|
||||||
try {
|
try {
|
||||||
if (window.electronAPI.readDirectory) {
|
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));
|
const imageFiles = files.filter(file => imageExtensions.test(file.name));
|
||||||
|
|
||||||
imageFiles.forEach(file => {
|
imageFiles.forEach(file => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue