Small tidy up on context plugin loading

This commit is contained in:
Nick O'Leary 2018-06-29 09:48:38 +01:00
parent bc01f9f8fd
commit 466cb4be89
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 72 additions and 43 deletions

View File

@ -159,6 +159,7 @@
"context": { "context": {
"error-module-not-loaded": "'__module__' could not be loaded", "error-module-not-loaded": "'__module__' could not be loaded",
"error-loading-module": "Error loading context module '__module__': __message__ ",
"error-module-not-defined": "'module' is not defined in '__storage__' of settings.contextStorage", "error-module-not-defined": "'module' is not defined in '__storage__' of settings.contextStorage",
"error-invalid-default-module": "Invalid storage '__storage__' is specified as a default storage", "error-invalid-default-module": "Invalid storage '__storage__' is specified as a default storage",
"error-use-undefined-storage": "Undefined storage '__storage__' is specified" "error-use-undefined-storage": "Undefined storage '__storage__' is specified"

View File

@ -22,7 +22,7 @@ var settings;
var contexts = {}; var contexts = {};
var globalContext = null; var globalContext = null;
var externalContexts = {}; var externalContexts = {};
var noContextStorage = true; var hasExternalContext = false;
function init(_settings) { function init(_settings) {
settings = _settings; settings = _settings;
@ -37,55 +37,80 @@ function init(_settings) {
} }
function load() { function load() {
// load & init plugins in settings.contextStorage return new Promise(function(resolve,reject) {
var plugins = settings.contextStorage; // load & init plugins in settings.contextStorage
var isAlias = false; var plugins = settings.contextStorage;
if (plugins) { var defaultIsAlias = false;
var promises = []; var promises = [];
noContextStorage = false; if (plugins) {
for(var pluginName in plugins){ for (var pluginName in plugins) {
if(pluginName === "_"){ if (plugins.hasOwnProperty(pluginName)) {
continue; // "_" is a reserved name - do not allow it to be overridden
} if (pluginName === "_") {
if(pluginName === "default" && typeof plugins[pluginName] === "string"){ continue;
isAlias = true; }
continue;
} // Check if this is setting the 'default' context to be a named plugin
var plugin; if (pluginName === "default" && typeof plugins[pluginName] === "string") {
if(plugins[pluginName].hasOwnProperty("module")){ // Check the 'default' alias exists before initialising anything
var config = plugins[pluginName].config || {}; if (!plugins.hasOwnProperty(plugins[pluginName])) {
copySettings(config, settings); return reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]})));
if(typeof plugins[pluginName].module === "string") { }
try{ defaultIsAlias = true;
plugin = require("./"+plugins[pluginName].module); continue;
}catch(err){ }
return Promise.reject(new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module}))); var plugin;
if (plugins[pluginName].hasOwnProperty("module")) {
// Get the provided config and copy in the 'approved' top-level settings (eg userDir)
var config = plugins[pluginName].config || {};
copySettings(config, settings);
if (typeof plugins[pluginName].module === "string") {
// This config identifies the module by name - assume it is a built-in one
// TODO: check it exists locally, if not, try to require it as-is
try {
plugin = require("./"+plugins[pluginName].module);
} catch(err) {
return reject(new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module})));
}
} else {
// Assume `module` is an already-required module we can use
plugin = plugins[pluginName].module;
}
try {
// Create a new instance of the plugin by calling its module function
externalContexts[pluginName] = plugin(config);
} catch(err) {
return reject(new Error(log._("context.error-loading-module",{module:pluginName,message:err.toString()})));
}
} else {
// Plugin does not specify a 'module'
return reject(new Error(log._("context.error-module-not-defined", {storage:pluginName})));
} }
} else {
plugin = plugins[pluginName].module;
} }
externalContexts[pluginName] = plugin(config);
}else{
return Promise.reject(new Error(log._("context.error-module-not-defined", {storage:pluginName})));
} }
}
for(var plugin in externalContexts){ // Open all of the configured contexts
if(externalContexts.hasOwnProperty(plugin)){ for (var plugin in externalContexts) {
promises.push(externalContexts[plugin].open()); if (externalContexts.hasOwnProperty(plugin)) {
promises.push(externalContexts[plugin].open());
}
} }
}
if(isAlias){ // If 'default' is an alias, point it at the right module - we have already
if(externalContexts.hasOwnProperty(plugins["default"])){ // checked that it exists
if (defaultIsAlias) {
externalContexts["default"] = externalContexts[plugins["default"]]; externalContexts["default"] = externalContexts[plugins["default"]];
}else{
return Promise.reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]})));
} }
} }
return Promise.all(promises);
} else { if (promises.length === 0) {
noContextStorage = true; promises.push(externalContexts["_"].open())
return externalContexts["_"].open(); } else {
} hasExternalContext = true;
}
return resolve(Promise.all(promises));
});
} }
function copySettings(config, settings){ function copySettings(config, settings){
@ -98,10 +123,13 @@ function copySettings(config, settings){
function getContextStorage(storage) { function getContextStorage(storage) {
if (externalContexts.hasOwnProperty(storage)) { if (externalContexts.hasOwnProperty(storage)) {
// A known context
return externalContexts[storage]; return externalContexts[storage];
} else if (externalContexts.hasOwnProperty("default")) { } else if (externalContexts.hasOwnProperty("default")) {
// Not known, but we have a default to fall back to
return externalContexts["default"]; return externalContexts["default"];
} else { } else {
// Not known and no default configured
var contextError = new Error(log._("context.error-use-undefined-storage", {storage:storage})); var contextError = new Error(log._("context.error-use-undefined-storage", {storage:storage}));
contextError.name = "ContextError"; contextError.name = "ContextError";
throw contextError; throw contextError;
@ -183,7 +211,7 @@ function getContext(localId,flowId) {
} }
function deleteContext(id,flowId) { function deleteContext(id,flowId) {
if(noContextStorage){ if(!hasExternalContext){
var contextId = id; var contextId = id;
if (flowId) { if (flowId) {
contextId = id+":"+flowId; contextId = id+":"+flowId;