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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var clone = require("clone");
|
|
|
|
var when = require("when");
|
|
|
|
var util = require("../util");
|
|
|
|
|
|
|
|
function createContext(id,seed) {
|
2016-01-11 12:28:01 +01:00
|
|
|
var data = seed || {};
|
2015-12-29 23:16:51 +01:00
|
|
|
var obj = seed || {};
|
|
|
|
obj.get = function get(key) {
|
|
|
|
return util.getMessageProperty(data,key);
|
|
|
|
};
|
|
|
|
obj.set = function set(key, value) {
|
|
|
|
util.setMessageProperty(data,key,value);
|
|
|
|
}
|
2017-05-03 21:51:33 +02:00
|
|
|
obj.keys = function() {
|
2017-10-10 22:13:38 +02:00
|
|
|
var keysData = Object.keys(data);
|
|
|
|
if (seed == null) {
|
|
|
|
return keysData;
|
|
|
|
} else {
|
|
|
|
return keysData.filter(function (key) {
|
|
|
|
return key !== "set" && key !== "get" && key !== "keys";
|
|
|
|
});
|
|
|
|
}
|
2017-05-03 21:51:33 +02:00
|
|
|
}
|
2015-12-29 23:16:51 +01:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
var contexts = {};
|
|
|
|
var globalContext = null;
|
|
|
|
|
|
|
|
function getContext(localId,flowId) {
|
|
|
|
var contextId = localId;
|
|
|
|
if (flowId) {
|
|
|
|
contextId = localId+":"+flowId;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
function deleteContext(id,flowId) {
|
|
|
|
var contextId = id;
|
|
|
|
if (flowId) {
|
|
|
|
contextId = id+":"+flowId;
|
|
|
|
}
|
|
|
|
delete contexts[contextId];
|
|
|
|
}
|
|
|
|
function clean(flowConfig) {
|
|
|
|
var activeIds = {};
|
|
|
|
var contextId;
|
|
|
|
var node;
|
|
|
|
for (var id in contexts) {
|
|
|
|
if (contexts.hasOwnProperty(id)) {
|
|
|
|
var idParts = id.split(":");
|
2016-07-04 00:08:53 +02:00
|
|
|
if (!flowConfig.allNodes.hasOwnProperty(idParts[0])) {
|
2015-12-29 23:16:51 +01:00
|
|
|
delete contexts[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
init: function(settings) {
|
|
|
|
globalContext = createContext("global",settings.functionGlobalContext || {});
|
|
|
|
},
|
|
|
|
get: getContext,
|
2016-06-11 23:53:27 +02:00
|
|
|
delete: deleteContext,
|
2015-12-29 23:16:51 +01:00
|
|
|
clean:clean
|
|
|
|
};
|