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

Allow get and keys to be called without callback

This commit is contained in:
HirokiUchikawa 2018-07-27 21:33:38 +09:00
parent 4e549dd426
commit 6533a9793c
3 changed files with 131 additions and 60 deletions

View File

@ -220,7 +220,7 @@ function createContext(id,seed) {
callback = storage; callback = storage;
storage = "_"; storage = "_";
} }
if (typeof callback !== 'function'){ if (callback && typeof callback !== 'function'){
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
} }
context = getContextStorage(storage); context = getContextStorage(storage);
@ -279,7 +279,7 @@ function createContext(id,seed) {
callback = storage; callback = storage;
storage = "_"; storage = "_";
} }
if (typeof callback !== 'function') { if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
} }
context = getContextStorage(storage); context = getContextStorage(storage);

View File

@ -550,15 +550,21 @@ describe('function node', function() {
} }
} }
it('should handle error on get persistable node context (w/o callback)', function(done) { it('should get persistable node context (w/o callback)', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.get('count','memory1');return msg;"}, var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.get('count','memory1');return msg;"},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1"); initContext(function () {
var n2 = helper.getNode("n2"); var n1 = helper.getNode("n1");
n1.context().set("count","0","memory1"); var n2 = helper.getNode("n2");
n1.receive({payload:"foo",topic: "bar"}); n1.context().set("count","0","memory1");
checkCallbackError('n1', done); n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', '0');
done();
});
n1.receive({payload:"foo",topic: "bar"});
});
}); });
}); });
@ -566,15 +572,17 @@ describe('function node', function() {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.get('count','memory1',function (err, val) { msg.payload=val; node.send(msg); });"}, var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.get('count','memory1',function (err, val) { msg.payload=val; node.send(msg); });"},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1"); initContext(function () {
var n2 = helper.getNode("n2"); var n1 = helper.getNode("n1");
n1.context().set("count","0","memory1"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n1.context().set("count","0","memory1");
msg.should.have.property('topic', 'bar'); n2.on("input", function(msg) {
msg.should.have.property('payload', '0'); msg.should.have.property('topic', 'bar');
done(); msg.should.have.property('payload', '0');
done();
});
n1.receive({payload:"foo",topic: "bar"});
}); });
n1.receive({payload:"foo",topic: "bar"});
}); });
}); });
@ -594,7 +602,7 @@ describe('function node', function() {
}); });
}); });
it('should handle error on get keys in persistable node context (w/o callback)', function(done) { it('should get keys in persistable node context (w/o callback)', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.keys('memory1');return msg;"}, var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.keys('memory1');return msg;"},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {
@ -602,8 +610,17 @@ describe('function node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n1.context().set("count","0","memory1"); n1.context().set("count","0","memory1");
n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', ['count']);
done();
}
catch(e) {
done(e);
}
});
n1.receive({payload:"foo",topic: "bar"}); n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
}); });
}); });
}); });
@ -793,15 +810,21 @@ describe('function node', function() {
}); });
}); });
it('should handle error on get persistable flow context (w/o callback)', function(done) { it('should get persistable flow context (w/o callback)', function(done) {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.get('count','memory1');return msg;"}, var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.get('count','memory1');return msg;"},
{id:"n2", type:"helper",z:"flowA"}]; {id:"n2", type:"helper",z:"flowA"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1"); initContext(function () {
var n2 = helper.getNode("n2"); var n1 = helper.getNode("n1");
n1.context().flow.set("count","0","memory1"); var n2 = helper.getNode("n2");
n1.receive({payload:"foo",topic: "bar"}); n1.context().flow.set("count","0","memory1");
checkCallbackError('n1', done); n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', '0');
done();
});
n1.receive({payload:"foo",topic: "bar"});
});
}); });
}); });
@ -809,15 +832,17 @@ describe('function node', function() {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.get('count','memory1', function(err, val) { msg.payload=val; node.send(msg); });"}, var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.get('count','memory1', function(err, val) { msg.payload=val; node.send(msg); });"},
{id:"n2", type:"helper",z:"flowA"}]; {id:"n2", type:"helper",z:"flowA"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1"); initContext(function () {
var n2 = helper.getNode("n2"); var n1 = helper.getNode("n1");
n1.context().flow.set("count","0","memory1"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n1.context().flow.set("count","0","memory1");
msg.should.have.property('topic', 'bar'); n2.on("input", function(msg) {
msg.should.have.property('payload', '0'); msg.should.have.property('topic', 'bar');
done(); msg.should.have.property('payload', '0');
done();
});
n1.receive({payload:"foo",topic: "bar"});
}); });
n1.receive({payload:"foo",topic: "bar"});
}); });
}); });
@ -837,20 +862,6 @@ describe('function node', function() {
}); });
}); });
it('should handle error on get persistable flow context', function(done) {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=context.flow.get('count','memory1');return msg;"},
{id:"n2", type:"helper",z:"flowA"}];
helper.load(functionNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n1.context().flow.set("count","0","memory1");
n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
});
});
});
it('should get keys in flow context', function(done) { it('should get keys in flow context', function(done) {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.keys();return msg;"}, var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.keys();return msg;"},
{id:"n2", type:"helper",z:"flowA"}]; {id:"n2", type:"helper",z:"flowA"}];
@ -867,7 +878,7 @@ describe('function node', function() {
}); });
}); });
it('should handle error on get keys in persistable flow context (w/o callback)', function(done) { it('should get keys in persistable flow context (w/o callback)', function(done) {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.keys('memory1');return msg;"}, var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.keys('memory1');return msg;"},
{id:"n2", type:"helper",z:"flowA"}]; {id:"n2", type:"helper",z:"flowA"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {
@ -875,8 +886,17 @@ describe('function node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n1.context().flow.set("count","0","memory1"); n1.context().flow.set("count","0","memory1");
n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', ['count']);
done();
}
catch(e) {
done(e);
}
});
n1.receive({payload:"foo",topic: "bar"}); n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
}); });
}); });
}); });
@ -986,7 +1006,7 @@ describe('function node', function() {
}); });
}); });
it('should handle error on get persistable global context (w/o callback)', function(done) { it('should get persistable global context (w/o callback)', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=global.get('count', 'memory1');return msg;"}, var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=global.get('count', 'memory1');return msg;"},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
initContext(function () { initContext(function () {
@ -994,8 +1014,12 @@ describe('function node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n1.context().global.set("count","0", 'memory1'); n1.context().global.set("count","0", 'memory1');
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', '0');
done();
});
n1.receive({payload:"foo",topic: "bar"}); n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
}); });
}); });
}); });
@ -1034,7 +1058,7 @@ describe('function node', function() {
}); });
}); });
it('should handle error on get persistable global context (w/o callback)', function(done) { it('should get persistable global context (w/o callback)', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.global.get('count','memory1');return msg;"}, var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.global.get('count','memory1');return msg;"},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {
@ -1042,8 +1066,17 @@ describe('function node', function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n1.context().global.set("count","0", "memory1"); n1.context().global.set("count","0", "memory1");
n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', '0');
done();
}
catch(e) {
done(e);
}
});
n1.receive({payload:"foo",topic: "bar"}); n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
}); });
}); });
}); });
@ -1071,6 +1104,48 @@ describe('function node', function() {
}); });
}); });
it('should handle error on get persistable context', function(done) {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=context.get('count','memory1','callback');return msg;"},
{id:"n2", type:"helper",z:"flowA"}];
helper.load(functionNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n1.context().flow.set("count","0","memory1");
n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
});
});
});
it('should handle error on set persistable context', function(done) {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=context.set('count','0','memory1','callback');return msg;"},
{id:"n2", type:"helper",z:"flowA"}];
helper.load(functionNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
});
});
});
it('should handle error on get keys in persistable context', function(done) {
var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=context.keys('memory1','callback');return msg;"},
{id:"n2", type:"helper",z:"flowA"}];
helper.load(functionNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n1.context().flow.set("count","0","memory1");
n1.receive({payload:"foo",topic: "bar"});
checkCallbackError('n1', done);
});
});
});
it('should handle setTimeout()', function(done) { it('should handle setTimeout()', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"setTimeout(function(){node.send(msg);},1000);"}, var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"setTimeout(function(){node.send(msg);},1000);"},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];

View File

@ -793,15 +793,13 @@ describe('context', function() {
}); });
}); });
it('should throw an error if callback of context.get is not specified', function (done) { it('should not throw an error if callback of context.get is not specified', function (done) {
Context.init({ contextStorage: memoryStorage }); Context.init({ contextStorage: memoryStorage });
Context.load().then(function () { Context.load().then(function () {
var context = Context.get("1", "flow"); var context = Context.get("1", "flow");
context.get("foo", "memory"); context.get("foo", "memory");
done("should throw an error.");
}).catch(function () {
done(); done();
}); }).catch(done);
}); });
it('should throw an error if callback of context.set is not a function', function (done) { it('should throw an error if callback of context.set is not a function', function (done) {
@ -835,15 +833,13 @@ describe('context', function() {
}); });
}); });
it('should throw an error if callback of context.keys is not specified', function (done) { it('should not throw an error if callback of context.keys is not specified', function (done) {
Context.init({ contextStorage: memoryStorage }); Context.init({ contextStorage: memoryStorage });
Context.load().then(function () { Context.load().then(function () {
var context = Context.get("1", "flow"); var context = Context.get("1", "flow");
context.keys("memory"); context.keys("memory");
done("should throw an error.");
}).catch(function () {
done(); done();
}); }).catch(done);
}); });
}); });