From db3356cec075e33b205fa67348791a47afd33059 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 29 Jun 2018 17:49:17 -0400 Subject: [PATCH 1/2] detect if there is a connection error and pass it down to the ready function --- lib/contracts/code_generator.js | 4 ++-- lib/contracts/code_templates/load-manager.js.ejs | 6 +++--- lib/contracts/code_templates/web3-connector.js.ejs | 8 +++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index ef9fb651..dc835430 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -135,9 +135,9 @@ class CodeGenerator { } else { let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]"; if (self.env === 'development') { - web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();', warnAboutMetamask: true}); + web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done(err);', warnAboutMetamask: true}); } else { - web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();', warnAboutMetamask: true}); + web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done(err);', warnAboutMetamask: true}); } } diff --git a/lib/contracts/code_templates/load-manager.js.ejs b/lib/contracts/code_templates/load-manager.js.ejs index ff9b8259..0dd7f561 100644 --- a/lib/contracts/code_templates/load-manager.js.ejs +++ b/lib/contracts/code_templates/load-manager.js.ejs @@ -1,4 +1,4 @@ -__mainContext.__LoadManager = function() { this.list = []; this.done = false; } -__mainContext.__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } } -__mainContext.__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) } +__mainContext.__LoadManager = function() { this.list = []; this.done = false; this.err = null; } +__mainContext.__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(this.err); } else { this.list.push(cb) } } +__mainContext.__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function(err) { self.done = true; self.err = err; self.list.map((x) => x.apply(x, [self.err])) }) } __mainContext.__loadManagerInstance = new __mainContext.__LoadManager(); diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index f44368db..2cf5f9b5 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -25,12 +25,14 @@ __reduce(<%- connectionList %>,function(prev, value, next) { }); }, function(err, _result) { __getAccounts(function(err, accounts) { - web3.eth.defaultAccount = accounts[0]; <% if (warnAboutMetamask) { %> - if (web3.eth.currentProvider.isMetaMask) { - console.log("Note: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node"); + if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) { + console.log("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em"); } <% } %> + if (accounts) { + web3.eth.defaultAccount = accounts[0]; + } <%- done %> }); }); From 6ee387653d8db833f08b5595557565cabb4bc346 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 29 Jun 2018 18:00:21 -0400 Subject: [PATCH 2/2] fix metamask check for non dev environment --- lib/contracts/code_generator.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index dc835430..d7943fa5 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -126,7 +126,6 @@ class CodeGenerator { result += plugin.generateProvider(self) + "\n"; }); } else { - let web3Load; if (isDeployment) { @@ -134,11 +133,8 @@ class CodeGenerator { web3Load = Templates.define_web3_simple({url: connection, done: 'done();'}); } else { let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]"; - if (self.env === 'development') { - web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done(err);', warnAboutMetamask: true}); - } else { - web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done(err);', warnAboutMetamask: true}); - } + let isDev = (self.env === 'development'); + web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done(err);', warnAboutMetamask: isDev}); } result += Templates.do_when_loaded({block: web3Load});