From 9e6dcbdc6f39e9e369b543dffe512e06ceaa22ce Mon Sep 17 00:00:00 2001 From: Everton Fraga Date: Wed, 24 Aug 2016 14:10:22 -0300 Subject: [PATCH] Fixes blank screen at startup (#1110) * Removing unneeded code * Checking if DB is writable * Resolving DB sync when database is empty * Enhancing selected tab defaults at startup * Checking for R/W permissions * Restoring log entry * Improving error handling --- interface/client/appStart.js | 19 ++++++-------- modules/db.js | 8 ++---- modules/syncMinimongo.js | 48 ++++++++++++++---------------------- 3 files changed, 28 insertions(+), 47 deletions(-) diff --git a/interface/client/appStart.js b/interface/client/appStart.js index 5ec4d4bc1..bbc62ae66 100644 --- a/interface/client/appStart.js +++ b/interface/client/appStart.js @@ -4,11 +4,6 @@ if(location.hash) return; - -// set browser as default tab -if(!LocalStore.get('selectedTab')) - LocalStore.set('selectedTab', 'wallet'); - /** The init function of Mist @@ -17,7 +12,6 @@ The init function of Mist mistInit = function(){ console.info('Initialise Mist'); - Tabs.onceSynced.then(function() { if (0 <= location.search.indexOf('reset-tabs')) { console.info('Resetting UI tabs'); @@ -35,17 +29,20 @@ mistInit = function(){ }); } - Tabs.upsert({_id: 'wallet'}, { url: 'https://wallet.ethereum.org', position: 1, permissions: { admin: true } - }); - }) - .then(function() { - window.trigger('mist-ready'); + }); + + // Sets browser as default tab if: + // 1) there's no record of selected tab + // 2) data is corrupted (no saved tab matches localstore) + if(!LocalStore.get('selectedTab') || !Tabs.findOne(LocalStore.get('selectedTab'))){ + LocalStore.set('selectedTab', 'wallet'); + } }); }; diff --git a/modules/db.js b/modules/db.js index 7a4ef4b30..20fde7480 100644 --- a/modules/db.js +++ b/modules/db.js @@ -15,10 +15,8 @@ exports.init = function() { return Q.try(() => { // if db file doesn't exist then create it try { - log.debug(`Check that db exists: ${filePath}`); - - fs.accessSync(filePath, fs.R_OK); - + log.debug('Check that db exists and it\'s writeable: ${filePath}'); + fs.accessSync(filePath, fs.R_OK | fs.W_OK); return Q.resolve(); } catch (err) { log.info(`Creating db: ${filePath}`); @@ -43,10 +41,8 @@ exports.init = function() { autoloadCallback: function(err) { if (err) { log.error(err); - reject(new Error('Error instantiating db')); } - resolve(); } }); diff --git a/modules/syncMinimongo.js b/modules/syncMinimongo.js index 9bd42fabf..86451482f 100644 --- a/modules/syncMinimongo.js +++ b/modules/syncMinimongo.js @@ -1,21 +1,15 @@ /** @module syncMinimongo */ - - var electron = require('electron'); - - /** * Sync IPC calls received from given window into given db table. * @param {Object} coll Db collection to save to. */ exports.backendSync = function() { var log = require('./utils/logger').create('syncMinimongo'); - var db = require('./db'); - var ipc = electron.ipcMain; ipc.on('minimongo-add', function(event, args) { @@ -28,7 +22,6 @@ exports.backendSync = function() { if (!coll.findOne({_id: _id})) { args.fields._id = _id; - coll.insert(args.fields); } }); @@ -40,7 +33,6 @@ exports.backendSync = function() { log.trace('minimongo-changed', collName, args._id); var _id = args._id; - var item = coll.findOne({_id: _id}); if (item) { @@ -61,7 +53,6 @@ exports.backendSync = function() { log.trace('minimongo-removed', collName, args._id); var _id = args._id; - var item = coll.findOne({_id: _id}); if (item) { @@ -74,9 +65,8 @@ exports.backendSync = function() { // get all data (synchronous) ipc.on('minimongo-reloadSync', function(event, args) { var collName = args.collName, - coll = db.getCollection(collName); - - var docs = coll.find(); + coll = db.getCollection(collName), + docs = coll.find(); log.debug('minimongo-reloadSync, no. of docs:', collName, docs.length); @@ -100,34 +90,32 @@ exports.backendSync = function() { exports.frontendSync = function(coll) { - var ipc = electron.ipcRenderer; - - var collName = coll._name; + var ipc = electron.ipcRenderer, + collName = coll._name, + syncDoneResolver; console.debug('Reload collection from backend: ', collName); - var syncDoneResolver = null; coll.onceSynced = new Promise(function(resolve, reject) { syncDoneResolver = resolve; }); - (new Promise(function(resolve, reject) { - var dataStr = ipc.sendSync('minimongo-reloadSync', { + var dataStr, dataJson; + + dataStr = ipc.sendSync('minimongo-reloadSync', { collName: collName }); - if (!dataStr) { - return resolve(); - } - try { - coll.remove({}); - - var dataJson = JSON.parse(dataStr); + if (!dataStr || (dataJson = JSON.parse(dataStr)).length == 0) { + return resolve(); + } var done = 0; + coll.remove({}); + // we do inserts slowly, to avoid race conditions when it comes // to updating the UI dataJson.forEach(function(record) { @@ -141,9 +129,9 @@ exports.frontendSync = function(coll) { record.redirect = null; } - coll.insert(record); + coll.insert(record); } catch (err) { - console.error(err.toString()); + console.error(err.toString()); } done++; @@ -152,7 +140,7 @@ exports.frontendSync = function(coll) { resolve(); } }); - }); + }); } catch (err) { reject(err); } @@ -163,10 +151,10 @@ exports.frontendSync = function(coll) { .then(function() { // start watching for changes coll.find().observeChanges({ - 'added': function(id, fields){ + 'added': function(id, fields){ ipc.send('minimongo-add', { collName: collName, - _id: id, + _id: id, fields: fields, }); },