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(){