Fraser commited on
Commit
b195c80
Β·
1 Parent(s): dd3fe0a
src/lib/components/AutoTrainerScanner/AutoTrainerScanner.svelte CHANGED
@@ -32,6 +32,12 @@
32
  error: null as string | null
33
  });
34
 
 
 
 
 
 
 
35
  let showDetails = $state(false);
36
  let isInitializing = $state(false);
37
  let shouldStop = $state(false);
@@ -122,13 +128,20 @@
122
  // Fetch remote image
123
  const imageFile = await fetchRemoteImage(nextImage.remoteUrl, nextImage.imagePath);
124
 
 
 
 
 
 
125
  // Queue the image in PicletGenerator
126
  if (picletGenerator) {
127
  picletGenerator.queueTrainerImage(imageFile, nextImage.imagePath);
128
  }
129
 
130
  // Wait for this image to be processed before continuing
131
- // (The onTrainerImageCompleted callback will handle the database update)
 
 
132
 
133
  } catch (error) {
134
  console.error(`Failed to process ${nextImage.imagePath}:`, error);
@@ -136,12 +149,15 @@
136
  nextImage.imagePath,
137
  error instanceof Error ? error.message : 'Unknown error'
138
  );
 
 
 
 
 
 
139
  }
140
 
141
  await updateProgress();
142
-
143
- // Small delay between images
144
- await new Promise(resolve => setTimeout(resolve, 1000));
145
  }
146
  }
147
 
@@ -159,26 +175,32 @@
159
 
160
  async function onTrainerImageCompleted(imagePath: string, picletId: number) {
161
  console.log(`βœ… Trainer image completed: ${imagePath} -> Piclet ID: ${picletId}`);
162
- console.log(`πŸ”§ DEBUG: Marking ${imagePath} as completed in database...`);
163
 
164
  try {
165
  await markImageProcessingCompleted(imagePath, picletId);
166
- console.log(`βœ… DEBUG: Successfully marked ${imagePath} as completed`);
167
  } catch (error) {
168
- console.error(`❌ DEBUG: Failed to mark ${imagePath} as completed:`, error);
169
  }
170
 
171
  await updateProgress();
172
 
173
- // Check what getNextPendingImage returns after this update
174
- const nextImage = await getNextPendingImage();
175
- console.log(`πŸ”§ DEBUG: Next pending image after completion:`, nextImage?.imagePath || 'null');
 
 
176
  }
177
 
178
  async function onTrainerImageFailed(imagePath: string, error: string) {
179
  console.error(`❌ Trainer image failed: ${imagePath} -> ${error}`);
180
  await markImageProcessingFailed(imagePath, error);
181
  await updateProgress();
 
 
 
 
 
 
182
  }
183
 
184
  function formatImageName(imagePath: string | null): string {
 
32
  error: null as string | null
33
  });
34
 
35
+ // Promise tracking for current image processing
36
+ let currentImagePromise: {
37
+ resolve: (value: any) => void;
38
+ reject: (error: any) => void;
39
+ } | null = null;
40
+
41
  let showDetails = $state(false);
42
  let isInitializing = $state(false);
43
  let shouldStop = $state(false);
 
128
  // Fetch remote image
129
  const imageFile = await fetchRemoteImage(nextImage.remoteUrl, nextImage.imagePath);
130
 
131
+ // Create a Promise that will be resolved by the completion callback
132
+ const imageProcessingPromise = new Promise<void>((resolve, reject) => {
133
+ currentImagePromise = { resolve, reject };
134
+ });
135
+
136
  // Queue the image in PicletGenerator
137
  if (picletGenerator) {
138
  picletGenerator.queueTrainerImage(imageFile, nextImage.imagePath);
139
  }
140
 
141
  // Wait for this image to be processed before continuing
142
+ console.log(`πŸ”§ DEBUG: Waiting for completion of ${nextImage.imagePath}...`);
143
+ await imageProcessingPromise;
144
+ console.log(`βœ… DEBUG: Completed processing of ${nextImage.imagePath}, moving to next image`);
145
 
146
  } catch (error) {
147
  console.error(`Failed to process ${nextImage.imagePath}:`, error);
 
149
  nextImage.imagePath,
150
  error instanceof Error ? error.message : 'Unknown error'
151
  );
152
+
153
+ // Reject the current promise if it exists
154
+ if (currentImagePromise) {
155
+ currentImagePromise.reject(error);
156
+ currentImagePromise = null;
157
+ }
158
  }
159
 
160
  await updateProgress();
 
 
 
161
  }
162
  }
163
 
 
175
 
176
  async function onTrainerImageCompleted(imagePath: string, picletId: number) {
177
  console.log(`βœ… Trainer image completed: ${imagePath} -> Piclet ID: ${picletId}`);
 
178
 
179
  try {
180
  await markImageProcessingCompleted(imagePath, picletId);
 
181
  } catch (error) {
182
+ console.error(`❌ Failed to mark ${imagePath} as completed:`, error);
183
  }
184
 
185
  await updateProgress();
186
 
187
+ // Resolve the current image processing promise
188
+ if (currentImagePromise) {
189
+ currentImagePromise.resolve(undefined);
190
+ currentImagePromise = null;
191
+ }
192
  }
193
 
194
  async function onTrainerImageFailed(imagePath: string, error: string) {
195
  console.error(`❌ Trainer image failed: ${imagePath} -> ${error}`);
196
  await markImageProcessingFailed(imagePath, error);
197
  await updateProgress();
198
+
199
+ // Resolve the current image processing promise (failed images should still allow progression)
200
+ if (currentImagePromise) {
201
+ currentImagePromise.resolve(undefined);
202
+ currentImagePromise = null;
203
+ }
204
  }
205
 
206
  function formatImageName(imagePath: string | null): string {
src/lib/db/trainerScanning.ts CHANGED
@@ -116,19 +116,11 @@ export async function markImageProcessingCompleted(
116
  imagePath: string,
117
  picletInstanceId: number
118
  ): Promise<void> {
119
- console.log(`πŸ”§ DB DEBUG: markImageProcessingCompleted called for: ${imagePath}`);
120
-
121
- try {
122
- await updateScanProgress(imagePath, {
123
- status: 'completed',
124
- picletInstanceId,
125
- completedAt: new Date()
126
- });
127
- console.log(`βœ… DB DEBUG: Successfully updated ${imagePath} to completed status`);
128
- } catch (error) {
129
- console.error(`❌ DB DEBUG: Failed to update ${imagePath}:`, error);
130
- throw error;
131
- }
132
  }
133
 
134
  // Mark image processing as failed
 
116
  imagePath: string,
117
  picletInstanceId: number
118
  ): Promise<void> {
119
+ await updateScanProgress(imagePath, {
120
+ status: 'completed',
121
+ picletInstanceId,
122
+ completedAt: new Date()
123
+ });
 
 
 
 
 
 
 
 
124
  }
125
 
126
  // Mark image processing as failed