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

Switch node: only use promises when absolutely necessary

Fixes a significant performance regression introduced when the
node was made async-only with the persistent context work.
This commit is contained in:
Nick O'Leary 2018-09-14 14:03:36 +01:00
parent 17a737ca88
commit 66ee27c5fa
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -92,6 +92,7 @@ module.exports = function(RED) {
} }
function getProperty(node,msg) { function getProperty(node,msg) {
if (node.useAsyncRules) {
return new Promise((resolve,reject) => { return new Promise((resolve,reject) => {
if (node.propertyType === 'jsonata') { if (node.propertyType === 'jsonata') {
RED.util.evaluateJSONataExpression(node.property,msg,(err,value) => { RED.util.evaluateJSONataExpression(node.property,msg,(err,value) => {
@ -111,9 +112,25 @@ module.exports = function(RED) {
}); });
} }
}); });
} else {
if (node.propertyType === 'jsonata') {
try {
return RED.util.evaluateJSONataExpression(node.property,msg);
} catch(err) {
throw new Error(RED._("switch.errors.invalid-expr",{error:err.message}))
}
} else {
try {
return RED.util.evaluateNodeProperty(node.property,node.propertyType,node,msg);
} catch(err) {
return undefined;
}
}
}
} }
function getV1(node,msg,rule,hasParts) { function getV1(node,msg,rule,hasParts) {
if (node.useAsyncRules) {
return new Promise( (resolve,reject) => { return new Promise( (resolve,reject) => {
if (rule.vt === 'prev') { if (rule.vt === 'prev') {
resolve(node.previousValue); resolve(node.previousValue);
@ -133,7 +150,7 @@ module.exports = function(RED) {
} }
}); });
} else if (rule.vt === 'json') { } else if (rule.vt === 'json') {
resolve("json"); resolve("json"); // TODO: ?! invalid case
} else if (rule.vt === 'null') { } else if (rule.vt === 'null') {
resolve("null"); resolve("null");
} else { } else {
@ -146,9 +163,38 @@ module.exports = function(RED) {
}); });
} }
}); });
} else {
if (rule.vt === 'prev') {
return node.previousValue;
} else if (rule.vt === 'jsonata') {
var exp = rule.v;
if (rule.t === 'jsonata_exp') {
if (hasParts) {
exp.assign("I", msg.parts.index);
exp.assign("N", msg.parts.count);
}
}
try {
return RED.util.evaluateJSONataExpression(exp,msg);
} catch(err) {
throw new Error(RED._("switch.errors.invalid-expr",{error:err.message}))
}
} else if (rule.vt === 'json') {
return "json"; // TODO: ?! invalid case
} else if (rule.vt === 'null') {
return "null";
} else {
try {
return RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
} catch(err) {
return undefined;
}
}
}
} }
function getV2(node,msg,rule) { function getV2(node,msg,rule) {
if (node.useAsyncRules) {
return new Promise((resolve,reject) => { return new Promise((resolve,reject) => {
var v2 = rule.v2; var v2 = rule.v2;
if (rule.v2t === 'prev') { if (rule.v2t === 'prev') {
@ -173,9 +219,30 @@ module.exports = function(RED) {
resolve(v2); resolve(v2);
} }
}) })
} else {
var v2 = rule.v2;
if (rule.v2t === 'prev') {
return node.previousValue;
} else if (rule.v2t === 'jsonata') {
try {
return RED.util.evaluateJSONataExpression(rule.v2,msg);
} catch(err) {
throw new Error(RED._("switch.errors.invalid-expr",{error:err.message}))
}
} else if (typeof v2 !== 'undefined') {
try {
return RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg);
} catch(err) {
return undefined;
}
} else {
return v2;
}
}
} }
function applyRule(node, msg, property, state) { function applyRule(node, msg, property, state) {
if (node.useAsyncRules) {
return new Promise((resolve,reject) => { return new Promise((resolve,reject) => {
var rule = node.rules[state.currentRule]; var rule = node.rules[state.currentRule];
@ -202,6 +269,25 @@ module.exports = function(RED) {
resolve(state.currentRule < node.rules.length - 1); resolve(state.currentRule < node.rules.length - 1);
}); });
}) })
} else {
var rule = node.rules[state.currentRule];
var v1 = getV1(node,msg,rule,state.hasParts);
var v2 = getV2(node,msg,rule);
if (rule.t == "else") {
property = state.elseflag;
state.elseflag = true;
}
if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) {
state.onward.push(msg);
state.elseflag = false;
if (node.checkall == "false") {
return false;
}
} else {
state.onward.push(null);
}
return state.currentRule < node.rules.length - 1
}
} }
function applyRules(node, msg, property,state) { function applyRules(node, msg, property,state) {
@ -215,6 +301,7 @@ module.exports = function(RED) {
msg.parts.hasOwnProperty("index") msg.parts.hasOwnProperty("index")
} }
} }
if (node.useAsyncRules) {
return applyRule(node,msg,property,state).then(hasMore => { return applyRule(node,msg,property,state).then(hasMore => {
if (hasMore) { if (hasMore) {
state.currentRule++; state.currentRule++;
@ -224,6 +311,16 @@ module.exports = function(RED) {
return state.onward; return state.onward;
} }
}); });
} else {
var hasMore = applyRule(node,msg,property,state);
if (hasMore) {
state.currentRule++;
return applyRules(node,msg,property,state);
} else {
node.previousValue = property;
return state.onward;
}
}
} }
@ -248,6 +345,14 @@ module.exports = function(RED) {
var valid = true; var valid = true;
var repair = n.repair; var repair = n.repair;
var needsCount = repair; var needsCount = repair;
this.useAsyncRules = (
this.propertyType === 'flow' ||
this.propertyType === 'global' || (
this.propertyType === 'jsonata' &&
/\$(flow|global)Context/.test(this.property)
)
);
for (var i=0; i<this.rules.length; i+=1) { for (var i=0; i<this.rules.length; i+=1) {
var rule = this.rules[i]; var rule = this.rules[i];
needsCount = needsCount || ((rule.t === "tail") || (rule.t === "jsonata_exp")); needsCount = needsCount || ((rule.t === "tail") || (rule.t === "jsonata_exp"));
@ -258,6 +363,13 @@ module.exports = function(RED) {
rule.vt = 'str'; rule.vt = 'str';
} }
} }
this.useAsyncRules = this.useAsyncRules || (
rule.vt === 'flow' ||
rule.vt === 'global' || (
rule.vt === 'jsonata' &&
/\$(flow|global)Context/.test(rule.v)
)
);
if (rule.vt === 'num') { if (rule.vt === 'num') {
if (!isNaN(Number(rule.v))) { if (!isNaN(Number(rule.v))) {
rule.v = Number(rule.v); rule.v = Number(rule.v);
@ -270,6 +382,9 @@ module.exports = function(RED) {
valid = false; valid = false;
} }
} }
if (rule.vt === 'flow' || rule.vt === 'global' || rule.vt === 'jsonata') {
this.useAsyncRules = true;
}
if (typeof rule.v2 !== 'undefined') { if (typeof rule.v2 !== 'undefined') {
if (!rule.v2t) { if (!rule.v2t) {
if (!isNaN(Number(rule.v2))) { if (!isNaN(Number(rule.v2))) {
@ -278,6 +393,13 @@ module.exports = function(RED) {
rule.v2t = 'str'; rule.v2t = 'str';
} }
} }
this.useAsyncRules = this.useAsyncRules || (
rule.v2t === 'flow' ||
rule.v2t === 'global' || (
rule.v2t === 'jsonata' &&
/\$(flow|global)Context/.test(rule.v2)
)
);
if (rule.v2t === 'num') { if (rule.v2t === 'num') {
rule.v2 = Number(rule.v2); rule.v2 = Number(rule.v2);
} else if (rule.v2t === 'jsonata') { } else if (rule.v2t === 'jsonata') {
@ -290,7 +412,6 @@ module.exports = function(RED) {
} }
} }
} }
if (!valid) { if (!valid) {
return; return;
} }
@ -420,6 +541,7 @@ module.exports = function(RED) {
if (needsCount && checkParts && hasParts) { if (needsCount && checkParts && hasParts) {
return addMessageToPending(msg); return addMessageToPending(msg);
} }
if (node.useAsyncRules) {
return getProperty(node,msg) return getProperty(node,msg)
.then(property => applyRules(node,msg,property)) .then(property => applyRules(node,msg,property))
.then(onward => { .then(onward => {
@ -432,6 +554,19 @@ module.exports = function(RED) {
}).catch(err => { }).catch(err => {
node.warn(err); node.warn(err);
}); });
} else {
try {
var property = getProperty(node,msg);
var onward = applyRules(node,msg,property);
if (!repair || !hasParts) {
node.send(onward);
} else {
sendGroupMessages(onward, msg);
}
} catch(err) {
node.warn(err);
}
}
} }
function clearPending() { function clearPending() {
@ -473,7 +608,11 @@ module.exports = function(RED) {
} }
this.on('input', function(msg) { this.on('input', function(msg) {
if (node.useAsyncRules) {
processMessageQueue(msg); processMessageQueue(msg);
} else {
processMessage(msg,true);
}
}); });
this.on('close', function() { this.on('close', function() {