jfrery-zama commited on
Commit
9f3b9e4
·
unverified ·
1 Parent(s): 7fd096c

fix progress bar

Browse files
Files changed (1) hide show
  1. wasm-demo.js +17 -11
wasm-demo.js CHANGED
@@ -310,9 +310,14 @@ async function pollTaskStatus(currentTaskId, currentUid) {
310
  userMessage = 'Processing your request...';
311
  showComputing = true;
312
 
313
- // Progress bar and timer are already started in btnSend.onclick
314
- // Just ensure they're visible
315
  show('progressContainer', true);
 
 
 
 
 
 
316
  } else if (statusData.status === 'success' || statusData.status === 'completed') {
317
  userMessage = 'Processing complete!';
318
  // Set progress to 100% and clear timer
@@ -324,6 +329,7 @@ async function pollTaskStatus(currentTaskId, currentUid) {
324
  } else if (['failure', 'revoked', 'unknown', 'error'].includes(statusData.status.toLowerCase())) {
325
  userMessage = 'Task failed. Please try again.';
326
  // Clear timer on failure
 
327
  if (progressTimer) {
328
  clearInterval(progressTimer);
329
  progressTimer = null;
@@ -342,6 +348,7 @@ async function pollTaskStatus(currentTaskId, currentUid) {
342
  console.error('[Poll] Task failed or unrecoverable:', statusData);
343
  show('spin', false);
344
  show('progressContainer', false);
 
345
  if (progressTimer) {
346
  clearInterval(progressTimer);
347
  progressTimer = null;
@@ -360,6 +367,7 @@ async function pollTaskStatus(currentTaskId, currentUid) {
360
  $('srvStatus').textContent = 'Connection error. Please check your network.';
361
  show('spin', false);
362
  show('progressContainer', false);
 
363
  if (progressTimer) {
364
  clearInterval(progressTimer);
365
  progressTimer = null;
@@ -369,9 +377,9 @@ async function pollTaskStatus(currentTaskId, currentUid) {
369
  }
370
 
371
  function updateProgressBar() {
372
- if (!window.taskStartTime || !window.expectedDuration) return;
373
 
374
- const elapsed = performance.now() - window.taskStartTime;
375
  const progress = Math.min((elapsed / window.expectedDuration) * 100, 95); // Cap at 95% until completion
376
  $('progressBar').style.width = `${progress}%`;
377
  console.log(`[Progress] ${progress.toFixed(1)}% (${(elapsed/1000).toFixed(1)}s / ${(window.expectedDuration/1000).toFixed(1)}s)`);
@@ -403,6 +411,7 @@ async function getTaskResult(currentTaskId, currentUid, taskName) {
403
  show('spin', false);
404
  show('progressContainer', false);
405
  $('srvComputing').hidden = true;
 
406
  if (progressTimer) {
407
  clearInterval(progressTimer);
408
  progressTimer = null;
@@ -423,12 +432,13 @@ $('btnSend').onclick = async () => {
423
  show('encIcon', false);
424
  show('spin', true);
425
  show('processingNote', true);
426
- show('progressContainer', true); // Always show progress bar
427
  $('progressBar').style.width = '0%'; // Reset progress bar
428
  $('srvStatus').textContent = 'Sending encrypted data...';
429
  $('srvComputing').hidden = true; // Ensure it's hidden initially
430
  window.expectedDuration = currentTokenCount * 30 * 1000; // Convert to milliseconds
431
- window.taskStartTime = performance.now(); // Set start time when task is submitted
 
432
 
433
  // Clear any existing progress timer
434
  if (progressTimer) {
@@ -460,11 +470,6 @@ $('btnSend').onclick = async () => {
460
  console.log('[Main] Task submitted to server. Task ID:', taskId);
461
  $('srvStatus').textContent = 'Request submitted. Checking status...';
462
 
463
- // Start progress bar timer immediately
464
- if (!progressTimer) {
465
- progressTimer = setInterval(updateProgressBar, 1000);
466
- }
467
-
468
  pollTaskStatus(taskId, sessionUid).then(finalStatus => {
469
  if (finalStatus && (finalStatus.status === 'success' || finalStatus.status === 'completed')) {
470
  getTaskResult(taskId, sessionUid, 'synthid');
@@ -478,6 +483,7 @@ $('btnSend').onclick = async () => {
478
  show('spin', false);
479
  show('progressContainer', false);
480
  $('srvComputing').hidden = true;
 
481
  if (progressTimer) {
482
  clearInterval(progressTimer);
483
  progressTimer = null;
 
310
  userMessage = 'Processing your request...';
311
  showComputing = true;
312
 
313
+ // Start progress bar when processing actually begins
 
314
  show('progressContainer', true);
315
+ if (!window.processingStartTime) {
316
+ window.processingStartTime = performance.now(); // Set processing start time
317
+ }
318
+ if (!progressTimer) {
319
+ progressTimer = setInterval(updateProgressBar, 1000);
320
+ }
321
  } else if (statusData.status === 'success' || statusData.status === 'completed') {
322
  userMessage = 'Processing complete!';
323
  // Set progress to 100% and clear timer
 
329
  } else if (['failure', 'revoked', 'unknown', 'error'].includes(statusData.status.toLowerCase())) {
330
  userMessage = 'Task failed. Please try again.';
331
  // Clear timer on failure
332
+ window.processingStartTime = null;
333
  if (progressTimer) {
334
  clearInterval(progressTimer);
335
  progressTimer = null;
 
348
  console.error('[Poll] Task failed or unrecoverable:', statusData);
349
  show('spin', false);
350
  show('progressContainer', false);
351
+ window.processingStartTime = null;
352
  if (progressTimer) {
353
  clearInterval(progressTimer);
354
  progressTimer = null;
 
367
  $('srvStatus').textContent = 'Connection error. Please check your network.';
368
  show('spin', false);
369
  show('progressContainer', false);
370
+ window.processingStartTime = null;
371
  if (progressTimer) {
372
  clearInterval(progressTimer);
373
  progressTimer = null;
 
377
  }
378
 
379
  function updateProgressBar() {
380
+ if (!window.processingStartTime || !window.expectedDuration) return;
381
 
382
+ const elapsed = performance.now() - window.processingStartTime;
383
  const progress = Math.min((elapsed / window.expectedDuration) * 100, 95); // Cap at 95% until completion
384
  $('progressBar').style.width = `${progress}%`;
385
  console.log(`[Progress] ${progress.toFixed(1)}% (${(elapsed/1000).toFixed(1)}s / ${(window.expectedDuration/1000).toFixed(1)}s)`);
 
411
  show('spin', false);
412
  show('progressContainer', false);
413
  $('srvComputing').hidden = true;
414
+ window.processingStartTime = null;
415
  if (progressTimer) {
416
  clearInterval(progressTimer);
417
  progressTimer = null;
 
432
  show('encIcon', false);
433
  show('spin', true);
434
  show('processingNote', true);
435
+ show('progressContainer', false); // Hide initially, show when processing starts
436
  $('progressBar').style.width = '0%'; // Reset progress bar
437
  $('srvStatus').textContent = 'Sending encrypted data...';
438
  $('srvComputing').hidden = true; // Ensure it's hidden initially
439
  window.expectedDuration = currentTokenCount * 30 * 1000; // Convert to milliseconds
440
+ window.taskStartTime = performance.now(); // Set start time when task is submitted (for overall duration)
441
+ window.processingStartTime = null; // Will be set when processing actually begins
442
 
443
  // Clear any existing progress timer
444
  if (progressTimer) {
 
470
  console.log('[Main] Task submitted to server. Task ID:', taskId);
471
  $('srvStatus').textContent = 'Request submitted. Checking status...';
472
 
 
 
 
 
 
473
  pollTaskStatus(taskId, sessionUid).then(finalStatus => {
474
  if (finalStatus && (finalStatus.status === 'success' || finalStatus.status === 'completed')) {
475
  getTaskResult(taskId, sessionUid, 'synthid');
 
483
  show('spin', false);
484
  show('progressContainer', false);
485
  $('srvComputing').hidden = true;
486
+ window.processingStartTime = null; // Reset processing start time
487
  if (progressTimer) {
488
  clearInterval(progressTimer);
489
  progressTimer = null;