Merge pull request #1800 from node-red-hitachi/0.19-callback-called-twice

Prevent the callback to be called twice
This commit is contained in:
Nick O'Leary 2018-07-12 10:45:58 +01:00 committed by GitHub
commit 051c147b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 140 additions and 65 deletions

View File

@ -30,19 +30,24 @@ Memory.prototype.close = function(){
Memory.prototype.get = function(scope, key, callback) { Memory.prototype.get = function(scope, key, callback) {
var value; var value;
var error;
try{ try{
if(this.data[scope]){ if(this.data[scope]){
value = util.getMessageProperty(this.data[scope], key); value = util.getMessageProperty(this.data[scope], key);
} }
}catch(err){ }catch(err){
if(callback){ if(callback){
callback(err); error = err;
}else{ }else{
throw err; throw err;
} }
} }
if(callback){ if(callback){
callback(null, value); if(error){
callback(error);
} else {
callback(null, value);
}
} else { } else {
return value; return value;
} }
@ -52,22 +57,24 @@ Memory.prototype.set =function(scope, key, value, callback) {
if(!this.data[scope]){ if(!this.data[scope]){
this.data[scope] = {}; this.data[scope] = {};
} }
var error;
try{ try{
util.setMessageProperty(this.data[scope],key,value); util.setMessageProperty(this.data[scope],key,value);
}catch(err){ }catch(err){
if(callback){ if(callback){
callback(err); error = err;
}else{ }else{
throw err; throw err;
} }
} }
if(callback){ if(callback){
callback(null); callback(error || null);
} }
}; };
Memory.prototype.keys = function(scope, callback){ Memory.prototype.keys = function(scope, callback){
var values = []; var values = [];
var error;
try{ try{
if(this.data[scope]){ if(this.data[scope]){
if (scope !== "global") { if (scope !== "global") {
@ -80,13 +87,17 @@ Memory.prototype.keys = function(scope, callback){
} }
}catch(err){ }catch(err){
if(callback){ if(callback){
callback(err); error = err;
}else{ }else{
throw err; throw err;
} }
} }
if(callback){ if(callback){
callback(null, values); if(error){
callback(error);
} else {
callback(null, values);
}
} else { } else {
return values; return values;
} }

View File

@ -32,77 +32,141 @@ describe('memory',function() {
}); });
describe('#get/set',function() { describe('#get/set',function() {
it('should store property',function() { describe('sync',function() {
should.not.exist(context.get("nodeX","foo")); it('should store property',function() {
context.set("nodeX","foo","test"); should.not.exist(context.get("nodeX","foo"));
context.get("nodeX","foo").should.equal("test"); context.set("nodeX","foo","test");
context.get("nodeX","foo").should.equal("test");
});
it('should store property - creates parent properties',function() {
context.set("nodeX","foo.bar","test");
context.get("nodeX","foo").should.eql({bar:"test"});
});
it('should delete property',function() {
context.set("nodeX","foo.abc.bar1","test1");
context.set("nodeX","foo.abc.bar2","test2");
context.get("nodeX","foo.abc").should.eql({bar1:"test1",bar2:"test2"});
context.set("nodeX","foo.abc.bar1",undefined);
context.get("nodeX","foo.abc").should.eql({bar2:"test2"});
context.set("nodeX","foo.abc",undefined);
should.not.exist(context.get("nodeX","foo.abc"));
context.set("nodeX","foo",undefined);
should.not.exist(context.get("nodeX","foo"));
});
it('should not shared context with other scope', function() {
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","testX");
context.set("nodeY","foo","testY");
context.get("nodeX","foo").should.equal("testX");
context.get("nodeY","foo").should.equal("testY");
});
it('should thorw the error if the error occurs', function() {
try{
context.set("nodeX",".foo","test");
should.fail("Error was not thrown");
}catch(err){
should.exist(err);
try{
context.get("nodeX",".foo");
should.fail("Error was not thrown");
}catch(err){
should.exist(err);
}
}
});
}); });
it('should store property - creates parent properties',function() { describe('async',function() {
context.set("nodeX","foo.bar","test"); it('should store property',function(done) {
context.get("nodeX","foo").should.eql({bar:"test"}); context.get("nodeX","foo",function(err, value){
}); should.not.exist(value);
context.set("nodeX","foo","test",function(err){
context.get("nodeX","foo",function(err, value){
value.should.equal("test");
done();
});
});
});
});
it('should delete property',function() { it('should pass the error to callback if the error occurs',function(done) {
context.set("nodeX","foo.abc.bar1","test1"); context.set("nodeX",".foo","test",function(err, value){
context.set("nodeX","foo.abc.bar2","test2"); should.exist(err);
context.get("nodeX","foo.abc").should.eql({bar1:"test1",bar2:"test2"}); context.get("nodeX",".foo",function(err){
context.set("nodeX","foo.abc.bar1",undefined); should.exist(err);
context.get("nodeX","foo.abc").should.eql({bar2:"test2"}); done();
context.set("nodeX","foo.abc",undefined); });
should.not.exist(context.get("nodeX","foo.abc")); });
context.set("nodeX","foo",undefined); });
should.not.exist(context.get("nodeX","foo"));
});
it('should not shared context with other scope', function() {
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","testX");
context.set("nodeY","foo","testY");
context.get("nodeX","foo").should.equal("testX");
context.get("nodeY","foo").should.equal("testY");
}); });
}); });
describe('#keys',function() { describe('#keys',function() {
it('should enumerate context keys', function() { describe('sync',function() {
var keys = context.keys("nodeX"); it('should enumerate context keys', function() {
keys.should.be.an.Array(); var keys = context.keys("nodeX");
keys.should.be.empty(); keys.should.be.an.Array();
keys.should.be.empty();
context.set("nodeX","foo","bar"); context.set("nodeX","foo","bar");
keys = context.keys("nodeX"); keys = context.keys("nodeX");
keys.should.have.length(1); keys.should.have.length(1);
keys[0].should.equal("foo"); keys[0].should.equal("foo");
context.set("nodeX","abc.def","bar"); context.set("nodeX","abc.def","bar");
keys = context.keys("nodeX"); keys = context.keys("nodeX");
keys.should.have.length(2); keys.should.have.length(2);
keys[1].should.equal("abc"); keys[1].should.equal("abc");
});
it('should enumerate context keys in each scopes', function() {
var keysX = context.keys("nodeX");
keysX.should.be.an.Array();
keysX.should.be.empty();
var keysY = context.keys("nodeY");
keysY.should.be.an.Array();
keysY.should.be.empty();
context.set("nodeX","foo","bar");
context.set("nodeY","hoge","piyo");
keysX = context.keys("nodeX");
keysX.should.have.length(1);
keysX[0].should.equal("foo");
keysY = context.keys("nodeY");
keysY.should.have.length(1);
keysY[0].should.equal("hoge");
});
}); });
it('should enumerate context keys in each scopes', function() { describe('async',function() {
var keysX = context.keys("nodeX"); it('should enumerate context keys', function(done) {
keysX.should.be.an.Array(); context.keys("nodeX", function(err, keys) {
keysX.should.be.empty(); keys.should.be.an.Array();
keys.should.be.empty();
var keysY = context.keys("nodeY"); context.set("nodeX", "foo", "bar", function(err) {
keysY.should.be.an.Array(); context.keys("nodeX", function(err, keys) {
keysY.should.be.empty(); keys.should.have.length(1);
keys[0].should.equal("foo");
context.set("nodeX","foo","bar"); context.set("nodeX","abc.def","bar",function(err){
context.set("nodeY","hoge","piyo"); context.keys("nodeX",function(err, keys){
keysX = context.keys("nodeX"); keys.should.have.length(2);
keysX.should.have.length(1); keys[1].should.equal("abc");
keysX[0].should.equal("foo"); done();
});
keysY = context.keys("nodeY"); });
keysY.should.have.length(1); });
keysY[0].should.equal("hoge"); });
});
});
}); });
}); });
describe('#delete',function() { describe('#delete',function() {