Add open/close API for context

This commit is contained in:
HirokiUchikawa
2018-05-30 10:24:27 +09:00
parent 28d05e2449
commit 7185bcd51f
8 changed files with 318 additions and 260 deletions

View File

@@ -17,6 +17,7 @@
var clone = require("clone");
var util = require("../../util");
var log = require("../../log");
var when = require("when");
var settings;
var contexts = {};
@@ -30,8 +31,10 @@ function init(_settings) {
// init memory plugin
var memory = require("./memory");
var seed = settings.functionGlobalContext || {};
externalContexts["_"] = memory();
globalContext = createContext("global",settings.functionGlobalContext || {});
externalContexts["_"].setGlobalContext(seed);
globalContext = createContext("global",seed);
}
function load() {
@@ -39,6 +42,7 @@ function load() {
var plugins = settings.contextStorage;
var isAlias = false;
if (plugins) {
var promises = [];
noContextStorage = false;
for(var pluginName in plugins){
if(pluginName === "_"){
@@ -56,25 +60,32 @@ function load() {
try{
plugin = require("./"+plugins[pluginName].module);
}catch(err){
throw new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module}));
return when.reject(new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module})));
}
} else {
plugin = plugins[pluginName].module;
}
externalContexts[pluginName] = plugin(config);
}else{
throw new Error(log._("context.error-module-not-defined", {storage:pluginName}));
return when.reject(new Error(log._("context.error-module-not-defined", {storage:pluginName})));
}
}
if(isAlias){
if(externalContexts.hasOwnProperty(plugins["default"])){
externalContexts["default"] = externalContexts[plugins["default"]];
}else{
throw new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]}));
return when.reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]})));
}
}
for(var plugin in externalContexts){
if(externalContexts.hasOwnProperty(plugin)){
promises.push(externalContexts[plugin].open());
}
}
return when.all(promises);
} else {
noContextStorage = true;
return externalContexts["_"].open();
}
}
@@ -157,9 +168,6 @@ function createContext(id,seed) {
var context = getContextStorage(storageName);
return context.keys(scope);
};
if(id === "global"){
externalContexts["_"].setGlobalContext(seed);
}
return obj;
}
@@ -210,10 +218,21 @@ function clean(flowConfig) {
}
}
function close() {
var promises = [];
for(var plugin in externalContexts){
if(externalContexts.hasOwnProperty(plugin)){
promises.push(externalContexts[plugin].close());
}
}
return when.all(promises);
}
module.exports = {
init: init,
load: load,
get: getContext,
delete: deleteContext,
clean:clean
clean: clean,
close: close
};

View File

@@ -17,11 +17,10 @@
var JsonDB = require('node-json-db');
var fs = require('fs-extra');
var path = require("path");
var when = require("when");
function createStorage(storageBaseDir, scope) {
var i = scope.indexOf(":")
if(i === -1){
if(scope.indexOf(":") === -1){
if(scope === "global"){
return new JsonDB(path.join(storageBaseDir,"global",scope), true, true);
}else{ // scope:flow
@@ -69,6 +68,14 @@ function LocalFileSystem(config){
this.storages = {};
}
LocalFileSystem.prototype.open = function(){
return when.resolve();
}
LocalFileSystem.prototype.close = function(){
return when.resolve();
}
LocalFileSystem.prototype.get = function (scope, key) {
if(!this.storages[scope]){
return undefined;

View File

@@ -15,11 +15,20 @@
**/
var util = require("../../util");
var when = require("when");
function Memory(config){
this.data = {};
}
Memory.prototype.open = function(){
return when.resolve();
};
Memory.prototype.close = function(){
return when.resolve();
};
Memory.prototype.get = function(scope, key) {
if(!this.data[scope]){
return undefined;
@@ -49,14 +58,7 @@ Memory.prototype.keys = function(scope){
Memory.prototype.delete = function(scope){
delete this.data[scope];
};
Memory.prototype.open = function(){
return true;
};
Memory.prototype.close = function(){
delete this.data;
};
Memory.prototype.setGlobalContext= function(seed){