node-red/red/runtime/nodes/context/index.js

201 lines
5.9 KiB
JavaScript
Raw Normal View History

2015-12-29 23:16:51 +01:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
2015-12-29 23:16:51 +01:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var clone = require("clone");
var util = require("../../util");
var settings;
var contexts = {};
var globalContext = null;
var externalContexts = {};
2018-03-28 08:54:16 +02:00
var noContextStorage = false;
function init(_settings) {
settings = _settings;
externalContexts = {};
2015-12-29 23:16:51 +01:00
2018-03-28 08:54:16 +02:00
// init memory plugin
externalContexts["_"] = require("./memory");
externalContexts["_"].init();
globalContext = createContext("global",settings.functionGlobalContext || {});
}
function load() {
// load & init plugins in settings.contextStorage
var plugins = settings.contextStorage;
var alias = null;
if (plugins) {
2018-03-28 08:54:16 +02:00
noContextStorage = false;
for(var pluginName in plugins){
if(pluginName === "_"){
continue;
}
if(pluginName === "default" && typeof plugins[pluginName] === "string"){
alias = plugins[pluginName];
continue;
}
var plugin;
if(plugins[pluginName].hasOwnProperty("module")){
var config = plugins[pluginName].config || {};
copySettings(config, settings);
2018-03-28 08:54:16 +02:00
if(typeof plugins[pluginName].module === "string") {
try{
plugin = require("./"+plugins[pluginName].module);
}catch(err){
throw new Error(plugins[pluginName].module + " could not be loaded");
}
} else {
plugin = plugins[pluginName].module;
}
plugin.init(config);
externalContexts[pluginName] = plugin;
}else{
2018-03-28 08:54:16 +02:00
throw new Error("module is not defined in settings.contextStorage." + pluginName );
}
}
if(alias){
if(externalContexts.hasOwnProperty(alias)){
externalContexts["default"] = externalContexts[alias];
}else{
2018-03-28 08:54:16 +02:00
throw new Error("default is invalid. module name=" + plugins["default"])
}
}
2018-03-28 08:54:16 +02:00
} else {
noContextStorage = true;
}
}
function copySettings(config, settings){
var copy = ["userDir"]
config.settings = {};
copy.forEach(function(setting){
config.settings[setting] = clone(settings[setting]);
});
}
2018-03-28 08:54:16 +02:00
function parseKey(key){
if(!key){
return null;
}
var keyPath = {storage: "", key: ""};
var index_$ = key.indexOf("$");
var index_dot = key.indexOf(".", 1);
if(index_$===0&&index_dot) {
keyPath.storage = key.substring(1,index_dot)||"default";
keyPath.key = key.substring(index_dot+1);
} else {
keyPath.key = key;
}
return keyPath;
}
function getContextStorage(keyPath) {
if (noContextStorage || !keyPath.storage) {
return externalContexts["_"];
} else if (externalContexts.hasOwnProperty(keyPath.storage)) {
return externalContexts[keyPath.storage];
} else if (externalContexts.hasOwnProperty("default")) {
return externalContexts["default"];
} else {
var contextError = new Error(keyPath.storage + " is not defined in contextStorage on settings.js");
contextError.name = "ContextError";
throw contextError;
}
}
function createContext(id,seed) {
var scope = id;
var obj = seed || {};
obj.get = function(key) {
2018-03-28 08:54:16 +02:00
var keyPath = parseKey(key);
var context = getContextStorage(keyPath);
return context.get(keyPath.key, scope);
};
obj.set = function(key, value) {
2018-03-28 08:54:16 +02:00
var keyPath = parseKey(key);
var context = getContextStorage(keyPath);
return context.set(keyPath.key, value, scope);
};
obj.keys = function() {
//TODO: discuss about keys() behavior
var keys = [];
for(var plugin in externalContexts){
keys.concat(externalContexts[plugin].keys(scope));
}
return keys;
};
if(id === "global"){
externalContexts["_"].setGlobalContext(seed);
}
2015-12-29 23:16:51 +01:00
return obj;
}
function getContext(localId,flowId) {
var contextId = localId;
if (flowId) {
contextId = localId+":"+flowId;
2015-12-29 23:16:51 +01:00
}
if (contexts.hasOwnProperty(contextId)) {
2015-12-29 23:16:51 +01:00
return contexts[contextId];
}
var newContext = createContext(contextId);
if (flowId) {
newContext.flow = getContext(flowId);
}
if (globalContext) {
newContext.global = globalContext;
2015-12-29 23:16:51 +01:00
}
contexts[contextId] = newContext;
return newContext;
}
2015-12-29 23:16:51 +01:00
function deleteContext(id,flowId) {
var contextId = id;
if (flowId) {
contextId = id+":"+flowId;
}
for(var plugin in externalContexts){
externalContexts[plugin].delete(contextId);
2015-12-29 23:16:51 +01:00
}
delete contexts[contextId];
}
2015-12-29 23:16:51 +01:00
function clean(flowConfig) {
var activeIds = {};
var contextId;
var node;
for (var id in contexts) {
if (contexts.hasOwnProperty(id)) {
var idParts = id.split(":");
if (!flowConfig.allNodes.hasOwnProperty(idParts[0])) {
for(var plugin in externalContexts){
externalContexts[plugin].delete(id);
}
2015-12-29 23:16:51 +01:00
delete contexts[id];
}
}
}
}
2015-12-29 23:16:51 +01:00
module.exports = {
init: init,
load: load,
2015-12-29 23:16:51 +01:00
get: getContext,
2016-06-11 23:53:27 +02:00
delete: deleteContext,
2015-12-29 23:16:51 +01:00
clean:clean
};