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

View File

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