1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Improve context storage handling

This commit is contained in:
HirokiUchikawa 2018-06-26 11:43:37 +09:00
parent cce7ac09d0
commit 40ff54f67e
2 changed files with 42 additions and 21 deletions

View File

@ -22,7 +22,7 @@ var settings;
var contexts = {}; var contexts = {};
var globalContext = null; var globalContext = null;
var externalContexts = {}; var externalContexts = {};
var noContextStorage = false; var noContextStorage = true;
function init(_settings) { function init(_settings) {
settings = _settings; settings = _settings;
@ -96,9 +96,7 @@ function copySettings(config, settings){
} }
function getContextStorage(storage) { function getContextStorage(storage) {
if (noContextStorage || !storage) { if (externalContexts.hasOwnProperty(storage)) {
return externalContexts["_"];
} else if (externalContexts.hasOwnProperty(storage)) {
return externalContexts[storage]; return externalContexts[storage];
} else if (externalContexts.hasOwnProperty("default")) { } else if (externalContexts.hasOwnProperty("default")) {
return externalContexts["default"]; return externalContexts["default"];
@ -114,29 +112,52 @@ function createContext(id,seed) {
var obj = seed || {}; var obj = seed || {};
obj.get = function(key, storage, callback) { obj.get = function(key, storage, callback) {
if (typeof storage === 'function') { var context;
callback = storage; if (!storage && !callback) {
storage = "default"; context = externalContexts["_"];
} else if(typeof storage === "string" && typeof callback !== 'function'){ } else {
throw new Error("Callback must be a function"); if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
if (typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
} }
return getContextStorage(storage).get(scope, key, callback); return context.get(scope, key, callback);
}; };
obj.set = function(key, value, storage, callback) { obj.set = function(key, value, storage, callback) {
if (typeof storage === 'function') { var context;
callback = storage; if (!storage && !callback) {
storage = "default"; context = externalContexts["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
} }
getContextStorage(storage).set(scope, key, value, callback); context.set(scope, key, value, callback);
}; };
obj.keys = function(storage, callback) { obj.keys = function(storage, callback) {
if (typeof storage === 'function') { var context;
callback = storage; if (!storage && !callback) {
storage = "default"; context = externalContexts["_"];
} else if(typeof storage === "string" && typeof callback !== 'function'){ } else {
throw new Error("Callback must be a function"); if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
if (typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
} }
return getContextStorage(storage).keys(scope, callback); return context.keys(scope, callback);
}; };
return obj; return obj;
} }

View File

@ -263,7 +263,7 @@ describe('context', function() {
Context.init({contextStorage:{_:{module:testPlugin}}}); Context.init({contextStorage:{_:{module:testPlugin}}});
Context.load().then(function(){ Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
var cb = function(){done("An error occurred")} var cb = function(){}
context.set("foo","bar","_",cb); context.set("foo","bar","_",cb);
context.get("foo","_",cb); context.get("foo","_",cb);
context.keys("_",cb); context.keys("_",cb);