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