From e8d76b055521605de9104b397901ced904594009 Mon Sep 17 00:00:00 2001 From: HirokiUchikawa Date: Thu, 12 Jul 2018 11:13:29 +0900 Subject: [PATCH] Allow multiple values to be passed to `get` --- red/runtime/nodes/context/index.js | 59 ++++++++++++++--- test/red/runtime/nodes/context/index_spec.js | 70 ++++++++++++++++++++ 2 files changed, 121 insertions(+), 8 deletions(-) diff --git a/red/runtime/nodes/context/index.js b/red/runtime/nodes/context/index.js index e926d8c7d..0927dd9c6 100644 --- a/red/runtime/nodes/context/index.js +++ b/red/runtime/nodes/context/index.js @@ -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) { diff --git a/test/red/runtime/nodes/context/index_spec.js b/test/red/runtime/nodes/context/index_spec.js index 04a30a8f2..d4f33059e 100644 --- a/test/red/runtime/nodes/context/index_spec.js +++ b/test/red/runtime/nodes/context/index_spec.js @@ -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(){