fix loading order

This commit is contained in:
Iuri Matias 2017-10-07 16:51:56 -04:00
parent a9e63069fb
commit 3d88138804
1 changed files with 21 additions and 14 deletions

View File

@ -88,6 +88,12 @@ result += "\n";
result += "\n })(0, memo);"; result += "\n })(0, memo);";
result += "\n};"; 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 += "\nvar whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {"; result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);"; result += "\n document.addEventListener('DOMContentLoaded', cb);";
@ -105,7 +111,7 @@ result += "\n";
result += plugin.generateProvider(self) + "\n"; result += plugin.generateProvider(self) + "\n";
}); });
} else { } else {
result += "\nwhenEnvIsLoaded(function() {\n"; result += "\nwhenEnvIsLoaded(__loadManagerInstance.doFirst(function(done) {\n";
result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }"; 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 += "\nweb3.eth.getAccounts(function(err, account) { if(err) { next(null, true) } else { next(null, false) }})";
connectionCode += "\n}, function(err, _result) {"; connectionCode += "\n}, function(err, _result) {";
connectionCode += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
connectionCode += '\ndone();';
connectionCode += "\n});"; connectionCode += "\n});";
result += connectionCode; result += connectionCode;
} }
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; result += '\n}))';
result += '\n})';
} }
return result; return result;
@ -175,21 +182,21 @@ result += "\n";
let gasEstimates = JSON.stringify(contract.gasEstimates); let gasEstimates = JSON.stringify(contract.gasEstimates);
// TODO: refactor this // TODO: refactor this
result += "\nif (whenEnvIsLoaded === undefined) {"; //result += "\nif (whenEnvIsLoaded === undefined) {";
result += "\n var whenEnvIsLoaded = function(cb) {"; //result += "\n var whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {"; //result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);"; //result += "\n document.addEventListener('DOMContentLoaded', cb);";
result += "\n } else {"; //result += "\n } else {";
result += "\n cb();"; //result += "\n cb();";
result += "\n }"; //result += "\n }";
result += "\n }"; //result += "\n }";
result += "\n}"; //result += "\n}";
result += "\nwhenEnvIsLoaded(function() {"; result += "\n__loadManagerInstance.execWhenReady(function() {";
result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }"; result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }";
if (useEmbarkJS) { if (useEmbarkJS) {
let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined"; 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 { } else {
result += "\n" + className + "Abi = " + abi + ";"; result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);"; result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";