Browse Source

fix: handling of batch requests with zero files

KernelDeimos 10 months ago
parent
commit
c0063a871f

+ 5 - 3
packages/backend/src/routers/filesystem_api/batch/all.js

@@ -28,6 +28,7 @@ const { TeePromise } = require("../../../util/promise");
 const { EWMA, MovingMode } = require("../../../util/opmath");
 const { get_app } = require('../../../helpers');
 const { valid_file_size } = require("../../../util/validutil");
+const { OnlyOnceFn } = require("../../../util/fnutil.js");
 
 const commands = require('../../../filesystem/batch/commands.js').commands;
 
@@ -137,7 +138,7 @@ module.exports = eggspress('/batch', {
     let total = 0;
     let total_tbd = true;
 
-    const on_first_file = () => {
+    const on_nonfile_data_end = OnlyOnceFn(() => {
         if ( request_error ) {
             return;
         }
@@ -160,7 +161,7 @@ module.exports = eggspress('/batch', {
             pending_operations.splice(index, 1)[0];
             response_promises.splice(index, 1);
         }
-    }
+    });
 
 
     //-------------------------------------------------------------
@@ -228,7 +229,7 @@ module.exports = eggspress('/batch', {
         if ( batch_exe.total_tbd ) {
             batch_exe.total_tbd = false;
             batch_widget.ic = pending_operations.length;
-            on_first_file();
+            on_nonfile_data_end();
         }
 
         if ( fileinfos.length == 0 ) {
@@ -276,6 +277,7 @@ module.exports = eggspress('/batch', {
     // Awaiting responses
     //-------------------------------------------------------------
     await still_reading;
+    on_nonfile_data_end();
 
     if ( request_error ) {
         return;

+ 10 - 0
packages/backend/src/util/fnutil.js

@@ -9,6 +9,16 @@ const UtilFn = fn => {
     return fn;
 };
 
+const OnlyOnceFn = fn => {
+    let called = false;
+    return function onlyoncefn_call (...args) {
+        if ( called ) return;
+        called = true;
+        return fn(...args);
+    };
+};
+
 module.exports = {
     UtilFn,
+    OnlyOnceFn,
 };