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[key] = JSON.parse(context[key].msg); context = JSON.parse(context.msg);
break; break;
case "number": case "number":
context[key] = Number(context[key].msg); context = Number(context.msg);
break; break;
case "boolean": case "boolean":
context[key] = context[key].msg === 'true'; context = context.msg === 'true';
break;
case "undefined":
context = undefined;
break; break;
default: default:
// format string[*] and unknown format will go here // format string[*] and unknown format will go here
context[key] = context[key].msg; context = context.msg;
break; 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; };
$.getJSON("context/global",function (data) { let globalContext={};
globalContext = convertContextData(data); var fetchGlobalContext = function(key){
// trigger testExpression after context data fetched $.getJSON("context/global/"+key,function (data) {
expressionEditor.setValue(expressionEditor.getValue(),-1); globalContext[key] = convertContextData(data);
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];
if (flowContext.hasOwnProperty(key)) {
return getValueFromContext(val,flowContext); 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];
if (globalContext.hasOwnProperty(key)) {
return getValueFromContext(val,globalContext); 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;