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

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){
if(error){
callback(error);
} else {
callback(null, value); 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){
if(error){
callback(error);
} else {
callback(null, values); callback(null, values);
}
} else { } else {
return values; return values;
} }

View File

@ -32,6 +32,7 @@ describe('memory',function() {
}); });
describe('#get/set',function() { describe('#get/set',function() {
describe('sync',function() {
it('should store property',function() { it('should store property',function() {
should.not.exist(context.get("nodeX","foo")); should.not.exist(context.get("nodeX","foo"));
context.set("nodeX","foo","test"); context.set("nodeX","foo","test");
@ -64,9 +65,50 @@ describe('memory',function() {
context.get("nodeX","foo").should.equal("testX"); context.get("nodeX","foo").should.equal("testX");
context.get("nodeY","foo").should.equal("testY"); 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);
}
}
});
});
describe('async',function() {
it('should store property',function(done) {
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 pass the error to callback if the error occurs',function(done) {
context.set("nodeX",".foo","test",function(err, value){
should.exist(err);
context.get("nodeX",".foo",function(err){
should.exist(err);
done();
});
});
});
});
}); });
describe('#keys',function() { describe('#keys',function() {
describe('sync',function() {
it('should enumerate context keys', function() { it('should enumerate context keys', function() {
var keys = context.keys("nodeX"); var keys = context.keys("nodeX");
keys.should.be.an.Array(); keys.should.be.an.Array();
@ -102,7 +144,29 @@ describe('memory',function() {
keysY.should.have.length(1); keysY.should.have.length(1);
keysY[0].should.equal("hoge"); keysY[0].should.equal("hoge");
}); });
});
describe('async',function() {
it('should enumerate context keys', function(done) {
context.keys("nodeX", function(err, keys) {
keys.should.be.an.Array();
keys.should.be.empty();
context.set("nodeX", "foo", "bar", function(err) {
context.keys("nodeX", function(err, keys) {
keys.should.have.length(1);
keys[0].should.equal("foo");
context.set("nodeX","abc.def","bar",function(err){
context.keys("nodeX",function(err, keys){
keys.should.have.length(2);
keys[1].should.equal("abc");
done();
});
});
});
});
});
});
});
}); });
describe('#delete',function() { describe('#delete',function() {