mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #1796 from node-red-hitachi/0.19-multiple-values
Make it possible to get multiple values
This commit is contained in:
commit
2a287b2ae6
@ -191,13 +191,38 @@ function createContext(id,seed) {
|
|||||||
// Get the value from the underlying store. If it is undefined,
|
// Get the value from the underlying store. If it is undefined,
|
||||||
// check the seed for a default value.
|
// check the seed for a default value.
|
||||||
if (callback) {
|
if (callback) {
|
||||||
context.get(scope,key,function(err, v) {
|
if (!Array.isArray(key)) {
|
||||||
if (v === undefined) {
|
context.get(scope,key,function(err, v) {
|
||||||
callback(err, seed[key]);
|
if (v === undefined) {
|
||||||
} else {
|
callback(err, seed[key]);
|
||||||
callback(err, v);
|
} 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 {
|
} else {
|
||||||
// No callback, attempt to do this synchronously
|
// No callback, attempt to do this synchronously
|
||||||
var storeValue = context.get(scope,key);
|
var storeValue = context.get(scope,key);
|
||||||
@ -208,7 +233,25 @@ function createContext(id,seed) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
obj.set = function(key, value, storage, callback) {
|
||||||
|
@ -545,6 +545,76 @@ describe('context', function() {
|
|||||||
}).catch(done);
|
}).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(){
|
describe('delete context',function(){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user