Plugin Directory

Changeset 3340745


Ignore:
Timestamp:
08/07/2025 05:43:04 AM (6 months ago)
Author:
neroai
Message:

fix an issue

Location:
nero-ai-image-compressor
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nero-ai-image-compressor/tags/1.0.0/assets/js/admin.js

    r3340230 r3340745  
    144144
    145145          function pollTasks() {
    146             if (taskIdList.length > 0) {
    147               fetch(
    148                 neroaiic_ajax.ajax_url + "?action=neroaiic_batch_query_tasks",
    149                 {
    150                   method: "POST",
    151                   body: new URLSearchParams({
    152                     task_ids: taskIdList.join(","),
    153                     neroaiic_nonce: neroaiic_ajax.nonce,
    154                   }),
    155                   credentials: "same-origin",
    156                 }
    157               )
    158                 .then((r) => r.json())
    159                 .then(function (data) {
    160                   if (
    161                     data.success &&
    162                     data.data &&
    163                     data.data.data &&
    164                     Array.isArray(data.data.data.tasks)
    165                   ) {
    166                     var tasks = data.data.data.tasks;
    167                     var completedTaskIds = [];
    168 
    169                     tasks.forEach(function (task) {
    170                       var tid = task.task_id;
    171                       if (!tid || !pendingTasks[tid]) {
    172                         return;
     146            if (taskIdList.length === 0) {
     147              if (!allDone) {
     148                setTimeout(pollTasks, 1000); // Wait 1 second if no tasks
     149              }
     150              return;
     151            }
     152
     153            const CHUNK_SIZE = 10;
     154            // Take the next chunk of tasks to process
     155            const taskChunk = taskIdList.slice(0, CHUNK_SIZE);
     156
     157            fetch(
     158              neroaiic_ajax.ajax_url + "?action=neroaiic_batch_query_tasks",
     159              {
     160                method: "POST",
     161                body: new URLSearchParams({
     162                  task_ids: taskChunk.join(","),
     163                  neroaiic_nonce: neroaiic_ajax.nonce,
     164                }),
     165                credentials: "same-origin",
     166              }
     167            )
     168              .then((r) => r.json())
     169              .then((data) => {
     170                let completedTaskIds = [];
     171                let innerPromises = [];
     172
     173                if (
     174                  data.success &&
     175                  data.data &&
     176                  data.data.data &&
     177                  Array.isArray(data.data.data.tasks)
     178                ) {
     179                  const tasks = data.data.data.tasks;
     180                  tasks.forEach((task) => {
     181                    const tid = task.task_id;
     182                    if (!tid || !pendingTasks[tid]) {
     183                      return;
     184                    }
     185
     186                    if (task.status === "done" || task.status === "failed") {
     187                      const file = pendingTasks[tid].file;
     188                      if (task.status === "done") {
     189                        if (task.result && task.result.output) {
     190                          const p = fetch(
     191                            neroaiic_ajax.ajax_url +
     192                              "?action=neroaiic_save_output",
     193                            {
     194                              method: "POST",
     195                              body: new URLSearchParams({
     196                                file: file,
     197                                output: task.result.output,
     198                                neroaiic_nonce: neroaiic_ajax.nonce,
     199                              }),
     200                              credentials: "same-origin",
     201                            }
     202                          )
     203                            .then((r) => r.json())
     204                            .then((saveData) => {
     205                              results.push({
     206                                file: file,
     207                                result: saveData.data.result,
     208                                original_size: saveData.data.original_size,
     209                                compressed_size: saveData.data.compressed_size,
     210                                ratio: saveData.data.ratio,
     211                              });
     212                            });
     213                          innerPromises.push(p);
     214                        } else {
     215                          results.push({ file: file, result: "No output" });
     216                        }
     217                      } else {
     218                        results.push({ file: file, result: "Failed" });
    173219                      }
    174 
    175                       if (task.status === "done" || task.status === "failed") {
    176                         var file = pendingTasks[tid].file;
    177                         if (task.status === "done") {
    178                           if (task.result && task.result.output) {
    179                             fetch(
    180                               neroaiic_ajax.ajax_url +
    181                                 "?action=neroaiic_save_output",
    182                               {
    183                                 method: "POST",
    184                                 body: new URLSearchParams({
    185                                   file: file,
    186                                   output: task.result.output,
    187                                   neroaiic_nonce: neroaiic_ajax.nonce,
    188                                 }),
    189                                 credentials: "same-origin",
    190                               }
    191                             )
    192                               .then((r) => r.json())
    193                               .then(function (saveData) {
    194                                 results.push({
    195                                   file: file,
    196                                   result: saveData.data.result,
    197                                   original_size: saveData.data.original_size,
    198                                   compressed_size:
    199                                     saveData.data.compressed_size,
    200                                   ratio: saveData.data.ratio,
    201                                 });
    202                                 updateProgress();
    203                               });
    204                           } else {
    205                             results.push({ file: file, result: "No output" });
    206                             updateProgress();
    207                           }
    208                         } else {
    209                           // failed
    210                           results.push({ file: file, result: "Failed" });
    211                           updateProgress();
    212                         }
    213 
    214                         delete pendingTasks[tid];
    215                         completedTaskIds.push(tid);
    216                       }
    217                     });
    218 
    219                     if (completedTaskIds.length > 0) {
    220                       taskIdList = taskIdList.filter(function (tid) {
    221                         return completedTaskIds.indexOf(tid) === -1;
    222                       });
     220                      delete pendingTasks[tid];
     221                      completedTaskIds.push(tid);
    223222                    }
    224                     updateProgress();
     223                  });
     224                }
     225
     226                return Promise.all(innerPromises).then(() => {
     227                  if (completedTaskIds.length > 0) {
     228                    // Remove completed tasks from the main list
     229                    taskIdList = taskIdList.filter(
     230                      (tid) => completedTaskIds.indexOf(tid) === -1
     231                    );
    225232                  }
    226 
    227                   if (!allDone) setTimeout(pollTasks, 2000);
    228233                });
    229             } else {
    230               if (!allDone) setTimeout(pollTasks, 2000);
    231             }
     234              })
     235              .catch((error) => {
     236                console.error("Polling error:", error);
     237              })
     238              .finally(() => {
     239                updateProgress();
     240                // If there are still tasks, poll again quickly. Otherwise, wait.
     241                const nextPollTime = taskIdList.length > 0 ? 100 : 1000;
     242                if (!allDone) {
     243                  setTimeout(pollTasks, nextPollTime);
     244                }
     245              });
    232246          }
    233247
  • nero-ai-image-compressor/trunk/assets/js/admin.js

    r3340230 r3340745  
    144144
    145145          function pollTasks() {
    146             if (taskIdList.length > 0) {
    147               fetch(
    148                 neroaiic_ajax.ajax_url + "?action=neroaiic_batch_query_tasks",
    149                 {
    150                   method: "POST",
    151                   body: new URLSearchParams({
    152                     task_ids: taskIdList.join(","),
    153                     neroaiic_nonce: neroaiic_ajax.nonce,
    154                   }),
    155                   credentials: "same-origin",
    156                 }
    157               )
    158                 .then((r) => r.json())
    159                 .then(function (data) {
    160                   if (
    161                     data.success &&
    162                     data.data &&
    163                     data.data.data &&
    164                     Array.isArray(data.data.data.tasks)
    165                   ) {
    166                     var tasks = data.data.data.tasks;
    167                     var completedTaskIds = [];
    168 
    169                     tasks.forEach(function (task) {
    170                       var tid = task.task_id;
    171                       if (!tid || !pendingTasks[tid]) {
    172                         return;
     146            if (taskIdList.length === 0) {
     147              if (!allDone) {
     148                setTimeout(pollTasks, 1000); // Wait 1 second if no tasks
     149              }
     150              return;
     151            }
     152
     153            const CHUNK_SIZE = 10;
     154            // Take the next chunk of tasks to process
     155            const taskChunk = taskIdList.slice(0, CHUNK_SIZE);
     156
     157            fetch(
     158              neroaiic_ajax.ajax_url + "?action=neroaiic_batch_query_tasks",
     159              {
     160                method: "POST",
     161                body: new URLSearchParams({
     162                  task_ids: taskChunk.join(","),
     163                  neroaiic_nonce: neroaiic_ajax.nonce,
     164                }),
     165                credentials: "same-origin",
     166              }
     167            )
     168              .then((r) => r.json())
     169              .then((data) => {
     170                let completedTaskIds = [];
     171                let innerPromises = [];
     172
     173                if (
     174                  data.success &&
     175                  data.data &&
     176                  data.data.data &&
     177                  Array.isArray(data.data.data.tasks)
     178                ) {
     179                  const tasks = data.data.data.tasks;
     180                  tasks.forEach((task) => {
     181                    const tid = task.task_id;
     182                    if (!tid || !pendingTasks[tid]) {
     183                      return;
     184                    }
     185
     186                    if (task.status === "done" || task.status === "failed") {
     187                      const file = pendingTasks[tid].file;
     188                      if (task.status === "done") {
     189                        if (task.result && task.result.output) {
     190                          const p = fetch(
     191                            neroaiic_ajax.ajax_url +
     192                              "?action=neroaiic_save_output",
     193                            {
     194                              method: "POST",
     195                              body: new URLSearchParams({
     196                                file: file,
     197                                output: task.result.output,
     198                                neroaiic_nonce: neroaiic_ajax.nonce,
     199                              }),
     200                              credentials: "same-origin",
     201                            }
     202                          )
     203                            .then((r) => r.json())
     204                            .then((saveData) => {
     205                              results.push({
     206                                file: file,
     207                                result: saveData.data.result,
     208                                original_size: saveData.data.original_size,
     209                                compressed_size: saveData.data.compressed_size,
     210                                ratio: saveData.data.ratio,
     211                              });
     212                            });
     213                          innerPromises.push(p);
     214                        } else {
     215                          results.push({ file: file, result: "No output" });
     216                        }
     217                      } else {
     218                        results.push({ file: file, result: "Failed" });
    173219                      }
    174 
    175                       if (task.status === "done" || task.status === "failed") {
    176                         var file = pendingTasks[tid].file;
    177                         if (task.status === "done") {
    178                           if (task.result && task.result.output) {
    179                             fetch(
    180                               neroaiic_ajax.ajax_url +
    181                                 "?action=neroaiic_save_output",
    182                               {
    183                                 method: "POST",
    184                                 body: new URLSearchParams({
    185                                   file: file,
    186                                   output: task.result.output,
    187                                   neroaiic_nonce: neroaiic_ajax.nonce,
    188                                 }),
    189                                 credentials: "same-origin",
    190                               }
    191                             )
    192                               .then((r) => r.json())
    193                               .then(function (saveData) {
    194                                 results.push({
    195                                   file: file,
    196                                   result: saveData.data.result,
    197                                   original_size: saveData.data.original_size,
    198                                   compressed_size:
    199                                     saveData.data.compressed_size,
    200                                   ratio: saveData.data.ratio,
    201                                 });
    202                                 updateProgress();
    203                               });
    204                           } else {
    205                             results.push({ file: file, result: "No output" });
    206                             updateProgress();
    207                           }
    208                         } else {
    209                           // failed
    210                           results.push({ file: file, result: "Failed" });
    211                           updateProgress();
    212                         }
    213 
    214                         delete pendingTasks[tid];
    215                         completedTaskIds.push(tid);
    216                       }
    217                     });
    218 
    219                     if (completedTaskIds.length > 0) {
    220                       taskIdList = taskIdList.filter(function (tid) {
    221                         return completedTaskIds.indexOf(tid) === -1;
    222                       });
     220                      delete pendingTasks[tid];
     221                      completedTaskIds.push(tid);
    223222                    }
    224                     updateProgress();
     223                  });
     224                }
     225
     226                return Promise.all(innerPromises).then(() => {
     227                  if (completedTaskIds.length > 0) {
     228                    // Remove completed tasks from the main list
     229                    taskIdList = taskIdList.filter(
     230                      (tid) => completedTaskIds.indexOf(tid) === -1
     231                    );
    225232                  }
    226 
    227                   if (!allDone) setTimeout(pollTasks, 2000);
    228233                });
    229             } else {
    230               if (!allDone) setTimeout(pollTasks, 2000);
    231             }
     234              })
     235              .catch((error) => {
     236                console.error("Polling error:", error);
     237              })
     238              .finally(() => {
     239                updateProgress();
     240                // If there are still tasks, poll again quickly. Otherwise, wait.
     241                const nextPollTime = taskIdList.length > 0 ? 100 : 1000;
     242                if (!allDone) {
     243                  setTimeout(pollTasks, nextPollTime);
     244                }
     245              });
    232246          }
    233247
Note: See TracChangeset for help on using the changeset viewer.