Merge pull request #1796 from node-red-hitachi/0.19-multiple-values

Make it possible to get multiple values
This commit is contained in:
Nick O'Leary 2018-07-12 10:01:18 +01:00 committed by GitHub
commit 2a287b2ae6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 8 deletions

View File

@ -191,13 +191,38 @@ function createContext(id,seed) {
// Get the value from the underlying store. If it is undefined,
// check the seed for a default value.
if (callback) {
context.get(scope,key,function(err, v) {
if (v === undefined) {
callback(err, seed[key]);
} else {
callback(err, v);
}
})
if (!Array.isArray(key)) {
context.get(scope,key,function(err, v) {
if (v === undefined) {
callback(err, seed[key]);
} else {
callback(err, v);
}
});
} else {
// If key is an array, get the value of each key.
var storeValues = [];
var keys = key.slice();
var _key = keys.shift();
var cb = function(err, v) {
if (err) {
callback(err);
} else {
if (v === undefined) {
storeValues.push(seed[_key]);
} else {
storeValues.push(v);
}
if (keys.length === 0) {
callback.apply(null, [null].concat(storeValues));
} else {
_key = keys.shift();
context.get(scope, _key, cb);
}
}
};
context.get(scope, _key, cb);
}
} else {
// No callback, attempt to do this synchronously
var storeValue = context.get(scope,key);
@ -208,7 +233,25 @@ function createContext(id,seed) {
}
}
} else {
return context.get(scope, key, callback);
if (!Array.isArray(key)) {
return context.get(scope, key, callback);
} else {
var storeValues = [];
var keys = key.slice();
var cb = function(err, v) {
if (err) {
callback(err);
} else {
storeValues.push(v);
if (keys.length === 0) {
callback.apply(null, [null].concat(storeValues));
} else {
context.get(scope, keys.shift(), cb);
}
}
};
context.get(scope, keys.shift(), cb);
}
}
};
obj.set = function(key, value, storage, callback) {

View File

@ -545,6 +545,76 @@ describe('context', function() {
}).catch(done);
})
it('should return multiple values if key is an array', function(done) {
Context.init({contextStorage:memoryStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.set("foo1","bar1","memory");
context.set("foo2","bar2","memory");
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
foo1.should.be.equal("bar1");
foo2.should.be.equal("bar2");
should.not.exist(foo3);
done();
}
});
}).catch(function(err){ done(err); });
});
it('should return multiple functionGlobalContext values if key is an array', function(done) {
var fGC = { "foo1": 456, "foo2": 789 };
Context.init({contextStorage:memoryStorage, functionGlobalContext:fGC });
Context.load().then(function(){
var context = Context.get("1","flow");
context.global.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
foo1.should.be.equal(456);
foo2.should.be.equal(789);
should.not.exist(foo3);
done();
}
});
}).catch(function(err){ done(err); });
});
it('should return an error if an error occurs in getting multiple store values', function(done) {
Context.init({contextStorage:contextStorage});
stubGet.onFirstCall().callsArgWith(2, null, "bar1");
stubGet.onSecondCall().callsArgWith(2, "error2");
stubGet.onThirdCall().callsArgWith(2, null, "bar3");
Context.load().then(function(){
var context = Context.get("1","flow");
context.global.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err === "error2") {
done();
} else {
done("An error occurred");
}
});
}).catch(function(err){ done(err); });
});
it('should return a first error if some errors occur in getting multiple store values', function(done) {
Context.init({contextStorage:contextStorage});
stubGet.onFirstCall().callsArgWith(2, "error1");
stubGet.onSecondCall().callsArgWith(2, null, "bar2");
stubGet.onThirdCall().callsArgWith(2, "error3");
Context.load().then(function(){
var context = Context.get("1","flow");
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err === "error1") {
done();
} else {
done("An error occurred");
}
});
}).catch(function(err){ done(err); });
});
});
describe('delete context',function(){