From 2b77590402b171a6784f79aa01aa4a060206b9d5 Mon Sep 17 00:00:00 2001 From: GogoVega <92022724+GogoVega@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:54:42 +0100 Subject: [PATCH 1/2] Add a queue while installing or removing a module --- .../editor-client/src/js/ui/palette-editor.js | 81 ++++++++++++++----- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js b/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js index 3d949f8ca..9f7f964c3 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js @@ -115,8 +115,63 @@ RED.palette.editor = (function() { }); }) } - function installNodeModule(id,version,url,callback) { - var requestBody = { + + const moduleQueue = []; + const processQueue = function () { + if (moduleQueue.length === 0) { + return; + } + + const { type, body, callback } = moduleQueue[0]; + if (type === "install") { + $.ajax({ + url: "nodes", + type: "POST", + data: JSON.stringify(body), + contentType: "application/json; charset=utf-8" + }).done(function(_data, _textStatus, _xhr) { + callback(); + }).fail(function(xhr, textStatus, err) { + callback(xhr, textStatus, err); + }).always(function () { + // Remove the task from the list + moduleQueue.shift(); + // Process the next task + processQueue(); + }); + } else if (type === "remove") { + $.ajax({ + url: "nodes/" + body.id, + type: "DELETE" + }).done(function(_data, _textStatus, _xhr) { + callback(); + }).fail(function(xhr, _textStatus, _err) { + callback(xhr); + }).always(function () { + // Remove the task from the list + moduleQueue.shift(); + // Process the next task + processQueue(); + }); + } + }; + + /** + * Adds a module to the processing queue to install or remove it + * @param {string} type the type of request to apply to the module + * @param {Object} body an object with module info + * @param {(xhr?: JQuery.jqXHR, textStatus?: JQuery.Ajax.ErrorTextStatus, err?: string) => void} callback a callback function called when the request is done + */ + function addModuleToQueue(type, body, callback) { + moduleQueue.push({ type, body, callback }); + + if (moduleQueue.length === 1) { + processQueue(); + } + } + + function installNodeModule(id, version, url, callback) { + const requestBody = { module: id }; if (version) { @@ -125,26 +180,10 @@ RED.palette.editor = (function() { if (url) { requestBody.url = url; } - $.ajax({ - url:"nodes", - type: "POST", - data: JSON.stringify(requestBody), - contentType: "application/json; charset=utf-8" - }).done(function(data,textStatus,xhr) { - callback(); - }).fail(function(xhr,textStatus,err) { - callback(xhr,textStatus,err); - }); + addModuleToQueue("install", requestBody, callback); } - function removeNodeModule(id,callback) { - $.ajax({ - url:"nodes/"+id, - type: "DELETE" - }).done(function(data,textStatus,xhr) { - callback(); - }).fail(function(xhr,textStatus,err) { - callback(xhr); - }) + function removeNodeModule(id, callback) { + addModuleToQueue("remove", { id: id }, callback); } function refreshNodeModuleList() { From 2f2162d5312f7e99bc9ca844bb39539534ed09ec Mon Sep 17 00:00:00 2001 From: GogoVega <92022724+GogoVega@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:10:03 +0100 Subject: [PATCH 2/2] `startEvent` must be called in the processQueue --- .../@node-red/editor-client/src/js/ui/palette-editor.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js b/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js index 9f7f964c3..3c0c36dff 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/palette-editor.js @@ -124,6 +124,7 @@ RED.palette.editor = (function() { const { type, body, callback } = moduleQueue[0]; if (type === "install") { + RED.eventLog.startEvent(RED._("palette.editor.confirm.button.install") + ` : ${body.module} ${body.version}`); $.ajax({ url: "nodes", type: "POST", @@ -140,6 +141,7 @@ RED.palette.editor = (function() { processQueue(); }); } else if (type === "remove") { + RED.eventLog.startEvent(RED._("palette.editor.confirm.button.remove") + ` : ${body.id}`); $.ajax({ url: "nodes/" + body.id, type: "DELETE" @@ -1270,7 +1272,6 @@ RED.palette.editor = (function() { evt.preventDefault(); RED.actions.invoke("core:show-event-log"); }); - RED.eventLog.startEvent(RED._("palette.editor.confirm.button.install")+" : "+entry.name+" "+version); installNodeModule(entry.name,version,url,function(xhr) { spinner.remove(); if (xhr) { @@ -1329,7 +1330,6 @@ RED.palette.editor = (function() { evt.preventDefault(); RED.actions.invoke("core:show-event-log"); }); - RED.eventLog.startEvent(RED._("palette.editor.confirm.button.remove")+" : "+entry.name); removeNodeModule(entry.name, function(xhr) { spinner.remove(); if (xhr) { @@ -1443,7 +1443,6 @@ RED.palette.editor = (function() { evt.preventDefault(); RED.actions.invoke("core:show-event-log"); }); - RED.eventLog.startEvent(RED._("palette.editor.confirm.button.install")+" : "+entry.id+" "+entry.version); installNodeModule(entry.id,entry.version,entry.pkg_url,function(xhr, textStatus,err) { spinner.remove(); if (err && xhr.status === 504) {