1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Fix async loading of modules in function node

This commit is contained in:
Nick O'Leary 2021-07-14 21:10:59 +01:00
parent cce6a47f11
commit 395b499856
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -306,34 +306,35 @@ module.exports = function(RED) {
sandbox[vname] = null; sandbox[vname] = null;
var spec = module.module; var spec = module.module;
if (spec && (spec !== "")) { if (spec && (spec !== "")) {
RED.import(module.module).then(lib => { moduleLoadPromises.push(RED.import(module.module).then(lib => {
sandbox[vname] = lib; sandbox[vname] = lib;
}).catch(err => { }).catch(err => {
node.error(RED._("function.error.moduleLoadError",{module:module.spec, error:err.toString()})) node.error(RED._("function.error.moduleLoadError",{module:module.spec, error:err.toString()}))
moduleErrors = true;
throw err; throw err;
}); }));
} }
} }
}); });
if (moduleErrors) {
throw new Error(RED._("function.error.externalModuleLoadError"));
}
} }
const RESOLVING = 0;
const RESOLVED = 1;
const ERROR = 2;
var state = RESOLVING;
var messages = [];
var processMessage = (() => {});
node.on("input", function(msg,send,done) {
if(state === RESOLVING) {
messages.push({msg:msg, send:send, done:done});
}
else if(state === RESOLVED) {
processMessage(msg, send, done);
}
});
Promise.all(moduleLoadPromises).then(() => { Promise.all(moduleLoadPromises).then(() => {
const RESOLVING = 0;
const RESOLVED = 1;
const ERROR = 2;
var state = RESOLVING;
var messages = [];
var processMessage = (() => {});
node.on("input", function(msg,send,done) {
if(state === RESOLVING) {
messages.push({msg:msg, send:send, done:done});
}
else if(state === RESOLVED) {
processMessage(msg, send, done);
}
});
var context = vm.createContext(sandbox); var context = vm.createContext(sandbox);
try { try {
var iniScript = null; var iniScript = null;