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

load context data only when using

This commit is contained in:
yuewen 2023-01-16 10:40:12 +08:00
parent 37007f54fb
commit 843afd075f

View File

@ -252,40 +252,42 @@
// convert context data base on format // convert context data base on format
function convertContextData(data) { function convertContextData(data) {
const context = data[RED.settings.context.default] let context = data;
Object.keys(context).forEach((key) => { switch (context.format) {
switch (context[key].format) { case "Object":
case "Object": context = JSON.parse(context.msg);
context[key] = JSON.parse(context[key].msg); break;
break; case "number":
case "number": context = Number(context.msg);
context[key] = Number(context[key].msg); break;
break; case "boolean":
case "boolean": context = context.msg === 'true';
context[key] = context[key].msg === 'true'; break;
break; case "undefined":
default: context = undefined;
// format string[*] and unknown format will go here break;
context[key] = context[key].msg; default:
break; // format string[*] and unknown format will go here
} context = context.msg;
}) break;
}
return context; return context;
} }
let flowContext; let flowContext={};
const flowId = RED.workspaces.active(); var fetchFlowContext = function(key){
$.getJSON("context/flow/" + flowId,function (data) { $.getJSON("context/flow/" + RED.workspaces.active()+"/"+key,function (data) {
flowContext = convertContextData(data); flowContext[key] = convertContextData(data);
// trigger testExpression after context data fetched testExpression();
expressionEditor.setValue(expressionEditor.getValue(),-1); })
}) };
let globalContext; let globalContext={};
$.getJSON("context/global",function (data) { var fetchGlobalContext = function(key){
globalContext = convertContextData(data); $.getJSON("context/global/"+key,function (data) {
// trigger testExpression after context data fetched globalContext[key] = convertContextData(data);
expressionEditor.setValue(expressionEditor.getValue(),-1); testExpression();
}) })
};
var testExpression = function() { var testExpression = function() {
var value = testDataEditor.getValue(); var value = testDataEditor.getValue();
var parsedData; var parsedData;
@ -306,16 +308,22 @@
} }
} }
expr.assign('flowContext',function(val){ expr.assign('flowContext',function(val){
if (flowContext) { var key = val.split('.')[0];
return getValueFromContext(val, flowContext); if (flowContext.hasOwnProperty(key)) {
return getValueFromContext(val,flowContext);
}else if (key){
fetchFlowContext(key);
} }
return null; return undefined;
}) })
expr.assign('globalContext',function(val){ expr.assign('globalContext',function(val){
if (globalContext) { var key = val.split('.')[0];
return getValueFromContext(val, globalContext); if (globalContext.hasOwnProperty(key)) {
return getValueFromContext(val,globalContext);
}else if (key){
fetchGlobalContext(key);
} }
return null; return undefined;
}); });
expr.assign("env", function(name) { expr.assign("env", function(name) {
usesEnv = true; usesEnv = true;