fix progress issues

This commit is contained in:
2025-11-02 08:33:44 +01:00
parent d5f7b1598f
commit ca4bf72fde
8 changed files with 1217 additions and 322 deletions

View File

@@ -6,6 +6,7 @@ class QueueManager {
constructor() {
this.socket = null;
this.refreshInterval = null;
this.pendingProgressUpdates = new Map(); // Store progress updates waiting for cards
this.init();
}
@@ -78,6 +79,13 @@ class QueueManager {
const serieName = data.serie_name || data.serie || 'Unknown';
const episode = data.episode || '';
this.showToast(`Completed: ${serieName}${episode ? ' - Episode ' + episode : ''}`, 'success');
// Clear any pending progress updates for this download
const downloadId = data.item_id || data.download_id || data.id;
if (downloadId) {
this.pendingProgressUpdates.delete(downloadId);
}
// Full reload needed - item moved from active to completed
this.loadQueueData();
};
@@ -88,6 +96,13 @@ class QueueManager {
const handleDownloadError = (data) => {
const message = data.error || data.message || 'Unknown error';
this.showToast(`Download failed: ${message}`, 'error');
// Clear any pending progress updates for this download
const downloadId = data.item_id || data.download_id || data.id;
if (downloadId) {
this.pendingProgressUpdates.delete(downloadId);
}
// Full reload needed - item moved from active to failed
this.loadQueueData();
};
@@ -217,6 +232,9 @@ class QueueManager {
const data = await response.json();
this.updateQueueDisplay(data);
// Process any pending progress updates after queue is loaded
this.processPendingProgressUpdates();
} catch (error) {
console.error('Error loading queue data:', error);
@@ -286,20 +304,43 @@ class QueueManager {
updateDownloadProgress(data) {
console.log('updateDownloadProgress called with:', JSON.stringify(data, null, 2));
// Extract download ID - handle different data structures
let downloadId = data.id || data.download_id || data.item_id;
// Extract download ID - prioritize metadata.item_id (actual item ID)
// Progress service sends id with "download_" prefix, but we need the actual item ID
let downloadId = null;
// First try metadata.item_id (this is the actual download item ID)
if (data.metadata && data.metadata.item_id) {
downloadId = data.metadata.item_id;
}
// Fallback to other ID fields
if (!downloadId) {
downloadId = data.item_id || data.download_id;
}
// If ID starts with "download_", extract the actual ID
if (!downloadId && data.id) {
if (data.id.startsWith('download_')) {
downloadId = data.id.substring(9); // Remove "download_" prefix
} else {
downloadId = data.id;
}
}
// Check if data is wrapped in another 'data' property
if (!downloadId && data.data) {
downloadId = data.data.id || data.data.download_id || data.data.item_id;
if (data.data.metadata && data.data.metadata.item_id) {
downloadId = data.data.metadata.item_id;
} else if (data.data.item_id) {
downloadId = data.data.item_id;
} else if (data.data.id && data.data.id.startsWith('download_')) {
downloadId = data.data.id.substring(9);
} else {
downloadId = data.data.id || data.data.download_id;
}
data = data.data; // Use nested data
}
// Also try metadata.item_id as fallback
if (!downloadId && data.metadata && data.metadata.item_id) {
downloadId = data.metadata.item_id;
}
if (!downloadId) {
console.warn('No download ID in progress data');
console.warn('Data structure:', data);
@@ -307,15 +348,31 @@ class QueueManager {
return;
}
console.log(`Looking for download card with ID: ${downloadId}`);
// Find the download card in active downloads
const card = document.querySelector(`[data-download-id="${downloadId}"]`);
if (!card) {
// Card not found - might need to reload queue to get new active download
console.log(`Download card not found for ID: ${downloadId}, reloading queue`);
// Card not found - store update and reload queue
console.warn(`Download card not found for ID: ${downloadId}`);
// Debug: Log all existing download cards
const allCards = document.querySelectorAll('[data-download-id]');
console.log(`Found ${allCards.length} download cards:`);
allCards.forEach(c => console.log(` - ${c.getAttribute('data-download-id')}`));
// Store this progress update to retry after queue loads
console.log(`Storing progress update for ${downloadId} to retry after reload`);
this.pendingProgressUpdates.set(downloadId, data);
// Reload queue to sync state
console.log('Reloading queue to sync state...');
this.loadQueueData();
return;
}
console.log(`Found download card for ID: ${downloadId}, updating progress`);
// Extract progress information - handle both ProgressService and yt-dlp formats
const progress = data.progress || data;
const percent = progress.percent || 0;
@@ -364,6 +421,35 @@ class QueueManager {
console.log(`Updated progress for ${downloadId}: ${percent.toFixed(1)}%`);
}
processPendingProgressUpdates() {
if (this.pendingProgressUpdates.size === 0) {
return;
}
console.log(`Processing ${this.pendingProgressUpdates.size} pending progress updates...`);
// Process each pending update
const processed = [];
for (const [downloadId, data] of this.pendingProgressUpdates.entries()) {
// Check if card now exists
const card = document.querySelector(`[data-download-id="${downloadId}"]`);
if (card) {
console.log(`Retrying progress update for ${downloadId}`);
this.updateDownloadProgress(data);
processed.push(downloadId);
} else {
console.log(`Card still not found for ${downloadId}, will retry on next reload`);
}
}
// Remove processed updates
processed.forEach(id => this.pendingProgressUpdates.delete(id));
if (processed.length > 0) {
console.log(`Successfully processed ${processed.length} pending updates`);
}
}
renderActiveDownloads(downloads) {
const container = document.getElementById('active-downloads');