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

Allow the JSONata Expression to handle persistable store.

This commit is contained in:
HirokiUchikawa 2018-07-13 18:12:06 +09:00
parent d21e719cc1
commit adb0891335
2 changed files with 34 additions and 6 deletions

View File

@ -408,9 +408,9 @@ function evaluateJSONataExpression(expr,msg,callback) {
if (callback) {
// If callback provided, need to override the pre-assigned sync
// context functions to be their async variants
bindings.flowContext = function(val) {
bindings.flowContext = function(val, store) {
return new Promise((resolve,reject) => {
expr._node.context().flow.get(val, function(err,value) {
expr._node.context().flow.get(val, store, function(err,value) {
if (err) {
reject(err);
} else {
@ -419,9 +419,9 @@ function evaluateJSONataExpression(expr,msg,callback) {
})
});
}
bindings.globalContext = function(val) {
bindings.globalContext = function(val, store) {
return new Promise((resolve,reject) => {
expr._node.context().global.get(val, function(err,value) {
expr._node.context().global.get(val, store, function(err,value) {
if (err) {
reject(err);
} else {

View File

@ -459,7 +459,7 @@ describe("red/util", function() {
should.not.exist(result);
});
it('handles async flow context access', function(done) {
var expr = util.prepareJSONataExpression('$flowContext("foo")',{context:function() { return {flow:{get: function(key,callback) { setTimeout(()=>{callback(null,{'foo':'bar'}[key])},10)}}}}});
var expr = util.prepareJSONataExpression('$flowContext("foo")',{context:function() { return {flow:{get: function(key,store,callback) { setTimeout(()=>{callback(null,{'foo':'bar'}[key])},10)}}}}});
util.evaluateJSONataExpression(expr,{payload:"hello"},function(err,value) {
try {
should.not.exist(err);
@ -471,7 +471,7 @@ describe("red/util", function() {
});
})
it('handles async global context access', function(done) {
var expr = util.prepareJSONataExpression('$globalContext("foo")',{context:function() { return {global:{get: function(key,callback) { setTimeout(()=>{callback(null,{'foo':'bar'}[key])},10)}}}}});
var expr = util.prepareJSONataExpression('$globalContext("foo")',{context:function() { return {global:{get: function(key,store,callback) { setTimeout(()=>{callback(null,{'foo':'bar'}[key])},10)}}}}});
util.evaluateJSONataExpression(expr,{payload:"hello"},function(err,value) {
try {
should.not.exist(err);
@ -482,6 +482,34 @@ describe("red/util", function() {
}
});
})
it('handles persistable store in flow context access', function(done) {
var storeName;
var expr = util.prepareJSONataExpression('$flowContext("foo", "flowStoreName")',{context:function() { return {flow:{get: function(key,store,callback) { storeName = store;setTimeout(()=>{callback(null,{'foo':'bar'}[key])},10)}}}}});
util.evaluateJSONataExpression(expr,{payload:"hello"},function(err,value) {
try {
should.not.exist(err);
value.should.eql("bar");
storeName.should.equal("flowStoreName");
done();
} catch(err2) {
done(err2);
}
});
})
it('handles persistable store in global context access', function(done) {
var storeName;
var expr = util.prepareJSONataExpression('$globalContext("foo", "globalStoreName")',{context:function() { return {global:{get: function(key,store,callback) { storeName = store;setTimeout(()=>{callback(null,{'foo':'bar'}[key])},10)}}}}});
util.evaluateJSONataExpression(expr,{payload:"hello"},function(err,value) {
try {
should.not.exist(err);
value.should.eql("bar");
storeName.should.equal("globalStoreName");
done();
} catch(err2) {
done(err2);
}
});
})
});