From 3d88138804b318628d1e57fa9b3460f9e821d8b0 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 7 Oct 2017 16:51:56 -0400 Subject: [PATCH] fix loading order --- lib/contracts/code_generator.js | 35 ++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 2a7d2d92d..ab28f9c75 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -88,6 +88,12 @@ result += "\n"; result += "\n })(0, memo);"; result += "\n};"; + result += "\nvar __LoadManager = function() { this.list = []; this.done = false; }"; + result += "\n__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }"; + result += "\n__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }"; + result += "\nthis.__loadManagerInstance = new __LoadManager();"; + result += "\nthis.__mainContext = this;"; + result += "\nvar whenEnvIsLoaded = function(cb) {"; result += "\n if (typeof document !== 'undefined' && document !== null) {"; result += "\n document.addEventListener('DOMContentLoaded', cb);"; @@ -105,7 +111,7 @@ result += "\n"; result += plugin.generateProvider(self) + "\n"; }); } else { - result += "\nwhenEnvIsLoaded(function() {\n"; + result += "\nwhenEnvIsLoaded(__loadManagerInstance.doFirst(function(done) {\n"; result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }"; @@ -138,13 +144,14 @@ result += "\n"; connectionCode += "\nweb3.eth.getAccounts(function(err, account) { if(err) { next(null, true) } else { next(null, false) }})"; connectionCode += "\n}, function(err, _result) {"; + connectionCode += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; + connectionCode += '\ndone();'; connectionCode += "\n});"; result += connectionCode; } - result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; - result += '\n})'; + result += '\n}))'; } return result; @@ -175,21 +182,21 @@ result += "\n"; let gasEstimates = JSON.stringify(contract.gasEstimates); // TODO: refactor this - result += "\nif (whenEnvIsLoaded === undefined) {"; - result += "\n var whenEnvIsLoaded = function(cb) {"; - result += "\n if (typeof document !== 'undefined' && document !== null) {"; - result += "\n document.addEventListener('DOMContentLoaded', cb);"; - result += "\n } else {"; - result += "\n cb();"; - result += "\n }"; - result += "\n }"; - result += "\n}"; + //result += "\nif (whenEnvIsLoaded === undefined) {"; + //result += "\n var whenEnvIsLoaded = function(cb) {"; + //result += "\n if (typeof document !== 'undefined' && document !== null) {"; + //result += "\n document.addEventListener('DOMContentLoaded', cb);"; + //result += "\n } else {"; + //result += "\n cb();"; + //result += "\n }"; + //result += "\n }"; + //result += "\n}"; - result += "\nwhenEnvIsLoaded(function() {"; + result += "\n__loadManagerInstance.execWhenReady(function() {"; result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }"; if (useEmbarkJS) { let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined"; - result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});"; + result += "\n__mainContext." + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});"; } else { result += "\n" + className + "Abi = " + abi + ";"; result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";