2015-12-29 23:16:51 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +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.
|
|
|
|
**/
|
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
var clone = require("clone");
|
2018-03-15 07:15:26 +01:00
|
|
|
var util = require("../../util");
|
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
var settings;
|
2018-03-15 07:15:26 +01:00
|
|
|
var contexts = {};
|
|
|
|
var globalContext = null;
|
2018-03-23 09:18:56 +01:00
|
|
|
var externalContexts = {};
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function init(_settings) {
|
|
|
|
settings = _settings;
|
|
|
|
externalContexts = {};
|
2015-12-29 23:16:51 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
// init meomory plugin
|
|
|
|
externalContexts["_"] = require("./memory");
|
|
|
|
externalContexts["_"].init();
|
|
|
|
globalContext = createContext("global",settings.functionGlobalContext || {});
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function load() {
|
|
|
|
// load & init plugins in settings.contextStorage
|
|
|
|
var plugins = settings.contextStorage;
|
|
|
|
var alias = null;
|
|
|
|
if (plugins) {
|
|
|
|
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);
|
|
|
|
try{
|
|
|
|
plugin = require("./"+plugins[pluginName].module);
|
|
|
|
}catch(err){
|
|
|
|
throw new Error(plugins[pluginName].module + " could not be loaded");
|
|
|
|
}
|
|
|
|
plugin.init(config);
|
|
|
|
externalContexts[pluginName] = plugin;
|
|
|
|
}else{
|
|
|
|
throw new Error("module is is not defined in settings.contextStorage." + plugins[pluginName] );
|
|
|
|
}
|
2017-10-10 22:13:38 +02:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
if(alias){
|
|
|
|
if(externalContexts.hasOwnProperty(alias)){
|
|
|
|
externalContexts["default"] = externalContexts[alias];
|
|
|
|
}else{
|
|
|
|
throw new Error("default is invalid" + plugins["default"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function copySettings(config, settings){
|
|
|
|
var copy = ["userDir"]
|
|
|
|
config.settings = {};
|
|
|
|
copy.forEach(function(setting){
|
|
|
|
config.settings[setting] = clone(settings[setting]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createContext(id,seed) {
|
|
|
|
var scope = id;
|
|
|
|
var obj = seed || {};
|
2018-03-15 07:15:26 +01:00
|
|
|
|
|
|
|
obj.get = function(key) {
|
2018-03-23 09:18:56 +01:00
|
|
|
var result = parseKey(key);
|
|
|
|
if(!result){
|
|
|
|
return externalContexts["_"].get(key, scope);
|
2018-03-15 07:15:26 +01:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
if(externalContexts.hasOwnProperty(result[0])){
|
|
|
|
return externalContexts[result[0]].get(result[1], scope);
|
|
|
|
}else if(externalContexts.hasOwnProperty("defalut")){
|
|
|
|
return externalContexts["defalut"].get(result[1], scope);
|
|
|
|
}else{
|
|
|
|
throw new Error(result[0] + " is not defined in setting.js");
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
};
|
|
|
|
obj.set = function(key, value) {
|
2018-03-23 09:18:56 +01:00
|
|
|
var result = parseKey(key);
|
|
|
|
if(!result){
|
|
|
|
return externalContexts["_"].set(key, value, scope);
|
2018-03-15 07:15:26 +01:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
if(externalContexts.hasOwnProperty(result[0])){
|
|
|
|
externalContexts[result[0]].set(result[1], value, scope);
|
|
|
|
}else if(externalContexts.hasOwnProperty("defalut")){
|
|
|
|
externalContexts["defalut"].set(result[1], value, scope);
|
2018-03-15 07:15:26 +01:00
|
|
|
}else{
|
2018-03-23 09:18:56 +01:00
|
|
|
throw new Error(result[0] + " is not defined in setting.js");
|
2018-03-15 07:15:26 +01:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
};
|
|
|
|
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);
|
2017-05-03 21:51:33 +02:00
|
|
|
}
|
2015-12-29 23:16:51 +01:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContext(localId,flowId) {
|
|
|
|
var contextId = localId;
|
|
|
|
if (flowId) {
|
2018-03-23 09:18:56 +01:00
|
|
|
contextId = localId+":"+flowId;
|
2015-12-29 23:16:51 +01:00
|
|
|
}
|
2016-07-04 00:08:53 +02: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);
|
2017-04-12 21:48:43 +02:00
|
|
|
}
|
|
|
|
if (globalContext) {
|
|
|
|
newContext.global = globalContext;
|
2015-12-29 23:16:51 +01:00
|
|
|
}
|
|
|
|
contexts[contextId] = newContext;
|
|
|
|
return newContext;
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
function deleteContext(id,flowId) {
|
|
|
|
var contextId = id;
|
|
|
|
if (flowId) {
|
2018-03-23 09:18:56 +01:00
|
|
|
contextId = id+":"+flowId;
|
|
|
|
}
|
|
|
|
for(var plugin in externalContexts){
|
|
|
|
externalContexts[plugin].delete(contextId);
|
2015-12-29 23:16:51 +01:00
|
|
|
}
|
|
|
|
delete contexts[contextId];
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
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)) {
|
2018-03-23 09:18:56 +01:00
|
|
|
var idParts = id.split(":");
|
2016-07-04 00:08:53 +02:00
|
|
|
if (!flowConfig.allNodes.hasOwnProperty(idParts[0])) {
|
2018-03-23 09:18:56 +01:00
|
|
|
for(var plugin in externalContexts){
|
|
|
|
externalContexts[plugin].delete(id);
|
|
|
|
}
|
2015-12-29 23:16:51 +01:00
|
|
|
delete contexts[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-03-23 09:18:56 +01:00
|
|
|
function parseKey(key){
|
|
|
|
var keys = null;
|
|
|
|
if(!key){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var index_$ = key.indexOf("$");
|
|
|
|
var index_dot = key.indexOf(".", 1);
|
|
|
|
if(index_$ === 0 && index_dot){
|
|
|
|
keys = [];
|
|
|
|
keys[0] = key.substring(1,index_dot);
|
|
|
|
keys[1] = key.substring(index_dot + 1);
|
|
|
|
keys[0] = keys[0] || "default";
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
module.exports = {
|
2018-03-23 09:18:56 +01:00
|
|
|
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
|
|
|
|
};
|