mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Allow multiple keys and values to be passed to set
This commit is contained in:
parent
051c147b41
commit
6b2f5fbb19
@ -268,7 +268,29 @@ function createContext(id,seed) {
|
||||
}
|
||||
context = getContextStorage(storage);
|
||||
}
|
||||
context.set(scope, key, value, callback);
|
||||
if (!Array.isArray(key) || !Array.isArray(value) || key.length !== value.length) {
|
||||
context.set(scope, key, value, callback);
|
||||
} else {
|
||||
// If key and value are Array and each length is same, set each key-value pair.
|
||||
var index = 0;
|
||||
var cb = function(err, v) {
|
||||
if (err) {
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
} else {
|
||||
index++;
|
||||
if (index === key.length) {
|
||||
if (callback) {
|
||||
callback(null);
|
||||
}
|
||||
} else {
|
||||
context.set(scope, key[index], value[index], cb);
|
||||
}
|
||||
}
|
||||
};
|
||||
context.set(scope, key[index], value[index], cb);
|
||||
}
|
||||
};
|
||||
obj.keys = function(storage, callback) {
|
||||
var context;
|
||||
|
@ -615,6 +615,46 @@ describe('context', function() {
|
||||
});
|
||||
}).catch(function(err){ done(err); });
|
||||
});
|
||||
|
||||
it('should store multiple properties if key and value are arrays', function(done) {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
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");
|
||||
foo3.should.be.equal("bar3");
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error if an error occurs in storing multiple values', function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
stubSet.onFirstCall().callsArgWith(3, null);
|
||||
stubSet.onSecondCall().callsArgWith(3, "error2");
|
||||
stubSet.onThirdCall().callsArgWith(3, null);
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
||||
if (err === "error2") {
|
||||
done();
|
||||
} else {
|
||||
done("An error occurred");
|
||||
}
|
||||
});
|
||||
}).catch(function(err){ done(err); });
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete context',function(){
|
||||
|
Loading…
Reference in New Issue
Block a user