From c0063a871fd891a1774f1bee00e86170fed249fa Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 2 Jul 2024 15:33:09 -0400 Subject: [PATCH] fix: handling of batch requests with zero files --- .../backend/src/routers/filesystem_api/batch/all.js | 8 +++++--- packages/backend/src/util/fnutil.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/routers/filesystem_api/batch/all.js b/packages/backend/src/routers/filesystem_api/batch/all.js index 5b30d691..6e92ab08 100644 --- a/packages/backend/src/routers/filesystem_api/batch/all.js +++ b/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; diff --git a/packages/backend/src/util/fnutil.js b/packages/backend/src/util/fnutil.js index ffcfef98..d444ca05 100644 --- a/packages/backend/src/util/fnutil.js +++ b/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, };