Fix audio deletion async handling and add debugging

Fixes for audio deletion functionality:

1. Async Handling Fix:
   - Made deleteSelectedAudio() async function
   - Replaced forEach with Promise.all() for proper async handling
   - deletedCount was not updating correctly due to async timing issues
   - Now waits for all deletion promises to complete before showing results

2. Enhanced Error Handling:
   - Added try/catch around Promise.all()
   - Better error messages for failed deletions
   - Separate handling for no deletions vs errors

3. Debug Logging:
   - Added console logs to track audio file lookup process
   - Logs storage contents and search parameters
   - Helps identify why files aren't being found for deletion
   - Shows actual audioFile object found in storage

4. Improved User Feedback:
   - Shows specific count of successful deletions
   - Warning when no files are deleted
   - Error notification for deletion failures

This should resolve the deletion timing issues and provide better insight into any remaining problems.
This commit is contained in:
fritzsenpai 2025-09-25 21:36:25 -05:00
parent b4709e4f66
commit 663bc9c489
1 changed files with 29 additions and 8 deletions

37
game.js
View File

@ -2045,7 +2045,7 @@ ${usagePercent > 85 ? '⚠️ Storage getting full - consider deleting some imag
}
}
deleteSelectedAudio() {
async deleteSelectedAudio() {
const activeGallery = document.querySelector('.audio-gallery.active');
if (!activeGallery) return;
@ -2062,7 +2062,8 @@ ${usagePercent > 85 ? '⚠️ Storage getting full - consider deleting some imag
let deletedCount = 0;
const isDesktop = window.electronAPI !== undefined;
selectedItems.forEach(async (item) => {
// Use Promise.all to handle async operations properly
const deletionPromises = Array.from(selectedItems).map(async (item) => {
const category = item.dataset.category;
const filename = item.dataset.filename;
@ -2070,33 +2071,53 @@ ${usagePercent > 85 ? '⚠️ Storage getting full - consider deleting some imag
// Desktop mode - delete actual file
// Need to find the full audio file path from storage
const customAudio = this.dataManager.get('customAudio') || { background: [], ambient: [], effects: [] };
console.log(`Looking for audio file in category ${category}:`, customAudio[category]);
console.log(`Searching for filename: ${filename}`);
const audioFile = customAudio[category].find(audio =>
(typeof audio === 'string' && audio.includes(filename)) ||
(typeof audio === 'object' && (audio.filename === filename || audio.name === filename))
);
console.log(`Found audio file:`, audioFile);
if (audioFile) {
const audioPath = typeof audioFile === 'string' ? audioFile : audioFile.path;
console.log(`Attempting to delete audio file: ${audioPath}`);
const success = await this.fileManager.deleteAudio(audioPath, category);
if (success) {
deletedCount++;
console.log(`Successfully deleted: ${filename}`);
return true;
} else {
console.error(`Failed to delete audio file: ${filename}`);
return false;
}
} else {
console.error(`Audio file not found in storage: ${filename}`);
return false;
}
} else {
// Web mode - remove from storage only
this.removeAudioFromStorage(category, filename);
deletedCount++;
return true;
}
});
if (deletedCount > 0) {
const modeText = isDesktop ? 'file(s) deleted from disk' : 'reference(s) removed from storage';
this.showNotification(`${deletedCount} audio ${modeText}`, 'success');
this.loadAudioGallery(); // Refresh the gallery
// Wait for all deletions to complete
try {
const results = await Promise.all(deletionPromises);
deletedCount = results.filter(result => result === true).length;
if (deletedCount > 0) {
const modeText = isDesktop ? 'file(s) deleted from disk' : 'reference(s) removed from storage';
this.showNotification(`${deletedCount} audio ${modeText}`, 'success');
this.loadAudioGallery(); // Refresh the gallery
} else {
this.showNotification('No audio files were deleted', 'warning');
}
} catch (error) {
console.error('Error during audio deletion:', error);
this.showNotification('Error occurred during deletion', 'error');
}
}