From a8ec032553cc4fa51803b84fff411507c1b4dfa7 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 14 Sep 2018 23:21:05 +0100 Subject: [PATCH] Allow context store name to be provided in the key For nodes that get/set context, when multiple stores are configured they will not know to parse the store name from the key. So they will pass the store name in the key, such as #:(store)::key. Currently that will cause that full string to be used as the key and the default context store used - which is wrong. The code now parses out the store name from the key if it is set - athough if the call to get/set does include the store argument, it will take precedence. This only applies when the key is a string - it doesn't apply when an array of keys is provided. --- red/runtime/nodes/context/index.js | 52 ++++++++++++++------ test/red/runtime/nodes/context/index_spec.js | 17 +++++++ 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/red/runtime/nodes/context/index.js b/red/runtime/nodes/context/index.js index f048947ad..d08734a2f 100644 --- a/red/runtime/nodes/context/index.js +++ b/red/runtime/nodes/context/index.js @@ -225,18 +225,28 @@ function createContext(id,seed) { get: { value: function(key, storage, callback) { var context; - if (!storage && !callback) { - context = stores["_"]; + + if (!callback && typeof storage === 'function') { + callback = storage; + storage = undefined; + } + if (callback && typeof callback !== 'function'){ + throw new Error("Callback must be a function"); + } + + if (!Array.isArray(key)) { + var keyParts = util.parseContextStore(key); + key = keyParts.key; + if (!storage) { + storage = keyParts.store || "_"; + } } else { - if (typeof storage === 'function') { - callback = storage; + if (!storage) { storage = "_"; } - if (callback && typeof callback !== 'function'){ - throw new Error("Callback must be a function"); - } - context = getContextStorage(storage); } + context = getContextStorage(storage); + if (callback) { if (!seed) { context.get(scope,key,callback); @@ -270,18 +280,28 @@ function createContext(id,seed) { set: { value: function(key, value, storage, callback) { var context; - if (!storage && !callback) { - context = stores["_"]; + + if (!callback && typeof storage === 'function') { + callback = storage; + storage = undefined; + } + if (callback && typeof callback !== 'function'){ + throw new Error("Callback must be a function"); + } + + if (!Array.isArray(key)) { + var keyParts = util.parseContextStore(key); + key = keyParts.key; + if (!storage) { + storage = keyParts.store || "_"; + } } else { - if (typeof storage === 'function') { - callback = storage; + if (!storage) { storage = "_"; } - if (callback && typeof callback !== 'function') { - throw new Error("Callback must be a function"); - } - context = getContextStorage(storage); } + context = getContextStorage(storage); + context.set(scope, key, value, callback); } }, diff --git a/test/red/runtime/nodes/context/index_spec.js b/test/red/runtime/nodes/context/index_spec.js index 3beb9b201..c723a0a6e 100644 --- a/test/red/runtime/nodes/context/index_spec.js +++ b/test/red/runtime/nodes/context/index_spec.js @@ -499,6 +499,23 @@ describe('context', function() { done(); }).catch(done); }); + + it('should allow the store name to be provide in the key', function(done) { + Context.init({contextStorage:contextDefaultStorage}); + Context.load().then(function(){ + var context = Context.get("1","flow"); + var cb = function(){done("An error occurred")} + context.set("#:(test)::foo","bar"); + context.get("#:(test)::foo"); + stubGet2.called.should.be.false(); + stubSet2.called.should.be.false(); + stubSet.calledWithExactly("1:flow","foo","bar",undefined).should.be.true(); + stubGet.calledWith("1:flow","foo").should.be.true(); + done(); + }).catch(done); + }); + + it('should use default as the alias of other context', function(done) { Context.init({contextStorage:contextAlias}); Context.load().then(function(){