1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Expose list of context stores to the editor

This commit is contained in:
Nick O'Leary 2018-07-03 14:17:42 +01:00
parent a1251371d7
commit c440a4c730
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 39 additions and 27 deletions

View File

@ -40,6 +40,8 @@ module.exports = {
}) })
} }
safeSettings.context = runtime.nodes.listContextStores();
var themeSettings = theme.settings(); var themeSettings = theme.settings();
if (themeSettings) { if (themeSettings) {
safeSettings.editorTheme = themeSettings; safeSettings.editorTheme = themeSettings;

View File

@ -25,11 +25,12 @@ var contexts = {};
// A map of store name to instance // A map of store name to instance
var stores = {}; var stores = {};
var storeList = [];
var defaultStore;
// Whether there context storage has been configured or left as default // Whether there context storage has been configured or left as default
var hasConfiguredStore = false; var hasConfiguredStore = false;
var defaultStore = "_";
function init(_settings) { function init(_settings) {
settings = _settings; settings = _settings;
@ -37,15 +38,16 @@ function init(_settings) {
var seed = settings.functionGlobalContext || {}; var seed = settings.functionGlobalContext || {};
contexts['global'] = createContext("global",seed); contexts['global'] = createContext("global",seed);
stores["_"] = new memory(); stores["_"] = new memory();
defaultStore = "memory";
} }
function load() { function load() {
return new Promise(function(resolve,reject) { return new Promise(function(resolve,reject) {
// load & init plugins in settings.contextStorage // load & init plugins in settings.contextStorage
var plugins = settings.contextStorage; var plugins = settings.contextStorage || {};
var defaultIsAlias = false; var defaultIsAlias = false;
var promises = []; var promises = [];
if (plugins) { if (plugins && Object.keys(plugins).length > 0) {
var hasDefault = plugins.hasOwnProperty('default'); var hasDefault = plugins.hasOwnProperty('default');
var defaultName; var defaultName;
for (var pluginName in plugins) { for (var pluginName in plugins) {
@ -104,34 +106,36 @@ function load() {
promises.push(stores[plugin].open()); promises.push(stores[plugin].open());
} }
} }
// There is a 'default' listed in the configuration // There is a 'default' listed in the configuration
if (hasDefault) { if (hasDefault) {
// If 'default' is an alias, point it at the right module - we have already // If 'default' is an alias, point it at the right module - we have already
// checked that it exists. If it isn't an alias, then it will // checked that it exists. If it isn't an alias, then it will
// already be set to a configured store // already be set to a configured store
if (defaultIsAlias) { if (defaultIsAlias) {
stores["default"] = stores[plugins["default"]]; stores["_"] = stores[plugins["default"]];
} defaultStore = plugins["default"];
} else {
stores["_"] = stores["default"]; stores["_"] = stores["default"];
defaultStore = "default";
}
} else if (defaultName) { } else if (defaultName) {
// No 'default' listed, so pick first in list as the default // No 'default' listed, so pick first in list as the default
stores["default"] = stores[defaultName]; stores["_"] = stores[defaultName];
stores["_"] = stores["default"]; defaultStore = defaultName;
} // else there were no stores list the config object - fall through defaultIsAlias = true;
// to below where we default to a memory store
}
if (promises.length === 0) {
// No stores have been configured. Setup the default as an instance
// of memory storage
stores["_"] = memory();
stores["default"] = stores["_"];
promises.push(stores["_"].open())
} else { } else {
// if there's configured storage then the lifecycle is slightly different // else there were no stores list the config object - fall through
// - specifically, we don't delete node context on redeploy // to below where we default to a memory store
storeList = ["memory"];
defaultStore = "memory";
}
hasConfiguredStore = true; hasConfiguredStore = true;
storeList = Object.keys(stores).filter(n=>!(defaultIsAlias && n==="default") && n!== "_");
} else {
// No configured plugins
promises.push(stores["_"].open())
storeList = ["memory"];
defaultStore = "memory";
} }
return resolve(Promise.all(promises)); return resolve(Promise.all(promises));
}); });
@ -149,9 +153,9 @@ function getContextStorage(storage) {
if (stores.hasOwnProperty(storage)) { if (stores.hasOwnProperty(storage)) {
// A known context // A known context
return stores[storage]; return stores[storage];
} else if (stores.hasOwnProperty("default")) { } else if (stores.hasOwnProperty("_")) {
// Not known, but we have a default to fall back to // Not known, but we have a default to fall back to
return stores["default"]; return stores["_"];
} else { } else {
// Not known and no default configured // 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}));
@ -176,7 +180,7 @@ function createContext(id,seed) {
} else { } else {
if (typeof storage === 'function') { if (typeof storage === 'function') {
callback = storage; callback = storage;
storage = "default"; storage = "_";
} }
if (typeof callback !== 'function'){ if (typeof callback !== 'function'){
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
@ -214,7 +218,7 @@ function createContext(id,seed) {
} else { } else {
if (typeof storage === 'function') { if (typeof storage === 'function') {
callback = storage; callback = storage;
storage = "default"; storage = "_";
} }
if (callback && typeof callback !== 'function') { if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
@ -230,7 +234,7 @@ function createContext(id,seed) {
} else { } else {
if (typeof storage === 'function') { if (typeof storage === 'function') {
callback = storage; callback = storage;
storage = "default"; storage = "_";
} }
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
@ -312,9 +316,14 @@ function close() {
return Promise.all(promises); return Promise.all(promises);
} }
function listStores() {
return {default:defaultStore,stores:storeList};
}
module.exports = { module.exports = {
init: init, init: init,
load: load, load: load,
listStores: listStores,
get: getContext, get: getContext,
delete: deleteContext, delete: deleteContext,
clean: clean, clean: clean,

View File

@ -222,5 +222,6 @@ module.exports = {
// Contexts // Contexts
loadContextsPlugin: context.load, loadContextsPlugin: context.load,
closeContextsPlugin: context.close closeContextsPlugin: context.close,
listContextStores: context.listStores
}; };