Make storage.init return a promise to async initialisation

part of #62
This commit is contained in:
Nicholas O'Leary
2013-11-12 17:13:06 +00:00
parent 1536dcdf1e
commit 848a69dc26
6 changed files with 205 additions and 197 deletions

View File

@@ -19,7 +19,7 @@ var storage = null;
function init() {
redApp = require("./server").app;
storage = require("./storage").storage;
storage = require("./storage");
// -------- Flow Library --------
redApp.post(new RegExp("/library/flows\/(.*)"), function(req,res) {

View File

@@ -277,7 +277,7 @@ module.exports.setConfig = function(conf) {
if (!storage) {
// Do this lazily to ensure the storage provider as been initialised
storage = require("./storage").storage;
storage = require("./storage");
}
storage.getCredentials().then(function(creds) {
credentials = creds;

View File

@@ -20,12 +20,13 @@ var redNodes = require("./nodes");
var app = null;
var server = null;
var settings = null;
var storage = null;
function createServer(_server,settings) {
function createServer(_server,_settings) {
server = _server;
storage = require("./storage").init(settings);
settings = _settings;
storage = require("./storage");
app = createUI(settings);
flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json';
@@ -61,25 +62,27 @@ function createServer(_server,settings) {
}
function start() {
console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes");
util.log("------------------------------------------");
redNodes.load();
util.log("");
util.log('You may ignore any errors above here if they are for');
util.log('nodes you are not using. The nodes indicated will not');
util.log('be available in the main palette until any missing');
util.log('modules are installed, typically by running:');
util.log(' npm install {the module name}');
util.log('or any other errors are resolved');
util.log("------------------------------------------");
storage.getFlows().then(function(flows) {
if (flows.length > 0) {
redNodes.setConfig(flows);
}
}).otherwise(function(err) {
util.log("[red] Error loading flows : "+err);
storage.init(settings).then(function() {
console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes");
util.log("------------------------------------------");
redNodes.load();
util.log("");
util.log('You may ignore any errors above here if they are for');
util.log('nodes you are not using. The nodes indicated will not');
util.log('be available in the main palette until any missing');
util.log('modules are installed, typically by running:');
util.log(' npm install {the module name}');
util.log('or any other errors are resolved');
util.log("------------------------------------------");
storage.getFlows().then(function(flows) {
if (flows.length > 0) {
redNodes.setConfig(flows);
}
}).otherwise(function(err) {
util.log("[red] Error loading flows : "+err);
});
});
}

View File

@@ -15,19 +15,9 @@
**/
var settings;
var storage;
var settings = require('../red').settings;
module.exports = {
init: function(_settings) {
settings = _settings;
var storageType = settings.storageModule || "localfilesystem";
storage = require("./"+storageType).init(settings);
return storage;
},
};
var storageType = settings.storageModule || "localfilesystem";
module.exports.__defineGetter__("storage", function() { return storage; });
module.exports = require("./"+storageType);

View File

@@ -160,7 +160,9 @@ var localfilesystem = {
if (!fs.existsSync(libFlowsDir)) {
fs.mkdirSync(libFlowsDir);
}
return this;
var defer = when.defer();
defer.resolve();
return defer.promise;
},
getFlows: function() {
var defer = when.defer();